Reputation: 2732
When I use the following command, everything works as expected (a new gnome-terminal is open, the current working directory is changed and the terminal remains open):
gnome-terminal -e "bash -c 'cd /';$SHELL"
# ↑
# no space character here
But when I use:
gnome-terminal -e "bash -c 'cd /'; $SHELL"
# ↑
# note the space
I can see a terminal that opens, but I can't see if the current working directory is changed or not because the terminal it closes immediately.
My question is: why this is happening; how can be wrong if I put a space in the second case?
Upvotes: 0
Views: 150
Reputation: 10271
gnome-terminal
does its own parsing of the command given to it, using g_shell_parse_argv, which apparently doesn't consider ;
to be a word separator, so if a ;
is adjacent to a non-whitespace character, it is considered to be part of that non-whitespace character's word.
This can result in surprising behavior if the command you pass to gnome-terminal
has shell metacharacters in it.
I used strace
on the gnome-terminal
process to see what it does. The first command
gnome-terminal -e "bash -c 'cd /';$SHELL"
results in gnome-terminal running the following command
execve("/bin/bash", ["bash", "-c", "cd /;/bin/bash"])
The second command
gnome-terminal -e "bash -c 'cd /'; $SHELL"
results in gnome-terminal running the following command
execve("/bin/bash", ["bash", "-c", "cd /;", "/bin/bash"])
That being said, you should also note that a command such as bash -c 'cd /'
will not have a lasting effect on the working directory of any command run after it. So I think that the first command you typed got the desired result only because of the gnome-terminal parsing misfeature, and a more robust way of writing it would be
gnome-terminal -e "bash -c 'cd /;$SHELL'"
Upvotes: 3
Reputation: 123570
In the first case, gnome-terminal executes
bash -c "cd /;/bin/bash"
in the second case, it runs
bash -c "cd /;" /bin/bash
The first case means "evaluate cd /;/bin/bash
", which will cd and run an interactive shell.
The second case means "evaluate cd /;
with $0
set to /bin/bash
", which will run cd and then exit.
Upvotes: 6