Execute command on Terminal launch (macOS)
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.