Execute command on Terminal launch (macOS)
Is there an easy way to force the Terminal to run a command every time it is launched on macOS?
I did some searching on here and most of the time this question is asked on here with regards to Ubuntu but I can't find a Mac solution. For Ubuntu, everyone says to use bashrc
but that file isn't in my home directory.
Many thanks.
The most common way is to add the command to .bash_profile
file in your home directory.
Quoting man bash
which lists other options as well:
When bash is invoked as an interactive login shell, or as a non-interactive shell with the
--login
option, it first reads and executes commands from the file/etc/profile
, if that file exists. After reading that file, it looks for~/.bash_profile
,~/.bash_login
, and~/.profile
, in that order, and reads and executes commands from the first one that exists and is readable.
If you want to run a command when Terminal.app opens a shell, not just for any interactive shell:
- Open Terminal → Preferences → Profiles.
- Select the profile you use, then switch to the Shell tab.
-
Under Startup, enter the command you wish to run as the Run command.