Unix & Linux
bash shell-script shell gnome-terminal gnome-shell
Updated Fri, 22 Jul 2022 13:58:26 GMT

How to pass variables to gnome-terminal command

I am trying to use variables to construct the --command arg for gnome-terminal.

My shell script look like this:

gnome-terminal -e 'sh -c "while true; do
        python acra.py $versionCode $buildId 0 $daily
        sleep 600 # 10 mins

But when I run this script, new terminal open but it cannot recognize these variables, I'm only receive sys.argv = ['acra.py', '0'] inside my python script, so I guess the cmd was executed just like:

python acra.py 0

So how can I use variable in this case?


Assuming that gnome-terminal behaves like xterm:

gnome-terminal -e sh -c 'some commands here' sh "$variable1" "$variable2" "etc."

The strings at the end of the command line will be available inside the sh -c script as $1, $2, $3, etc. The first argument to the script, the string sh, will be placed in $0 and used in error messages by the shell.

In your case:

gnome-terminal -e sh -c '
    while true; do
        python acra.py "$1" "$2" 0 "$3"
        sleep 600
    done' sh "$versionCode" "$buildId" "$daily"

This assumes that the acra.py script is available in the current working directory.