0

i have a script to launch gnome terminal with multiple tabs. The tabs are opened based on a list of directories listed in .tabs file that are in the same dir as the script.

I create a string with several --tabs --working-directory /some/dir one for each line in the tabs file.

before i run the command i echo it to the console. then i actually run it. Problem is gnome does open the tabs but it doesn't open each one in it's working directory. However if i copy-paste what i printed to the console it works great. i can't figure out why.

here is the script:

#!/bin/bash 

SCRIPT_DIR=$(dirname $(readlink -f $0))
declare -a profiles_list=()
while [[ "$1" != "" ]]
do
        profiles_list+=($1)
        shift
done

if [[ ${#profiles_list[@]} -eq 0 ]]
then
        profiles_list+=("TERM_DEFAULT")
fi

TERM_LIST_PARAM=""

for profile in ${profiles_list[@]}
do
        file="${SCRIPT_DIR}/${profile}.tabs"
        if [[ ! -f $file ]]
        then
                echo $file does not exists
        fi
        while read tab
        do
                tab_line="--tab --working-directory='${tab}'"
                TERM_LIST_PARAM="$tab_line $TERM_LIST_PARAM"
        done < $file
done
echo gnome-terminal ${TERM_LIST_PARAM}
gnome-terminal -v ${TERM_LIST_PARAM}
echo $?
exit 0

2 Answers2

1

Use arrays for storing separate arguments. Using strings makes the shell confused when it later tries to split them up on whitespaces.

TERM_LIST_PARAM=( )

Followed by,

tab_line=( --tab --working-directory="$tab" )
TERM_LIST_PARAM=( "${tab_line[@]}" "${TERM_LIST_PARAM[@]}" )  # or: TERM_LIST_PARAM+=( "${tab_line[@]}" )

Then,

gnome-terminal -v "${TERM_LIST_PARAM[@]}"

Additionally, you must double quote $0, $1, $file, and ${profiles_list[@]} in your code, or you'll run into issues as soon as any of these values contains spaces and/or globbing characters.

See also:

Kusalananda
  • 333,661
0

apprently single quotes around the directory is disturbing gnome-terminal's piece just inside the script. removing them solved the problem.