2

I'm trying to use some script variables in a Bash script to make my code clearer.

However, for some reason, my variables are not being expanded/used.

Nothing happens when I use the variable with the full command spelled out

This is the script I made:

command1=$SOMEPATH/abc.sh
command2=$SOMEPATH/def.sh input
command3=$SOMEPATH/ghi

 gnome-terminal                                                                   \
 --tab -t "Server 1"  -e 'bash -c "export BASH_POST_RC=\"$command1\"; exec bash"'  \
 --tab -t "Server 2"  -e 'bash -c "export BASH_POST_RC=\"$command2";  exec bash"' \
 --tab -t "Server 3"  -e 'bash -c "export BASH_POST_RC=\"$command3";  exec bash"' 
1
  • Single quotes prevent expansion of variables. Use double quotes to save blanks: command2="$SOMEPATH/def.sh input"
    – Cyrus
    Commented Dec 31, 2014 at 17:18

2 Answers 2

2

Declare your variables in double quotes

command1="$SOMEPATH/abc.sh"
command2="$SOMEPATH/def.sh input"
command3="$SOMEPATH/ghi"
0

Add export command1 command2 command3 between variable assignments and gnome-terminal command. Alternatively (and better) prefix export to all assignments:

command1="$SOMEPATH/abc.sh"
command2="$SOMEPATH/def.sh input"
command3="$SOMEPATH/ghi"

 gnome-terminal                                                                   \
 --tab -t "Server 1"  -e 'bash -c "export BASH_POST_RC=\"$command1\"; exec bash"'  \
 --tab -t "Server 2"  -e 'bash -c "export BASH_POST_RC=\"$command2";  exec bash"' \
 --tab -t "Server 3"  -e 'bash -c "export BASH_POST_RC=\"$command3";  exec bash"'  

This is because you using single quote in -e options, so variables will be used not in your script, but in bash you are running in tabs.

PS: Note that quoting still needed.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .