LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-03-2007, 03:45 AM   #1
George2
Member
 
Registered: Oct 2003
Posts: 354

Rep: Reputation: 30
sh command


Hello everyone,


I am wondering what is the differences between executing a shell script itself and using sh command to execute?

Example (supposing foo.sh is a Linux shell script),

1. foo.sh
2. sh foo.sh


thanks in advance,
George
 
Old 09-03-2007, 04:02 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

When executing a script by itself, it must have execute permissions. (example 1 foo.sh)

You can use sh to execute a script that does not have execute permissions (example 2 sh foo.sh).

Hope this helps.
 
Old 09-03-2007, 04:45 AM   #3
George2
Member
 
Registered: Oct 2003
Posts: 354

Original Poster
Rep: Reputation: 30
Thanks druuna,


Quote:
Originally Posted by druuna View Post
Hi,

When executing a script by itself, it must have execute permissions. (example 1 foo.sh)

You can use sh to execute a script that does not have execute permissions (example 2 sh foo.sh).

Hope this helps.
If I grant execution permission, for example, chmod a+x, then using sh to execute or using the script itself to execute directly are the same effect?


regards,
George
 
Old 09-03-2007, 05:24 AM   #4
samwise17
Member
 
Registered: Jul 2007
Location: Sydney
Distribution: Arch,Slackware,Puppy
Posts: 87

Rep: Reputation: 15
The character '#!' at the beginning of a file lets the system know what program executes it,
e.g. #!/bin/bash or #!/bin/perl.
If you run 'sh foo.sh' you probably don't need this, however if you just type './foo.sh' you do.
I generally just put the #!/bin/<whatever> at the top and run scripts by running 'chmod u+x <script> and then './<script>'
 
Old 09-03-2007, 04:20 PM   #5
cfaj
Member
 
Registered: Dec 2003
Location: Toronto, Canada
Distribution: Mint, Mandriva
Posts: 221

Rep: Reputation: 31
Quote:
Originally Posted by samwise17 View Post
The character '#!' at the beginning of a file lets the system know what program executes it,
e.g. #!/bin/bash or #!/bin/perl.
If you run 'sh foo.sh' you probably don't need this, however if you just type './foo.sh' you do.
No, the shebang is not necessary unless you are using an interpreter other than the default shell, or in special circumstances such as a CGI script.
Quote:
I generally just put the #!/bin/<whatever> at the top
I generally don't use a shebang unless it is for special use. I often move scripts between different systems. If the script requires bash, #!/bin/bash will not work on most of them (on one it's /usr/local/bin/bash, on another it's /usr/pkg/bin/bash, etc.).
Quote:
and run scripts by running 'chmod u+x <script> and then './<script>'
I never put scripts in the current directory. I always use a central directory ($HOME/bin or /usr/local/bin) which is in my PATH.
 
Old 09-04-2007, 12:12 AM   #6
George2
Member
 
Registered: Oct 2003
Posts: 354

Original Poster
Rep: Reputation: 30
Thanks samwise17,


Quote:
Originally Posted by samwise17 View Post
The character '#!' at the beginning of a file lets the system know what program executes it,
e.g. #!/bin/bash or #!/bin/perl.
If you run 'sh foo.sh' you probably don't need this, however if you just type './foo.sh' you do.
I generally just put the #!/bin/<whatever> at the top and run scripts by running 'chmod u+x <script> and then './<script>'
Your reply is great! I am wondering the only differences between executing shell script directly (method 1) and using sh to execute shell script (method 2) is, method 2 will use another shell to start the shell script, no other differences?


regards,
George
 
Old 09-04-2007, 12:15 AM   #7
George2
Member
 
Registered: Oct 2003
Posts: 354

Original Poster
Rep: Reputation: 30
Thanks cfaj,


Quote:
Originally Posted by cfaj View Post
[INDENT]No, the shebang is not necessary unless you are using an interpreter other than the default shell, or in special circumstances such as a CGI script.
Two more questions,

1. What is *shebang*?

2. Why *the shebang is not necessary unless you are using an interpreter*? Show an example please?


regards,
George
 
Old 09-04-2007, 01:45 AM   #8
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
The character sequence '#!' is colloquially called 'shebang',
e.g.
#!/bin/bash
or
#!/usr/bin/perl

If you are writing a shell script, it's usually advised (in prod systems at least) to specify (via the #! invocation) which shell is required to run this script properly.
Otherwise, what can happen is that eg you write it in bash, but the default shell on the target system turns out to be csh.
Not all bash cmds are understood by the csh shell.

Using the shebang line as described above means that you simplify the cmd line by not having to specify the actual shell by name externally to the script.
Note that eg cron has a minimal/almost non-existent default shell env, which means you should always specify the complete path for a cmd eg /usr/bin/perl and not the short form eg perl.
HTH
 
Old 09-04-2007, 02:53 AM   #9
cfaj
Member
 
Registered: Dec 2003
Location: Toronto, Canada
Distribution: Mint, Mandriva
Posts: 221

Rep: Reputation: 31
Quote:
Originally Posted by chrism01 View Post
The character sequence '#!' is colloquially called 'shebang',
e.g.
#!/bin/bash
or
#!/usr/bin/perl

If you are writing a shell script, it's usually advised (in prod systems at least) to specify (via the #! invocation) which shell is required to run this script properly.
Otherwise, what can happen is that eg you write it in bash, but the default shell on the target system turns out to be csh.

It is just as likely that bash may be in a different location on the target system; a shebang makes the script less portable.

If you are writing a script to be used on more than one system, it is safest to use POSIX compliant code that can be run by /bin/sh on most systems.
Quote:
Not all bash cmds are understood by the csh shell.

"Not all" is an understatement. A Bourne-type shell (such as bash) is a completely different language from csh; it's not just the commands, but the syntax is very different.
Quote:
Using the shebang line as described above means that you simplify the cmd line by not having to specify the actual shell by name externally to the script.

You do not have to specify the shell on the command line if there is no shebang. If the script is written for /bin/sh, that will be used to execute the script even if you call it from csh.
Quote:
Note that eg cron has a minimal/almost non-existent default shell env, which means you should always specify the complete path for a cmd eg /usr/bin/perl and not the short form eg perl.
 
Old 09-04-2007, 02:58 AM   #10
George2
Member
 
Registered: Oct 2003
Posts: 354

Original Poster
Rep: Reputation: 30
Thanks chrism01,


I want to confirm with you that there is no functonal differences between using sh to execute and execute the shell script directly, right?

In my shell script, I am using #!/bin/bash at the first line.


regards,
George

Quote:
Originally Posted by chrism01 View Post
The character sequence '#!' is colloquially called 'shebang',
e.g.
#!/bin/bash
or
#!/usr/bin/perl

If you are writing a shell script, it's usually advised (in prod systems at least) to specify (via the #! invocation) which shell is required to run this script properly.
Otherwise, what can happen is that eg you write it in bash, but the default shell on the target system turns out to be csh.
Not all bash cmds are understood by the csh shell.

Using the shebang line as described above means that you simplify the cmd line by not having to specify the actual shell by name externally to the script.
Note that eg cron has a minimal/almost non-existent default shell env, which means you should always specify the complete path for a cmd eg /usr/bin/perl and not the short form eg perl.
HTH
 
Old 09-04-2007, 03:19 AM   #11
George2
Member
 
Registered: Oct 2003
Posts: 354

Original Poster
Rep: Reputation: 30
Thanks man,


If I am using #!/bin/bash at the first line, there are any functional differences between executing the shell script directly and using sh to execute?


regards,
George

Quote:
Originally Posted by cfaj View Post

It is just as likely that bash may be in a different location on the target system; a shebang makes the script less portable.

If you are writing a script to be used on more than one system, it is safest to use POSIX compliant code that can be run by /bin/sh on most systems.

"Not all" is an understatement. A Bourne-type shell (such as bash) is a completely different language from csh; it's not just the commands, but the syntax is very different.

You do not have to specify the shell on the command line if there is no shebang. If the script is written for /bin/sh, that will be used to execute the script even if you call it from csh.
 
Old 09-04-2007, 06:00 AM   #12
samwise17
Member
 
Registered: Jul 2007
Location: Sydney
Distribution: Arch,Slackware,Puppy
Posts: 87

Rep: Reputation: 15
AFAIK, nope. But what cfaj says is new to me. I like the idea of putting stuff in /usr/local/bin but most of my stuff has a specific purpose and not really general use, so I keep them in the directory related to the project.
 
Old 09-04-2007, 11:39 AM   #13
cfaj
Member
 
Registered: Dec 2003
Location: Toronto, Canada
Distribution: Mint, Mandriva
Posts: 221

Rep: Reputation: 31
Quote:
Originally Posted by George2 View Post
If I am using #!/bin/bash at the first line, there are any functional differences between executing the shell script directly and using sh to execute?

If you are calling a script as the argument to a shell, the shebang makes no difference; it is just another comment. The shebang is only used when the script is executed as a command.
 
Old 09-04-2007, 01:12 PM   #14
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 735

Rep: Reputation: 76
Hi.

The portability of scripts is decreased if you specify an absolute path.

I once used Solaris often and ran into such problems going back and forth to and from Linux.

I use a perl utility, fixin (fix interpreter), that adjusts the shebang line of scripts for the correct path. It works not only for perl shebangs, but for anything you have in a shebang -- awk, sh, etc.

The fixin program was published in one of the first Programming perl books. An on-line copy of the code is http://search.cpan.org/src/JOHNL/DBD...mples/fixin.pl

Best wishes ... cheers, makyo

PS The origin of the word shebang can be found at http://en.wikipedia.org/wiki/Shebang_%28Unix%29
 
Old 09-04-2007, 08:45 PM   #15
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
The point of specifying the shell your code requires (on production systems) is so that you don't end up with a script that partially runs, then screws up/crashes because it's running under the wrong intepreter.
It's much safer for it not to run at all if it can't find the correct interpreter.
It can cost a company a lot of money if it goes wrong...
It also tells the sysadmin/maintenance prog which shell it will work with, rather than them having to guess.
You might not be avail to be asked when it goes pear-shaped ...
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Is there a single command to list all hardware installed (command line)? davee Linux - Hardware 6 02-28-2009 07:19 PM
Require Linux/Perl equivalent command for windows Command alix123 Programming 7 08-19-2005 02:23 AM
Redirecting output to a command-line argument of another command madiyaan Linux - Newbie 1 02-19-2005 04:35 PM
Key stroke/command to shut down x and go into the command prompt screen? Fear58 Linux - General 1 07-14-2004 07:14 PM
Where is Command line utility for Cups and command tutorial mossy Linux - Software 8 01-16-2004 12:24 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:14 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration