Skip to main content
Became Hot Network Question
Use title which explains issue better so that others can find this question more easily; other minor fixes
Source Link
Anthony Geoghegan
  • 13.6k
  • 7
  • 62
  • 66

wildcards Wildcards not interpreted in bash when running a script from aby shell script when used with output redirection

saySay I have two scripts  :

  • script1.sh  :

     #!/bin/bash
     version=2.6
     log_file="$HOME/log-file-version-$version.log"
    
     touch $log_file &>/dev/null
    
     echo "log to my log file from script 1" >> $HOME/log-file-version-?.?.log
    
     gnome-terminal --tab --active --title="script2" --  sh script2.sh
    
  • script2.sh  :

     #!/bin/bash
    
     echo "log to my log file from script 2" >> $HOME/log-file-version-?.?.log
    

I run script1.shscript1.sh.

I'm left with two log files in my home  :

log-file-version-2.6.log
log-file-version-?.?.log

log-file-version-2.6.loglog-file-version-2.6.log contains  :

log to my log file from script 1

log-file-version-?.?.loglog-file-version-?.?.log contains  :

log to my log file from script 2

which means that in script 1 the wildcards from line 7 (>> $HOME/log-file-version-?.?.log) were correctly interpreted, but when a script is run with sh these wildcards don't work.

whyWhy is that and how can I fix this?

I need to use wildcards because I don't want to be passing arguments from shell script to shell script and I want them to be self-sufficient.

I'm using ubuntuUbuntu and running these scripts from the default terminal which is gnome-terminal.

wildcards not interpreted in bash when running a script from a script

say I have two scripts  :

  • script1.sh  :

     #!/bin/bash
     version=2.6
     log_file="$HOME/log-file-version-$version.log"
    
     touch $log_file &>/dev/null
    
     echo "log to my log file from script 1" >> $HOME/log-file-version-?.?.log
    
     gnome-terminal --tab --active --title="script2" --  sh script2.sh
    
  • script2.sh  :

     #!/bin/bash
    
     echo "log to my log file from script 2" >> $HOME/log-file-version-?.?.log
    

I run script1.sh.

I'm left with two log files in my home  :

log-file-version-2.6.log
log-file-version-?.?.log

log-file-version-2.6.log contains  :

log to my log file from script 1

log-file-version-?.?.log contains  :

log to my log file from script 2

which means that in script 1 the wildcards from line 7 (>> $HOME/log-file-version-?.?.log) were correctly interpreted, but when a script is run with sh these wildcards don't work.

why is that and how can I fix this?

I need to use wildcards because I don't want to be passing arguments from shell script to shell script and I want them to be self-sufficient.

I'm using ubuntu and running these scripts from the default terminal which is gnome-terminal.

Wildcards not interpreted by shell script when used with output redirection

Say I have two scripts:

  • script1.sh:

     #!/bin/bash
     version=2.6
     log_file="$HOME/log-file-version-$version.log"
    
     touch $log_file &>/dev/null
    
     echo "log to my log file from script 1" >> $HOME/log-file-version-?.?.log
    
     gnome-terminal --tab --active --title="script2" --  sh script2.sh
    
  • script2.sh:

     #!/bin/bash
    
     echo "log to my log file from script 2" >> $HOME/log-file-version-?.?.log
    

I run script1.sh.

I'm left with two log files in my home:

log-file-version-2.6.log
log-file-version-?.?.log

log-file-version-2.6.log contains:

log to my log file from script 1

log-file-version-?.?.log contains:

log to my log file from script 2

which means that in script 1 the wildcards from line 7 (>> $HOME/log-file-version-?.?.log) were correctly interpreted, but when a script is run with sh these wildcards don't work.

Why is that and how can I fix this?

I need to use wildcards because I don't want to be passing arguments from shell script to shell script and I want them to be self-sufficient.

I'm using Ubuntu and running these scripts from the default terminal which is gnome-terminal.

Source Link
tatsu
  • 306
  • 4
  • 17

wildcards not interpreted in bash when running a script from a script

say I have two scripts :

  • script1.sh :

     #!/bin/bash
     version=2.6
     log_file="$HOME/log-file-version-$version.log"
    
     touch $log_file &>/dev/null
    
     echo "log to my log file from script 1" >> $HOME/log-file-version-?.?.log
    
     gnome-terminal --tab --active --title="script2" --  sh script2.sh
    
  • script2.sh :

     #!/bin/bash
    
     echo "log to my log file from script 2" >> $HOME/log-file-version-?.?.log
    

I run script1.sh.

I'm left with two log files in my home :

log-file-version-2.6.log
log-file-version-?.?.log

log-file-version-2.6.log contains :

log to my log file from script 1

log-file-version-?.?.log contains :

log to my log file from script 2

which means that in script 1 the wildcards from line 7 (>> $HOME/log-file-version-?.?.log) were correctly interpreted, but when a script is run with sh these wildcards don't work.

why is that and how can I fix this?

I need to use wildcards because I don't want to be passing arguments from shell script to shell script and I want them to be self-sufficient.

I'm using ubuntu and running these scripts from the default terminal which is gnome-terminal.