Auto-Commands at Login
In yesterday’s post, The ALIAS Command Saves Repetitive Typing, I mentioned how alias definitions only stick around for as long as you are logged in. So, to make an alias act as if it is permanent, you have to re-define it every time you log in. Fortunately, there’s an easy way to do that automatically.
There’s a file in every user’s home folder called .bash_profile. Commands found in that file execute every time the user logs in.
In the case of a GUI environment such as Mac OS X, we’re talking about every time you open a new command-prompt window. (Note: if you are using a non-bash shell, then the name of the auto-command file might be simply .profile)
Note: Be aware that file names that begin with a period, like this one, are normally hidden from you. The Mac Finder, for example won’t show them if it is set to its default settings (see View Hidden Folders in Mac OSX Finder). The ls command won’t show them either, unless the “-a” option is specified (”ls -a”).
So, to specify commands that you want to run every time you log in, just edit the .bash_profile file and add the commands in question. On a Mac, one way to do this without involving the Finder is via the PICO editor from the command line:
cd ~ pico .bash_profile
Add your commands to the end of the file (or however you want to organize them), then press Ctrl+X to exit and save. You’ll have to log out and back in before the commands will take effect (i.e. close the terminal window and open a new one).
Tip: By the way, knowing about the PICO command-line editor is especially helpful when you need to access a restricted file via the sudo command (e.g. sudo pico /etc/hosts)
- The ALIAS Command Saves Repetitive Typing
- View Hidden Folders in Mac OSX Finder
- Auto-Commands at Login
- Learning Linux Commands
- Use Linux Commands and Shell Scripts directly in Windows
- Tips for Launching CygWin