Cerin
Cerin

Reputation: 64840

Programmatically open Gnome Terminal and run command

How do you programmatically opening a terminal application, like Gnome Terminal, and running cd /some/path; source ./setup.bash? I'm trying to write a script that will automatically launch some common terminals and IDEs for work.

I tried:

gnome-terminal --tab --working-directory="/some/path" -e 'source ./setup.bash'

but that launches a gnome-terminal window, but the window shows the error:

Failed to execute child process "source" (No such file or directory)

Presumably, that's because it's not executing the command in bash, so I instead tried:

gnome-terminal --tab --working-directory="/some/path" -e 'bash -c "source ./setup.bash"'

However, that seems to do nothing at all. It launches no window nor produces any stdout or stderr output.

The closest I could get was:

gnome-terminal --tab --working-directory="/some/path" -e 'bash -c "source ./setup.bash; bash -i"'

That launches gnome-terminal and seems to source setup.bash correctly, but some of the terminal formatting set by setup.bash isn't shown, presumably because I'm launching a new bash shell.

Is there a better way?

Upvotes: 5

Views: 3796

Answers (2)

Jack
Jack

Reputation: 6198

You can specify the bash startup file to set variables. You might want that file to have source $HOME/.bashrc in it:

$ gnome-terminal --working-directory="/some/path" -e 'bash --rcfile ./setup.bash -c gdb'

You can put a command in after that,as I have -c gdb.

Upvotes: 1

Harini
Harini

Reputation: 571

When you use the -e option the gnome-terminal will run that command without starting a new shell (you can even run something like: gnome-terminal -e gedit), so if you want to run a command into the bash shell into a new terminal/tab you have to do something like this:

gnome-terminal -x bash -c "command"

But note that when "command" ends the terminal/tab will end too.

Upvotes: 2

Related Questions