This problem I have for a long time. After running terminal .profile, and .bashrc file doesn`t work (are not executed). Could you indicate where should I looking form a source of problem?
Macos – .profile and .bashrc doesn`t work on the Mac
bashmacosprofileterminal
Related Solutions
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 with cd /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 do
gnome-terminal -x sh -c 'command1; command2; exec bash'
If 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):
gnome-terminal -e 'sh -c command1;command2;exec<TAB>bash'
gnome-terminal --tab -e 'sh -c command1;<TAB>exec<TAB>bash' \
--tab -e 'sh -c command2;<TAB>exec<TAB>bash'
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
:
eval "$BASH_POST_RC"
Then to run a some code after (really, at the end of) your bashrc:
gnome-terminal -x sh -c BASH_POST_RC=\''command1; command2'\''; exec bash'
or (less heavy on the quoting)
BASH_POST_RC='command1; command2' gnome-terminal
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?.
terdon's approach works well under the right circumstances, but if, e.g., .bashrc appends to the PATH variable, it will cause errors rather quickly.
Instead of simply resourcing the file, you could check its modification time first and compare it to the mtime of the last sourced version.
To do so, append this to ~/.bashrc:
Linux
bashrc_sourced=$(stat -c %Y ~/.bashrc) PROMPT_COMMAND=' test $(stat -c %Y ~/.bashrc) -ne $bashrc_sourced && source ~/.bashrc '
OS X and BSD
bashrc_sourced=$(stat -f %m ~/.bashrc) PROMPT_COMMAND=' test $(stat -f %m ~/.bashrc) -ne $bashrc_sourced && source ~/.bashrc '
Then, resource it manually one final time.
Best Answer
I had a similar problem with my
.profile
not being run. It turns out (as explained in this Apple StackExchange answer) that if you have.bash_profile
or.bash_login
files, then your.profile
will be ignored by bash.