Shell script to scroll through wildcard directories

I am trying to iterate over directories matching a pattern. This works great on the command line. But does not work in Shell script. Any idea?

for dirs in /var/www/html/my.domain.com/v*; do echo $dirs; done; 

The above commands list

  /var/www/html/my.domain.com/v1.0 /var/www/html/my.domain.com/v1.2 /var/www/html/my.domain.com/v2.0 

Here is the shell script version. Does not work:

  dirs=/var/www/html/my.domain.com/v* for dir in $dirs do echo "$dir" done 

Tried this too:

 dirs=`/var/www/html/my.domain.com/v*` #back quotes for dir in $dirs do echo "$dir" done 
+4
source share
1 answer

Glob templates , like /var/www/html/my.domain.com/v* , are bash . It seems that bash is not the default shell on your system when you run the script using sh script.sh .

Make sure you run the script using bash:

 bash script.sh 

If you intend to make the script executable directly using chmod +x script.sh , make sure you are missing the use of shebang in the first line:

 #!/bin/bash 
+3
source

All Articles