MacOS – How to auto run a simple python program when the terminal opens

bashmacospythonstartupterminal

Say I have a python program called foo.py that prints a line of text. How can I auto run that program when I boot up the terminal? I know you can, but I can't find the documentation. Also no third party programs please, I'm not looking to make the program executable, just to have it run on the terminal start up.

Best Answer

You can put command to run the script in .bashrc file inside your home directory, this file is read every time you open non-login shell (Terminal, Iterm and their tabs).

python /path/to/foo.py

Don't forget to set the permissions for all to execute:

chmod a+x foo.py