9

I'm using ubuntu and I notice many of my runnable scripts won't run unless I precede theme by ./.

For example, if I have a perl script: /home/me/script.pl and I'm currently in /home/me, typing script.pl won't do it, but ./script.pl will.

Why is that?

1
  • @David B: runnable file => executable file?
    – Lazer
    Aug 15, 2010 at 8:11

4 Answers 4

13

To execute a script, you need to type in the full path to the script unless the script is present in one of the directories listed in your $PATH environment variable. Generally (and by default) . (the current directory) isn't in your $PATH, so you need to type ./script to execute it.

21

Because the current directory is not in your path. This is a safety feature. If it was in your path, someone could potentially drop a malicious copy of a common command, and when you are in that directory instead of running the real sudo, for example, you'd run the fake one. That'd be a bad thing.

5
  • 3
    This is somewhat misleading. As long as . were after /usr/bin/, it wouldn't supersede the real sudo. It is to ensure you don't accidentally add programs to your path, but it's not primarily about superseding existing ones. Aug 4, 2010 at 14:00
  • Instead of sudo, I'd use ls and rm. Much more likely to be called. Aug 4, 2010 at 15:12
  • @aaron: sure but ls won't ask for your password and if the user can use sudo the password is interesting... ;)
    – laurent
    Aug 4, 2010 at 19:10
  • 1
    @Matthew. Yes, it still is a hard to exploit but real security risk. Lets say i put in a script sl that does bad things, then invokes ls, then deletes itself. You can't override the system tools, but you can 'typosquat' Aug 4, 2010 at 23:09
  • @laurent: What I had in mind was: cp /bin/bash /tmp/sfhsdh ; chmod u+s tmp/sfhsdh >& /dev/null ; $0 "$@". No need to know the password. Aug 5, 2010 at 13:15
4
echo $PATH

You must have the current directory (a single dot) in your path for this to work.

You can add it to your path if you want with the following commands.

sh/bash: export PATH=$PATH:.

tcsh/csh: set PATH = ($PATH .)

3

the shell searches the path for executables, and by default ./ is not in the path.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .