0

The following 'oneliner' does what I need:

$ for i in {1..5}; do echo $i; done
1
2
3
4
5

However, when I place exactly the same code into for-each.sh file and execute it, I get different result. Why?

for-each.sh file:

#!/bin/bash
for i in {1..10}; do echo $i; done

Result after execution:

$ ./for-each.sh
{1..10}

EDIT

Uf. I'm sorry. Now I noticed that I executed the for-each.sh by sh ./for-each.sh command and not by ./for-each.sh. I didn't know the difference between bash, sh, dash, ... After reading stackoverflow.com/a/5725402/915756 I realized that I executed the file by dash which points to /bin/sh by default on my Debian machine.

Community
  • 1
  • 1
Michal Vician
  • 2,296
  • 2
  • 21
  • 46
  • What's your default bash path? Can you check if `which bash` returns `/bin/bash` as well? – fedorqui 'SO stop harming' Jan 22 '14 at 16:15
  • `which bash` returned `/bin/bash` – Michal Vician Jan 22 '14 at 16:27
  • The example above works for me, btw. It doesn't work when I use `sh ./for-each.sh` – Aaron Digulla Jan 22 '14 at 16:31
  • Maybe you have different bash version. Mine is `GNU bash, version 4.2.25(1)-release (x86_64-pc-linux-gnu)` – Michal Vician Jan 22 '14 at 16:34
  • That's why I was asking if the bash you are using is normal. It is funny that in your command line (bash) it works but not in a script called with bash. The braces are bash, so... I don't know. Of course `seq` makes it, but feel curious about why it is not working under the script. – fedorqui 'SO stop harming' Jan 22 '14 at 16:41
  • How about trying: /bin/bash -xv ./for-each.sh – Mark Setchell Jan 22 '14 at 16:47
  • Are you sure the `#!/bin/bash` line is correct? Did you copy-and-paste the script into your question? A typo on that line might cause the script to be executed by `/bin/sh` rather than `/bin/bash`. What happens when you type `/bin/bash ./for-each.sh`? – Keith Thompson Jan 22 '14 at 17:07
  • 1
    @AaronDigulla: This is not a duplicate of that question, which asks about using a *variable* in brace expansion. This question just uses constants: `{1..10}`. Other close voters: How is this not about programming? – Keith Thompson Jan 22 '14 at 17:07
  • @KeithThompson you are completely right, I didn't understand the close vote as well. See the updated question, because it was exactly this, executing with `sh` instead of `bash`. – fedorqui 'SO stop harming' Jan 22 '14 at 17:14
  • 2
    It's not *really* a duplicate of [this question](http://stackoverflow.com/questions/5725296/difference-between-sh-and-bash) either. The real problem is that when you type `sh ./for-each.sh`, the `#!` line is quietly ignored. The purpose of the [shebang](http://en.m.wikipedia.org/wiki/Shebang_(Unix)) is to let you execute the script directly: `./for-each.sh` -- or, better, `./for-each.bash` -- or, still better, `./for-each` (why should the user care what language it's written in)? In any case, the suffix of the file name has no effect (unless you're on Windows). – Keith Thompson Jan 22 '14 at 17:22

1 Answers1

4

If you're confident that it is indeed bash executing your script, you can explicitly turn on brace expansion (expansion of {...} expressions) as follows:

set -B   # same as: set -o braceexpand

Make this your script's first command after your shebang. (Conversely, set +B (set +o braceexpand) would turn brace extension OFF.)

Conceivably, your system is configured to have brace extension turned off by default.

mklement0
  • 245,023
  • 45
  • 419
  • 492