shell script--Need Help (253 Views)
Occasional Contributor
Posts: 8
Registered: ‎09-06-2011
Message 1 of 3 (253 Views)

shell script--Need Help

./ seems do magic in many occasions. What is behind this?


./test.ksh works, but test.ksh give test.ksh not found.



Honored Contributor
Duncan Edmonstone
Posts: 5,684
Registered: ‎08-05-2000
Message 2 of 3 (251 Views)

Re: shell script--Need Help

This is down to the PATH environment variable - i.e. the directories that the shell will look in to execute a command. You can see what it is currently set to using:


echo $PATH


So anything in those directories will be searched to execute the command you type.


I'm assuming you are attempting the command you specify as root? For root it is not a good idea to have the current directory on the PATH as it makes it easy for folks to inject trojans into the system by leaving scripts that attack a system in directories with names similar to standard commands. Of course when you are root you get round this by explicitly referencing the current directory by adding ./ to the start of the command.


If you look at standard .profiles for non-root users you will see that often the current directory is added to the PATH as follows:




But it is NOT a good idea to make this change for root.


Acclaimed Contributor
Dennis Handly
Posts: 25,290
Registered: ‎03-06-2006
Message 3 of 3 (239 Views)

Re: shell script-- $PATH lookup

>./ seems do magic in many occasions. What is behind this?


This is elementary shells 101.

As Duncan said, for all commands that are not relative, absolute or builtin, it looks them up in $PATH.

Adding "./" makes the command have a relative path.

The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.