The $
characters are being processed by your original shell, not the shell executed inside gnome-terminal
. Also, the embedded double quotes are delimiting the -c
argument, not being passed explicitly to bash
.
You need to escape the $
and "
characters to preserve them.
gnome-terminal --working-directory=/path/to/dir/ -- bash -c "{ for i in {1..10};
do echo \"2\"; echo \"file\"\$i".txt\"; echo \$'\n'; done; } | ./program; exec bash"
You could also put the command in single quotes, but then you won't be able to embed $'\n'
in it, because you can't escape quotes inside single quotes. But you can use printf
instead of echo
, since it will translate escape sequences itself.
gnome-terminal --working-directory=/path/to/dir/ -- bash -c '{ for i in {1..10};
do echo "2"; printf "file%d.txt\n\n" $i; done; } | ./program; exec bash'
See Difference between single and double quotes in Bash