Operating System - Linux
1752290 Members
4649 Online
108786 Solutions
New Discussion юеВ

difference between sh and ./

 
leelangco_1
Frequent Advisor

difference between sh and ./

i have a script abc.sh(777 privilege) which written by ksh

i login as root,and use sh abc.sh,it doesnot execute and returns no result.

when i use ./abc.sh,it can be correctly executed .

what is the difference between sh and ./, and why this happen
6 REPLIES 6
Dennis Handly
Acclaimed Contributor

Re: difference between sh and ./

If you use abc.sh or "sh abc.sh", it's going to want to look it up in your $PATH. If you use ./abc.sh, it knows exactly where to find it.

Typically root doesn't put "." in its $PATH as a security precaution.
Yogeeraj_1
Honored Contributor

Re: difference between sh and ./

Hi,

As Dennis mentioned it above, it is a PATH issue.

You can run a script by specifying either of the following:
1. Relative path
e.g.
./abc.sh

2. Full path
e,g,
/home/yogeeraj/abc.sh

3. Specify the location of the script in the PATH environment variable.
e.g.
export PATH=$PATH:/home/yogeeraj/
or
export PATH=$PATH:.

then you can just:
abc.sh

Also, as mentioned root have "." in its $PATH as a security precaution.


hope it is now clear.

kind regards
yogeeraj
No person was ever honoured for what he received. Honour has been the reward for what he gave (clavin coolidge)
Jeeshan
Honored Contributor

Re: difference between sh and ./

Hi

If you run a script using sh , it means you did not mention the which shell the script will use. such as if you mention in the script #!/usr/bin/ksh, you can execute the script using ./abc.sh way. Moreover if the file permissions will not 777 , but with ksh abc.sh will work

>>i login as root,and use sh abc.sh,it doesnot execute and returns no result.

try to run like this

#ksh abc.sh

you are trying to execute the script using POSIX shell and you write the script using korn shell. you have mistaken buddy.

a warrior never quits
Dennis Handly
Acclaimed Contributor

Re: difference between sh and ./

>ahsan: you are trying to execute the script using POSIX shell and you write the script using korn shell. you have mistaken buddy.

Since they are pretty similar, it still may work.
James R. Ferguson
Acclaimed Contributor

Re: difference between sh and ./

Hi:

As noted, on a properly configured system, root's '.profile' does not specify a dot ('.') in the PATH variable it exports to its environment. This means that to specify a search within your current directory, *you* may need to type "./" in front of a file name.

To add a dot to root's PATH is a security risk.

By the way, setting 777 (rwx) permissions on your scripts is very poor security too! There is NO need for your group, and certainly not the world's community, to be able to *write* to your script! A setting of 755 is as far as you should go.

Regards!

...JRF...
leelangco_1
Frequent Advisor

Re: difference between sh and ./

thank you all