You can make a difference in the Apple Support Community!

When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

/bin/bash vs /bin/sh

Why are there two binaries for bash (a.k.a. sh)? Know the differences between bash and sh but bash is supposed to be mostly sh compatible when it is invoked as sh. So why are there two separate binaries of different sizes?


dkelly@Clumsy {116} ls -li `which sh`

7096160 -r-xr-xr-x 1 root wheel 1346624 Jun 24 2010 /bin/sh*

dkelly@Clumsy {117} ls -li /bin/bash

7096159 -rwxr-xr-x 1 root wheel 1346544 Jun 24 2010 /bin/bash*

dkelly@Clumsy {118} sh --version

GNU bash, version 3.2.48(1)-release (x86_64-apple-darwin10.0)

Copyright (C) 2007 Free Software Foundation, Inc.

dkelly@Clumsy {119} bash --version

GNU bash, version 3.2.48(1)-release (x86_64-apple-darwin10.0)

Copyright (C) 2007 Free Software Foundation, Inc.

dkelly@Clumsy {120} uname -a

Darwin Clumsy.local 10.8.0 Darwin Kernel Version 10.8.0: Tue Jun 7 16:33:36 PDT 2011; root:xnu-1504.15.3~1/RELEASE_I386 i386

Mac Pro, Mac OS X (10.6.8)

Posted on Jul 14, 2011 8:07 AM

Reply
10 replies

Jan 3, 2017 6:18 PM in response to Linc Davis

You are on to something. Now it's 2017, using OS X Sierra, and this situation persists. Apple stopped linking the binaries. sh is a slightly larger size (and they hash to different values). It's different all over Unix and Linux with sh sometimes linking to bash. But here it's different somehow and of course, sh should be the POSIX compliant binary.


This is interesting stuff. I notice OS X has drifted far from it's BSD roots.

Jul 14, 2011 8:27 AM in response to David Kelly1

There aren't two binaries - sh is an alias for bash in os x. note that your third and fourth commands (where you ask for version numbers on sh and bash) both return GNU bash, version 3.2.48.


P.s. well, not literally an alias. I think sh on os x is a small executable that runs bash with a limited command set. Just trying to be clear.

Jul 14, 2011 9:12 AM in response to twtwtw

I used "ls -li" to extract the inode number to document the files are not a hard link of one to the other. A hard link was the expected result.


I don't think alias could have produced this result. The following Stuck_01_alias.jpg was created with "ln -s", Stuck_01_hard.jpg with just "ln", and "Stuck_01.jpg alias" was created in Finder. There is a difference but its not the difference we see between /bin/sh and /bin/bash:


dkelly@Clumsy {130} ls -li Stuck_01*

7158733 -rw-r--r-- 2 dkelly dkelly 1061912 Nov 22 2010 Stuck_01.jpg

9659073 -rw-r--r--@ 1 dkelly dkelly 168084 Jul 14 10:52 Stuck_01.jpg alias

9659070 lrwxr-xr-x 1 dkelly dkelly 12 Jul 14 10:51 Stuck_01_alias.jpg@ -> Stuck_01.jpg

7158733 -rw-r--r-- 2 dkelly dkelly 1061912 Nov 22 2010 Stuck_01_hard.jpg


If bored out of your gourd the above is me getting a 4wd truck stuck parking new-to-me toy hauler in my own flat soggy front yard and having to be pulled out. When things dried out I was able to retrieve the trailer and later paved the whole area with concrete.

http://home.hiwaay.net/~dkelly/Stuck_01.jpg

http://home.hiwaay.net/~dkelly/Stuck_02.jpg

http://home.hiwaay.net/~dkelly/Stuck_03.jpg

http://home.hiwaay.net/~dkelly/Carport.jpg

Jul 14, 2011 9:22 AM in response to David Kelly1

well, as I said in my postscript (which you probably didn't notice, granted), I'm guessing the executable you're seeing is probably a small translation thing - it handles the few syntax differences between sh and bash and passes the translated result on to bash. You'd need something like this for (say) handling some statement that's valid in bash but ought to throw an error in sh, so that someone working in sh gets the result they expect.


but that's just a guess.


re the pictures, I only have two comments:

  • Concrete is not a valid substitute for common sense.
  • You really need to hold a garage sale.

😝

Jul 14, 2011 11:43 AM in response to David Kelly1

/bin/sh is suppose to be the POSIX shell, and follow explicit POSIX specificied behavior.


/bin/bash while is very close to POSIX behavior, bash does things slightly different, and bash adds new features or even changes features as the bash dev team desires.


Yes the same sources are used to build both, but the code looks at argv[0] (the name used to invoke the shell), and if it is 'sh' then bash follows POSIX rules. If the name is 'bash' it follows bash rules.


As has been suggested, it is possible to use the exact same executable and use either a Unix hardlink or a Unix symlink to the executable as a way to provide both 'sh' and 'bash' names using the same executable.

Then again, it is also possible to build the code explicitly for 'sh' and 'bash' creating separate executables. This is most likely what Mac OS X is currently doing. Then again, over the years I have seen a lot of new kids put in charge of build environments (NOT Apple's, but other companies), where these kids did not understand Unix hardlinks, symlinks, and the use of argv[0] as a way to tell the difference, so they change the build/install code which breaks the links. And it has not always been kids. My most recent pupil was nearing retirement :-)

/bin/bash vs /bin/sh

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.