[SOLVED] "Command not found" unless I export its path

banderas20

Member
Joined
Aug 1, 2018
Messages
96
Reaction score
39
Credits
746
Hi.

I'm using Linux. I navigate to the directory where the command is, and I call it. I get the "Command not found error".

If I export its path, I can call the command with no problems.

¿Why doesn't it work if I'm located in the exact directory?

Thanks!
 


Preface the program name with ./

So it would be: ./program_name

This tells bash to use the current directory
 
What directory is the command you are want to run in and what command are you wanting to run? what's the output of the following when you run in from a terminal?
Code:
echo $PATH
 
OMG! My mistake. I forgot to add the ./ before the command.

Thanks for your quick reply!
 
@MattWinter is right. Preface the command with " ./ "

There are good reasons not to include the current directory in the PATH, which is why it is not enabled by default.

Some people add "." to their path, but it is not advised. If you must do it, put it at the end of your PATH, not the beginning. I recommend not doing it at all and learning to preface your local command with "./"
 
@MattWinter is right. Preface the command with " ./ "

There are good reasons not to include the current directory in the PATH, which is why it is not enabled by default.

Some people add "." to their path, but it is not advised. If you must do it, put it at the end of your PATH, not the beginning. I recommend not doing it at all and learning to preface your local command with "./"
Adding . to $PATH isn't a great idea.

A better solution would be to create a personal bin directory in your home directory (e.g. $HOME/bin AKA /home/yourusername/bin, or ~/bin for short) and add that to $PATH.
Then you can put any personal scripts, or programs you have in there. Or you could create symbolic links in your personal bin directory that point to executables in other directories, or add wrapper scripts that will cd into another directory and run a script/program that is in there.

However you choose to do it , if you have your own personal bin directory, you can run your own personal scripts and programs for yourself - as if they are installed normally. But without actually installing them system-wide and without affecting the wider system, or other users, if you're on a multi-user system.

I usually add a personal bin directory at $HOME/bin/ to $PATH and I also add $HOME/.local/bin for good measure.

Package managers like pip (for Python) and gem (for Ruby) sometimes install applications to this location, but it's not always added to $PATH. So I like to add it.

Some years ago I installed tuir (terminal based reddit client written in Python) using Pythons package manager pip and discovered that I couldn't run it. The system didn't know where tuir was. It wasn't anywhere in $PATH.
After much investigation, I looked for where the tuir executable had actually been installed to and it was in $HOME/.local/bin along with a few other useful programs that I'd installed and never been able to access.Go figure?!
So I added that location to $PATH alongside my personal bin directory.

At the risk of going hugely off-topic - another handy tip - if you have any personal scripts, or programs that you want to appear in your main system menu/launcher, you can create a custom .desktop file and put it into $HOME/.local/share/applications.

So if you have a terminal-based script called yourscript that you want to be able to run from the menu/launcher, then your .desktop file might look something like this:
yourscript.desktop:
Bash:
#!/usr/bin/env xdg-open
[Desktop Entry]
Encoding=UTF-8
Version=1.0
Name=yourscript
GenericName=your script
Comment="a script that does something useful"
Type=Application
Icon=utilities-terminal
Categories=Utilities;
# uncommenting the next line will make the launcher run the script in the background, without bringing up a window
#Exec=/home/yourusername/bin/yourscript
# Whereas this will open an xterm window and will run yourscript, so you can see your script running.
Exec=xterm -e '/home/yourusername/bin/yourscript;read -p "press any key to  continue..." -n1 -s'
Terminal=true

On most, if not ALL modern window managers / desktop environments, all you need to do is add a .desktop file like the above to $HOME/.local/share/applications and your script should immediately be available in your main menu, or in the launcher.
 
Nice work, folks :)

Moving this to Command Line.

Chris Turner
wizardfromoz
 

Members online


Latest posts

Top