3

I'm trying to launch one command in a new gnome-terminal window from a shell script, and it seems that my .bashrc file doesn't get sourced when calling gnome-terminal and executing a command:

gnome-terminal -t "my title" -e vim

But it does when launching gnome-terminal alone:

gnome-terminal -t "my title"

(for testing purposes, just add a echo "something" to the end of the .bashrc)

I also tried explicitly sourcing the .bashrc before running vim with no success:

gnome-terminal -t "my title" -x $SHELL -c "source $HOME/.bashrc; vim"

Am I missing something?

2 Answers 2

2

No, your are correct. gnome-terminal does not source .bashrc when running a command via the -e argument.

Bash and gnome-terminal are two separate things. When you run without -e gnome-terminal runs your default shell (which happens to be bash). When you run with -e gnome-terminal runs the command you provided (bash is not loaded so .bashrc is not sourced).

Your last example will not work either. $HOME is a variable in bash and not available. In addition source is a bash command, and vim likely needs a path.

The best way to do this, in my opinion is to make a wrapper script.

In your path somewhere (I added ~/.bin/ to my path) create a file called vim-x and set it executable.

#!/bin/bash
source $HOME/.bashrc
vim "$@"

Then you can run:

gnome-terminal -t "my title" -e /home/user/.bin/vim-x

1
  • Thanks for your reply. I tried this approach and it still doesn't work (no echo "something" in the new gnome-terminal window)
    – el.atomo
    Nov 21, 2012 at 13:10
2

I found the problem with the help of this reply.

Actually the .bashrc was sourced, but not completely processed because of the following lines:

# If not running interactively, don't do anything
[ -z "$PS1" ] && return

To bypass the problem, the cleanest approach would be to force the shell instance to be run in interactive mode (as seen here):

gnome-terminal -t "my title" -x $SHELL -ic "vim"
2
  • careful that will break things user wide.
    – coteyr
    Nov 21, 2012 at 13:45
  • Glad you got it working.
    – coteyr
    Nov 21, 2012 at 14:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.