LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-15-2013, 06:37 AM   #1
charithsrng
LQ Newbie
 
Registered: Jan 2013
Posts: 26

Rep: Reputation: Disabled
Functionality differences between Bash and sh


Hi All,

Need some explanation on Functionality differences between Bash and sh(better be with examples)

Thanks
 
Old 01-15-2013, 06:50 AM   #2
towheedm
Member
 
Registered: Sep 2011
Location: Trinidad & Tobago
Distribution: Debian Jessie
Posts: 592

Rep: Reputation: 119Reputation: 119
BASH, csh, zsh, DASH, etc are all different shells. sh is a symlink to your preferred shell. Have a look ay /bin (in my case):
Code:
ls -l /bin
-rwxr-xr-x 1 root root 811156 Apr 10  2010 bash
.
.
.
lrwxrwxrwx 1 root root      9 Jun  4  2012 sh -> /bin/bash
If you have a shell script that starts:
Code:
#! /bin/sh
then whatever shell is symlinked to /bin/sh is used to run the script.

However, if you have a shell script that starts:
Code:
#! /bin/bash
then the BASH shell is used to run the script. Simalarly:
Code:
#! /bin/csh
then the csh is used to run the script.

Hope it starts you off.
 
1 members found this post helpful.
Old 01-15-2013, 07:09 AM   #3
wigry
Member
 
Registered: Jul 2004
Distribution: slackware
Posts: 222

Rep: Reputation: 52
A little excerpt from the Bash manual page:

Code:
If  bash  is  invoked  with  the name sh, it tries to mimic the startup
behavior of historical versions of sh as  closely  as  possible,  while
conforming  to the POSIX standard as well.
 
Old 01-17-2013, 01:55 AM   #4
charithsrng
LQ Newbie
 
Registered: Jan 2013
Posts: 26

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by towheedm View Post
BASH, csh, zsh, DASH, etc are all different shells. sh is a symlink to your preferred shell. Have a look ay /bin (in my case):
Code:
ls -l /bin
-rwxr-xr-x 1 root root 811156 Apr 10  2010 bash
.
.
.
lrwxrwxrwx 1 root root      9 Jun  4  2012 sh -> /bin/bash
If you have a shell script that starts:
Code:
#! /bin/sh
then whatever shell is symlinked to /bin/sh is used to run the script.

However, if you have a shell script that starts:
Code:
#! /bin/bash
then the BASH shell is used to run the script. Simalarly:
Code:
#! /bin/csh
then the csh is used to run the script.

Hope it starts you off.
one Question-
if we execute a script which begins with /bin/sh in a bash shell what will be the effect?
 
Old 01-17-2013, 01:58 AM   #5
wigry
Member
 
Registered: Jul 2004
Distribution: slackware
Posts: 222

Rep: Reputation: 52
Quote:
Originally Posted by charithsrng View Post
one Question-
if we execute a script which begins with /bin/sh in a bash shell what will be the effect?
Actually that is the correct way of executing ANY script. /bin/sh will provide so called expected behavior. If you run script as /bin/bash, that means your script is using Bash-specific features and might not be portable.
 
Old 01-17-2013, 05:02 AM   #6
charithsrng
LQ Newbie
 
Registered: Jan 2013
Posts: 26

Original Poster
Rep: Reputation: Disabled
Smile

Quote:
Originally Posted by wigry View Post
actually that is the correct way of executing any script. /bin/sh will provide so called expected behavior. If you run script as /bin/bash, that means your script is using bash-specific features and might not be portable.
cleared..thanks a lot
 
Old 01-17-2013, 06:50 AM   #7
TheBigMing
Member
 
Registered: Dec 2008
Location: east anglia
Distribution: SuSE, antiX
Posts: 38

Rep: Reputation: 8
This is not entirely germane to this post but I have a post here:

http://www.linuxquestions.org/questi...1/#post4871241

(which I'm inclined to think might have been better on the 'Linux-General' forum). It's a sort of "I understand that I can do this & this but I know that doesn't work - why not?" question. I have only ever used bash. Might, I wonder, some other shell give different results.

ming
 
Old 01-17-2013, 07:05 AM   #8
towheedm
Member
 
Registered: Sep 2011
Location: Trinidad & Tobago
Distribution: Debian Jessie
Posts: 592

Rep: Reputation: 119Reputation: 119
The shebang line merely tells the shell which interpreter to use to run the script. So even from BASH:
Code:
#! /bin/csh
will use the csh as the command interpreter.
Similarly:
Code:
#! /bin/sh
will tell the shell to use whatever command interpreter is symlinked to /bin/sh.
And even:
Code:
#! /usr/bin/perl
will tell the shell to use PERL as the command interpreter.

Note also that you can completely leave out the shebang line. When you do this, you can run the script with:
Code:
sh myscript
to use the command interpreter symlinked to /bin/sh, or
Code:
bash myscript
to use the BASH command interpreter to run the script.
Note also that if you leave out the shebang line, you do not need to set the executable bit on the script.

Last edited by towheedm; 01-17-2013 at 07:09 AM.
 
1 members found this post helpful.
Old 01-17-2013, 05:52 PM   #9
TheBigMing
Member
 
Registered: Dec 2008
Location: east anglia
Distribution: SuSE, antiX
Posts: 38

Rep: Reputation: 8
@towheedm - Thanks for that. I'l play around with it a bit.
 
Old 01-18-2013, 10:19 AM   #10
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959
On most Linux systems /bin/sh is symlinked to either bash or dash, but that doesn't matter. When you run a script as sh, then it will be parsed according to POSIX-defined specifications (with a few legacy bourne-compatible modifications also thrown in), no matter what the actual shell doing the parsing is.

(The POSIX standard defines everything necessary for a minimally compatible unix-like operating system, and one of its parts is a basic shell that supports certain commands and syntax. So every POSIX OS provides some form of /bin/sh, even if the actual shell is bash or ksh, or whatever.)

What this means for any particular script depends on what POSIX says, and what the actual shell is.

If the code being parsed is explicitly defined by POSIX, then it will be treated as such. syntax or options that are incompatible with POSIX will be rejected with errors.

If the code in question is undefined by POSIX, that usually means that it's left up to the individual shell implementation to decide what to do. In practice what this means is that if the shell doing the parsing understands the syntax, then it will usually continue to work. So most bash constructs will continue to work if the reading shell is bash, or possibly ksh.

For example, if you try to use an array (an undefined feature) in your sh script, and the parser is bash or ksh, then it will continue to work, at least according to that shell's understanding. But if the parser is dash, it will not, because dash does not support arrays at all.

So it's very important, if true portability is required, that you stick with the POSIX syntax; only that is guaranteed to always run*. The dash shell in particular is one that follows POSIX very carefully, and does not have many extensions, if any. so if you want to know if your script will run as sh, try using #!/bin/dash as your shebang.

This page has a good rundown of bash syntax constructs and their POSIX equivalents:
http://mywiki.wooledge.org/Bashism



*Of course even then you still have to deal with differences between platforms and the commands they have available. But that's a different question.
 
Old 01-21-2013, 01:39 AM   #11
charithsrng
LQ Newbie
 
Registered: Jan 2013
Posts: 26

Original Poster
Rep: Reputation: Disabled
Thumbs up

Quote:
Originally Posted by David the H. View Post
On most Linux systems /bin/sh is symlinked to either bash or dash, but that doesn't matter. When you run a script as sh, then it will be parsed according to POSIX-defined specifications (with a few legacy bourne-compatible modifications also thrown in), no matter what the actual shell doing the parsing is.

(The POSIX standard defines everything necessary for a minimally compatible unix-like operating system, and one of its parts is a basic shell that supports certain commands and syntax. So every POSIX OS provides some form of /bin/sh, even if the actual shell is bash or ksh, or whatever.)

What this means for any particular script depends on what POSIX says, and what the actual shell is.

If the code being parsed is explicitly defined by POSIX, then it will be treated as such. syntax or options that are incompatible with POSIX will be rejected with errors.

If the code in question is undefined by POSIX, that usually means that it's left up to the individual shell implementation to decide what to do. In practice what this means is that if the shell doing the parsing understands the syntax, then it will usually continue to work. So most bash constructs will continue to work if the reading shell is bash, or possibly ksh.

For example, if you try to use an array (an undefined feature) in your sh script, and the parser is bash or ksh, then it will continue to work, at least according to that shell's understanding. But if the parser is dash, it will not, because dash does not support arrays at all.

So it's very important, if true portability is required, that you stick with the POSIX syntax; only that is guaranteed to always run*. The dash shell in particular is one that follows POSIX very carefully, and does not have many extensions, if any. so if you want to know if your script will run as sh, try using #!/bin/dash as your shebang.

This page has a good rundown of bash syntax constructs and their POSIX equivalents:
http://mywiki.wooledge.org/Bashism



*Of course even then you still have to deal with differences between platforms and the commands they have available. But that's a different question.
Thanks for the heads up.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
BASH Shell Differences: Login or Interactive theKbStockpiler Programming 10 02-21-2011 01:00 AM
LQ member's opinions about Zsh compared to Bash-Differences? linus72 Linux - Newbie 1 05-07-2009 08:09 PM
Script to find file differences in two directory trees (bash) Syqers Linux - Newbie 3 10-01-2008 11:55 PM
How to get differences between two files using bash script jfarhat Programming 5 09-09-2008 05:05 PM
bash script to output only differences in two files czarherr Programming 4 11-08-2004 06:25 PM


All times are GMT -5. The time now is 03:58 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration