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)

Related articles:


  1. Under Ubuntu 8.04, I want to have the Firestarter firewall start at the beginning of every log-on session.
    Can I assume, from the above article that, if I enter a command such as “firestarter start” into the .bash_profile file, it will accomplish my goal?
    Do you recommend a particular command for this or a different way to accomplish the same task?

  2. System-level functions like firewalls are usually installed as “daemons” that run all the time, regardless of who’s logged in. Not only do you want to make sure that they run all the time, even if no one is logged in, but also that you only want one copy running even if multiple users are logged in (or one user is logged in multiple times in different windows). So, no. .bash_profile would not be the right place to start Firestarter, because .bash_profile is user-specific.

Post a Comment

Your email is never published nor shared. Required fields are marked *

© 2006-2007 Maxim Software Corp.  All rights reserved.