For starting my dev environment I wrote a little script. One part is to open a gnome terminal with several tabs where automatically some commands should be executed. Some of those commands depend on an already executed .bashrc.
But when using
gnome-terminal --tab -e "command" --tab --tab
the command is executed before .bashrc was executed.
Is there a possibility to make an automated gnome-terminal -e behave like a manually used one? (even commands like "cd /foo/bar" do not work with gnome-terminal -e)
Best Answer
Once gnome-terminal has started bash, it's out of the loop as far as command execution is concerned: it only manages the input and output. So you'll need bash's cooperation to run something after
~/.bashrc
has been loaded.First, in many cases, you don't actually need to execute commands after
~/.bashrc
. For example, opening a terminal in a particular directory can simply be done withcd /foo/bar && gnome-terminal
. You can set environment variables in a similar way:VAR=value gnome-terminal
. (If your~/.bashrc
overrides environment variables, you're doing it wrong: environment variable definitions belong in~/.profile
)To execute commands in the terminal, but before
~/.bashrc
, you can doIf you want to use multiple tabs, you have to use
-e
instead of-x
. Gnome-terminal unhelpfully splits the argument of-e
at spaces rather than executing it through a shell. Nonetheless, you can write a shell command if you make sure not to include spaces in it. At least with gnome-terminal 2.26, you can use tabs, though (replace<TAB>
by a literal tab character):If you do need to run commands after
~/.bashrc
, make it run the commands. For example, include the following code at the end of~/.bashrc
:Then to run a some code after (really, at the end of) your bashrc:
or (less heavy on the quoting)
Although I don't particularly recommend doing it this way, you may be interested in the techniques mentioned in How to start a terminal with certain text already input on the command-line?.