0

I am using a remot server running Ubuntu 13.10 with Bash 4.2.45 installed. My home system is OS X Mavericks with Bash 3.2.51 (Darwin build) installed. I haven't used bash much in the past but I've been working on a pretty big script and noticed a few things that were weird about the version installed on the remote server.

Take a simple script for example:

#!/bin/sh

read n

if ((n > 10)); then
        echo "Number is pretty big"
else
        echo "NUMBER IS WEAK AND SMALL"
fi

While under bash 3.2.51 it recognizes the expression and works under the bash 4 on Ubuntu it throws a weird error:

script.sh: 5: script.sh: n: not found

But not only are (( . . . )) not recognized but sometimes variables and other logical statements. As a bash beginner it's very confusing and I wanted to ask what I could do about that. Can I change the syntax rules? Or just downgrade the remote server to a 3.x.y version? Any help is appreciated.

4

When i've read your query i was surprised by the behaviour you have, so i've decided to reproduce it quickly, so i've setup a new Ubuntu 13.10 VM with Bash version 4.2.45.

Once done, i was able to reproduce the behaviour you describe.

After further investigations, it seems that you just have to replace #!/bin/sh by #!/bin/bash to make it works.

Edit :

To launch the script :

  • bash script.sh and ./script.sh will work.
  • sh script.sh will not work.

Assuming you have #!/bin/bash at first line

Hope it will work for you !

  • Ah, I tried it with #!/bin/sh and #!/bin/bash. And on the remote desktop with 4.2.45 it both times throws the same errors. Are you sure that you're not changing anything else? – AreusAstarte Jan 2 '14 at 22:58
  • I have an OSX near me, will check why it works on it ! – user2196728 Jan 2 '14 at 23:21
  • 2
    Like user2196728, I simply changed #!/bin/sh to #!/bin/bash, and the problem went away. sh is implemented as dash on Ubuntu and as sh on OSX - try doing a man sh on Ubuntu and on OSX. On OSX, the man pages for bash and sh are combined, and the object file sizes of bash and sh on OSX are almost identical. So, that leads me to believe that sh on Ubuntu has a different lineage than bash, especially since the sizes on Ubuntu are over 10% different. I think proper sh scripts will run in bash, but not the other way around. – Billy McCloskey Jan 3 '14 at 2:54
2

You aren't using the Bourne Again Shell at all.

It's a mistake to assume that /bin/sh is anything more than a POSIX-conformant shell. (Even if it were bash, when bash is invoked as sh it subtly changes its behaviour.) On Ubuntu, /bin/sh now defaults to the Debian Almquist Shell, which is a different shell — one of the many shells (Thompson, Bourne, Bourne Again, Almquist, Korn, Z, Friendly Interactive, C, Tenex C, BusyBox, Policy-compliant Ordinary, …) that one can find on Unix and Linux systems. The switch of /bin/sh from Bourne Again to Debian Almquist improved the boot speed of Debian and Ubuntu systems, which execute a lot of shell scripts as part of the boot process.

If you really want to make use of bashisms like (()) in your script, then explicitly set /bin/bash as the script interpreter in its #! line.

Conversely, if you want to specify /bin/sh as your script interpreter across systems as diverse as Mac OS and Linux then stick strictly to only that which is POSIX-conformant.

Further reading

  • "As a bash beginner" Honestly no idea what you are trying to tell me…except yes, I should put bash in the header. Which I actually did on my linux script. But as somebody else pointed out, I need to run a bash script with "bash" (which I didn't know). – AreusAstarte Jan 2 '14 at 23:14
0

n is a variable, so you need to reference it using a "$":

#!/bin/sh

read n

if (($n > 10)); then
        echo "Number is pretty big"
else
        echo "NUMBER IS WEAK AND SMALL"
fi
  • Althought yes that is true, it doesn't solve the problem. Well...it script.sh: 5: script.sh: 5: not found – AreusAstarte Jan 2 '14 at 23:09
  • Really? That works for me: p.6core.net/p/T0NVAIEcfAxVKcstThwCwD59 – Teun Vink Jan 2 '14 at 23:12
  • Right the issue isn't the 3.x darwin build. That's where everything works. It's the 4.x build on ubuntu that doesn't work – AreusAstarte Jan 2 '14 at 23:19
  • Right, with Bash 4.2.45 neither n nor $n was working with the initial given script – user2196728 Jan 2 '14 at 23:24
  • As was pointed out in more than one answer now, #!/bin/sh on Ubuntu is not bash. (( )) is bash specific, so replace your hashbang: p.6core.net/p/k02MtdUYABC9pbXKjerrYjmi and it will work. – Teun Vink Jan 2 '14 at 23:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.