Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Necessity of ./ to run a script 2

Status
Not open for further replies.

630111

MIS
Oct 30, 2001
127
US
I heard the reason a long time ago but have forgotten it. Why is it that scripts have to be preceeded with a ./ in order to run them?

Thanks!
630111
 
because if the script is in a folder not contained in the PATH environmet var, the script will not be found... with ./script.sh you tell to the OS to execute the shell located in the current directory. if you want to execute a shell in other folder (not in PATH) execute it with /path/to/script/script.sh

Cheers.
 
630111,

The only reason you need to use the ./ is if you don't have the directory you are working in in your path statement or if you want to execute something in the current directory rather than the one in another location in your path statemnt. Ex. if you wanted to run a script in your working directory called batch, but not /bin/batch, you would type ./batch.

Hope this helps.

John
 
Thanks Chacalinc and johngiggs for your prompt replies.

There is a reason you don't want . in your path but I can't remember it. Anyone know what I'm talking about?

630111
 
. is the current folder, so if you have a shell script that execute another script *out* of the current folder, that script will not be found.
 
It is a security risk having . in your path.

It's especially a risk if you put . at the start of your path. The scenario is someone puts a file called 'ls' in some directory, then you come along and type 'ls' expecting to get a directory listing. But instead of running /bin/ls, you run the attackers script instead. Which may well run /bin/ls and hide itself in the process leaving you none the wiser. Whatever else it does it left to the imagination.

Putting . at the end of your path doesn't buy you much. All the correctly spelled commands will run what you expect to run, but faulty memory and typos will still catch you out.

At least ./script is a rather deliberate key sequence which is less likely to be typed accidentally.

--
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top