Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I have the following simple script:

#!/bin/bash
echo "Bash version ${BASH_VERSION}..."
for i in {1..99..2}
do
        echo $i
done

Output in case I run with sh file.sh:

Bash version ...
{1..99..2}

Output in case I run with bash file.sh:

Bash version 4.2.25(1)-release...
1
3
5
.
.
.
99

I have two queries:

  1. If I have written a shabang line specifying shell, shouldn't it run with bash whether I use sh file.sh or bash file.sh?

  2. I understand that $BASH_VERSION is not recognized by sh but what is the problem with for loop? Why isn't printing the numbers?

share|improve this question

2 Answers 2

up vote 14 down vote accepted

sh, the Bourne shell, is old. It's behaviour is specified by the POSIX standard. If you want new behaviour, you use bash, which gets new features added to it all the time. On many systems, sh is just bash, and bash turns on a compatibility mode when run under that name. On Ubuntu, sh is dash.

The brace expansion is a relatively new feature, and would not be available in older bashes as well. It certainly shouldn't be in sh.

The shebang line is only respected if you call the script as an executable:

./myscript.sh

I can give a script any shebang and still have it open in Python by running:

python myscript.sh

See this excellent question on Unix & Linux: Does the shebang determine the shell which runs the script?

share|improve this answer

SH is stupid. It can not do anything.

It only knows the most basic commands. It does not do anything else.

It can not understand {1..99} as a numeric value. It understands it as a string.

SH/Dash is not developed. It is meant to be an emergency prompt for DIRE circumstances (in interactive mode. Otherwise, it's a really good thing for scripting.).

Bash is recommended as it can do everything SH can, as well as the newest in customization and powerful features.

In answer to #1, the #! is only parsed if the program is called directly. (as in, ./program.sh)

share|improve this answer
5  
"It is meant to be an emergency prompt for DIRE circumstances." dash, which as you say provides sh in Ubuntu, isn't present mainly "to be an emergency prompt for DIRE circumstances" but instead so the system can run, and start, fast. In this way, dash is well suited to use in a modern OS, but what it's well suited for is as a low-profile, fast shell for scripting--particularly, running system scripts while booting a big, complex, modern OS, with minimal performance overhead. –  Eliah Kagan Aug 30 at 7:51
    
@EliahKagan You really only use SH if something when terribly wrong. –  Whaaaaaat Aug 30 at 7:52
    
You use sh pretty constantly, and particularly intensively as Ubuntu boots. It's mainly important for its automated scripting uses, and that is a very important use case in Ubuntu. (If you were to remove the /bin/sh symlink--so that, in effect, there would be "no SH"--things would go terribly wrong: Ubuntu could not boot up into a usable state.) –  Eliah Kagan Aug 30 at 7:54
    
@EliahKagan When did you last use SH as your preferred terminal? I know you use it to script, but when do you actually use it in interactive mode? –  Whaaaaaat Aug 30 at 7:56
1  
Yesterday, to check how portable a command was. But your point that interactive use of dash is rare is valid. (And anyway, the context of my interactive use was still scripting-oriented.) ....But "use" does not mean "interactive use," and more importantly, the use case in this question is not an interactive one! –  Eliah Kagan Aug 30 at 7:59

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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