Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question

migrated from stackoverflow.com Aug 4 '10 at 13:46

This question came from our site for professional and enthusiast programmers.

    
@David B: runnable file => executable file? –  Lazer Aug 15 '10 at 8:11
add comment

4 Answers 4

up vote 13 down vote accepted

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.

share|improve this answer
add comment

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.

share|improve this answer
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. –  Matthew Flaschen Aug 4 '10 at 14:00
    
Instead of sudo, I'd use ls and rm. Much more likely to be called. –  Aaron Digulla Aug 4 '10 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 '10 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' –  Rich Homolka Aug 4 '10 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. –  Aaron Digulla Aug 5 '10 at 13:15
add comment
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 .)

share|improve this answer
add comment

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.