0

Problem

I have two bash files: run.sh and run_1.sh. When I try to call run_1.sh from run.sh the loop doesnt work anymore as you can see in the output. I really don't understand what's happening. run_1.sh is located in sub-directory /runs/.


run.sh

1        #!/bin/bash
2        
3        for i in {1..10}
4        do
5         echo $i
6        done
7    
8    sh /runs/run_1.sh

runs/run_1.sh"

  #!/bin/bash
        
        for i in {11..20}
        do
         echo $i
        done

output

1
2
3
4
5
6
7
8
9
10
{11..20}
Community
  • 1
  • 1
Hani Goc
  • 2,371
  • 5
  • 45
  • 89
  • 2
    Instead of `sh /runs/run_1.sh` use `bash /runs/run_1.sh` – anubhava Oct 14 '15 at 15:35
  • @anubhava oh lol thank you. what is the difference. Can you put it as the answer please – Hani Goc Oct 14 '15 at 15:36
  • 1
    The difference is that `sh` is POSIX sh, and it doesn't support bash-only features. – Charles Duffy Oct 14 '15 at 15:38
  • BTW, this is right there in the bash tag info page (item #2 in "before asking about problematic code") at http://stackoverflow.com/tags/bash/info. This is a very, very, **very** duplicate question. – Charles Duffy Oct 14 '15 at 15:39
  • [More](http://unix.stackexchange.com/questions/3320/what-are-the-fundamental-differences-between-the-mainstream-nix-shells) than you probably will ever need at a unix quiz night on shells. – Shawn Mehan Oct 14 '15 at 15:42
  • BTW, the loop doesn't stop working -- it's the expansion that doesn't work; the behavior of `for` doesn't change in any immediately relevant way. You can test this by comparing `echo {{1..5}}` between both shells, no `for` loop required to demonstrate the difference. – Charles Duffy Oct 14 '15 at 15:43

1 Answers1

2

Instead of this line:

sh /runs/run_1.sh

use bash to execute other script

bash /runs/run_1.sh

Plain old bourne shell sh doesn't expand {11..20} which is a bash feature.

anubhava
  • 761,203
  • 64
  • 569
  • 643