Issue
I've got a simple shell script that should open a new terminal window with multiple (three) tabs.
open_gnome_terminal () {
gnome-terminal \
--window -- bash -ic "echo tab1; exec bash;" \
--tab -- bash -ic "echo tab2; exec bash;" \
--tab -- bash -ic "echo tab3; exec bash;"
}
open_gnome_terminal
However, when I run the script via . ./myScript
I get the following error:
--window: command not found
--tab: command not found
I also tried to run that script all in a single command in terminal and it worked fine, so my question is why backslash operator doesn't work properly in my shell script and how do I fix that? I use Linux Ubuntu 23.04
EDIT: I removed spaces after backslashes and the errors are gone now. Though I still didn't achieve the desired result of a new terminal window with 3 tabs (It only opens up a window with one tab)
Solution
Your code is:
open_gnome_terminal () {
gnome-terminal \
--window -- bash -ic "echo tab1; exec bash;" \
--tab -- bash -ic "echo tab2; exec bash;" \
--tab -- bash -ic "echo tab3; exec bash;"
}
This invokes gnome-terminal
with one option --window
.
Then option processing is terminated by --
.
The rest of the "line" is a program to run and its arguments:
bash -ic "echo tab1; exec bash;" --tab -- bash -ic "echo tab2; exec bash;" --tab -- bash -ic "echo tab3; exec bash;"
bash
reads a command from the argument following -c
.
The next argument (--tab
) is used to set its $0
.
The remaining arguments are ignored (since you did not refer to them in the first -c
command).
To do what you want, the --command
option is deprecated but appears to still work on my machine:
open_gnome_terminal() {
gnome-terminal \
--window --command 'bash -ic "echo tab1; exec bash"' \
--tab --command 'bash -ic "echo tab2; exec bash"' \
--tab --command 'bash -ic "echo tab3; exec bash"'
}
My manpage warns that this is not an actual shell commandline. I would probably put more complicated code into a shell script and invoke that (--command /path/to/my/script
)
Answered By - jhnc Answer Checked By - Timothy Miller (WPSolving Admin)