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
- fl0's blog
- 1311 reads
Recent blog posts
- Convert mov, mp4, avi, flv to ogv | ffmpeg2theora
- Stop Disabling SELinux! | Drupal and SELinux, Drupal doesn't send mails [SOLVED]
- Blender 2.59 + iCub SIM + Kinect = Movement Imitation
- Blender 2.59 + Kinect = Controlling Robot Arms in Game Engine
- Virtual (robot) Flobi - Motion Capture Playback Demo
- LimeSurvey | Default Password Change | Redirect to login page [SOLVED]
- Lighttpd | Can't have more connections than fds/2: 1024
- Warp1337 Theme for you!
- OpenNI Java Wrapper Test | Ubuntu 11.10
- Web history with Screenshots and stuff | Web archive
What is Flattr ? Flattr is the worlds first social micro-payment system The idea had already been initiated in 2007, but the first release was in 2010 due to typical geeky laziness. Flattr was founded to help people share money, not just content. https://flattr.com/about

















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