I.T. Security and Linux Administration

Sep 7 2011   4:31PM GMT

Shell Scripting tip



Posted by: Eric Hansen
Tags:
Bash
bash scripting

While for the most part you may never run into an issue when writing (and using) shell scripts, there’s one little caveat that I’ve experienced as of late.

Instead of starting your scripts with this:

#!/bin/sh

You will most likely better off in the long run specifying the exact shell you want to use.  For example, if you want to use Bash:

#!/bin/bash

The problem here is that /bin/sh is generally a symlink to a shell program (Bash, Dash, etc…).  While this is handy, not every shell is going to have the same features.  For example, in Bash, if you want to create an array variable, you’d do this:

VARNAME=(arrayval1 arrayval2 etc…)

However, on my VPS, /bin/sh is linked to /bin/dash, which doesn’t allow you to create arrays like that.  If you want to to see where your /bin/sh is linked to, you can run the following command:

ls -liha /bin | grep “sh ->” | grep lrwxrwxrwx

There might be multiple entries.  You could remove it then recreate the symlink, but if you’re going to write scripts for portability (like what I offer on this blog), you should specify the exact shell to be on the safe side.

 Comment on this Post

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to: