Difference between "./ "and "sh" and the default SHELL | Extra shell features | Ubuntu Dash

During the last days I wrote some shell scripts to keep my development environments in sync. When it came to arrays I experienced some weird problems.

fl0@fl0-m0thership:~/AutoPkgUpdater$ sh MakePlist.sh f00
MakePlist.sh: 25: Syntax error: "(" unexpected

Guess what's at line 25? Right, an array declaration...The weird thing: I was able to directly declare an array in the shell like the following:

fl0@fl0-m0thership:~/AutoPkgUpdater$ a=( one two three)

fl0@fl0-m0thership:~/AutoPkgUpdater$ echo ${a[1]}
two

To make things even more complex, running the script like "./MakePlist.sh" worked, NO Syntax error. Ok - here's the trick:

"...Running it as ./script.sh will make the kernel read the first line (the shebang), and then invoke bash to interpret the script. Running it as sh script.sh uses whatever shell your system defaults sh to (on Ubuntu this is Dash, which is sh-compatible, but doesn't support some of the extra features of Bash)..." [StackOverflow]

So you may change your "/bin/sh" to bash?

cd /bin
sudo rm sh
sudo ln -s /bin/bash sh

To make dash code "/bin/bash" compatible you may read the following:

https://wiki.ubuntu.com/DashAsBinSh

Comments

The "proper" way...

You could also just "dpkg-reconfigure dash" to change the default /bin/sh, would be the "proper way" to do it. Keep in mind that your boot time might go up, dash being faster for running the init scripts was one of the reasons debian/ubuntu chose it in the first place.
And if your script needs bash-specifics, just friggin run it with bash, not with sh. Same goes for the shebang. Expecting bash-behavior when invoking /bin/sh is never a safe bet. If all script authors would know the difference between bash and sh, which shell is installed as /bin/sh wouldn't really matter. Ah, i should stop ranting... ;)
-David (who's missing a box to put his name here?!)

Agreed!

Yeah right - I like to add you comment to the post...Would that be okay for you?

1337!!111!

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code>
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.