Execute a command every time Terminal is open

19

5

I'm on Ubuntu 13.04 and I'd like to configure my terminal, so that every time a new tab/window is open, it executes the following command automatically and right away.

 /bin/bash --login

How could I achieve this? On OSX with iTerm 2 there is an option to run a command on start. I guess here I should achieve this thru some kind of dotfile...

if __name__ is None

Posted 2013-05-04T01:17:51.307

Reputation: 447

Why do you want to do this? When you open a new terminal, this command is already executed. – Spack – 2013-05-04T01:27:44.240

No its not. I need this because of a bug in some development environment I use. The --login flag is crucial. – if __name__ is None – 2013-05-04T01:51:20.793

Answers

21

Add the command to your ~/.bashrc. Technically, you should be able to add to ~/.profile as well, from man bash, INVOCATION documentation:

If bash is invoked with the name sh, it tries to mimic the startup behavior of historical versions of sh as closely as possible, while conforming to the POSIX standard as well. When invoked as an interactive login shell, or a non-interactive shell with the --login option, it first attempts to read and execute commands from /etc/profile and ~/.profile, in that order.

Also:

Bash attempts to determine when it is being run with its standard input connected to a network connection, as when executed by the remote shell daemon, usually rshd, or the secure shell daemon sshd. If bash determines it is being run in this fashion, it reads and executes commands from ~/.bashrc and ~/.bashrc, if these files exist and are readable.

So, this means that if you're logged in locally you should be OK using ~/.profile; but, if you ssh to systems then you'll want to use ~/.bashrc. ~/.bashrc also applies when logged in locally; so, it has just become convention to use that.

user100059

Posted 2013-05-04T01:17:51.307

Reputation:

3

Try:

  • In terminal, Edit > Preferences > Tab Title and command > Run a custom command of my shell.

enter image description here

Or

  • gnome-terminal --tab -e "your command" to open a new tab.

More informations: ManPage Gnome-terminal

stderr

Posted 2013-05-04T01:17:51.307

Reputation: 9 300

2Actually, just checking the run command as login shell fixed my problem. – if __name__ is None – 2013-05-04T13:58:19.637