difference between sh and ./ (176 Views)
Reply
Frequent Advisor
leelangco_1
Posts: 76
Registered: ‎05-17-2007
Message 1 of 7 (176 Views)

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
Acclaimed Contributor
Dennis Handly
Posts: 25,291
Registered: ‎03-06-2006
Message 2 of 7 (176 Views)

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.
Honored Contributor
Yogeeraj_1
Posts: 4,613
Registered: ‎11-25-2001
Message 3 of 7 (176 Views)

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)
Honored Contributor
Jeeshan
Posts: 2,084
Registered: ‎11-05-2007
Message 4 of 7 (176 Views)

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
Acclaimed Contributor
Dennis Handly
Posts: 25,291
Registered: ‎03-06-2006
Message 5 of 7 (176 Views)

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.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 6 of 7 (176 Views)

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...
Frequent Advisor
leelangco_1
Posts: 76
Registered: ‎05-17-2007
Message 7 of 7 (176 Views)

Re: difference between sh and ./

thank you all
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.