Go Back   The macosxhints Forums > OS X Help Requests > UNIX - General



Reply
 
Thread Tools Rating: Thread Rating: 5 votes, 5.00 average. Display Modes
Old 04-27-2012, 05:44 PM   #1
spookybathtub
Prospect
 
Join Date: Apr 2007
Posts: 12
Multiple ways to create permanent aliases

On my mac, I have created some permanent aliases in the past, but I don't remember how. I can see them by typing
Code:
alias
But I don't see them in my ~/.profile, where I have stored some other aliases. Where else could they be? They are simple
Code:
alias c='clear'
alias ls='ls -lp'
spookybathtub is offline   Reply With Quote
Old 04-27-2012, 05:54 PM   #2
trevor
Moderator
 
Join Date: Jun 2003
Location: Boulder, CO USA
Posts: 19,839
Assuming that you're using bash:

/etc/profile
~/.bash_profile
~/.bash_login
~/.profile
For non-login shells: ~/.bashrc

Also, it could be in any file of any name that one of the above files calls.

Trevor
trevor is offline   Reply With Quote
Old 04-27-2012, 05:56 PM   #3
spookybathtub
Prospect
 
Join Date: Apr 2007
Posts: 12
Thanks for the reply. Yes, I am using bash. But of those files you listed, the only one that exists is ~/.profile. Are there any other places?
spookybathtub is offline   Reply With Quote
Old 04-28-2012, 10:26 AM   #4
cpragman
All Star
 
Join Date: Jan 2004
Location: Limerick, PA
Posts: 846
In Lion, it seems like terminal doesn't load a fresh copy of .profile when the terminal
App is launched. That means that changes to .profile don't seem to take effect.
cpragman is offline   Reply With Quote
Old 04-28-2012, 02:23 PM   #5
jsalmi
Major Leaguer
 
Join Date: May 2008
Location: Down by the river
Posts: 282
Quote:
Originally Posted by cpragman
In Lion, it seems like terminal doesn't load a fresh copy of .profile when the terminal
App is launched. That means that changes to .profile don't seem to take effect.

Copy your .profile to .bash_profile (cp .profile .bash_profile). Mine is read each and every time I use Termninal.
jsalmi is offline   Reply With Quote
Old 04-28-2012, 09:25 PM   #6
ganbustein
MVP
 
Join Date: Apr 2008
Location: Berkeley CA USA
Posts: 1,193
Each time Terminal opens a new window, it launches a new instance of your default shell as an interactive login session. If your default shell is bash, that means it reads and executes the commands from /etc/profile (assuming it exists, as it should), and then looks for and executes one of ~/.bash_profile, ~/.bash_login, or ~/.profile. It only executes the first one of these that it finds, which can be perplexing if you have more than one of them. (In particular, it won't read ~/.profile if either of ~/.bash_profile or ~/.bash_login exists.) This is the way it's always been. It's not new with Leopard.

An interactive but non-login bash session invokes only

You can force other files to be read. For example, your ~/.bash_login file could contain:

if [ -r ~/.profile ] ; then . ~/.profile; fi

if you want ~/.profile to be read even though ~/.bash_login is present. It's probably less confusing, though, to minimize the number of startup files you create. Put in ~/.bashrc the stuff you want in all interactive bash sessions, be they login or not, and put in ~/.bash_login the stuff that only applies to login shells. (For example, it's only at login time that you need to futz around with $PATH.) Then make ~/.bash_login invoke ~/.bashrc.

~/.profile is primarily for sh login shells, but bash will use it if the more specific ~/.bash* files can't be found.

Non-interactive shells do not as a rule read any startup files. They inherit everything they need from the environment of their invoker. (Kinda has to be this way. A startup file is likely to invoke non-interactive shells, and you'd get into a horrible loop.)
ganbustein is offline   Reply With Quote
Old 04-30-2012, 12:25 PM   #7
trevor
Moderator
 
Join Date: Jun 2003
Location: Boulder, CO USA
Posts: 19,839
Quote:
Originally Posted by trevor
Also, it could be in any file of any name that one of the above files calls.

Quote:
Originally Posted by spookybathtub
Thanks for the reply. Yes, I am using bash. But of those files you listed, the only one that exists is ~/.profile. Are there any other places?

Yes, it could be in any file of any name that ~/.profile calls. Can we see your ~/.profile?

Trevor
trevor is offline   Reply With Quote
Old 04-30-2012, 12:34 PM   #8
trevor
Moderator
 
Join Date: Jun 2003
Location: Boulder, CO USA
Posts: 19,839
Quote:
Originally Posted by ganbustein
It's probably less confusing, though, to minimize the number of startup files you create. Put in ~/.bashrc the stuff you want in all interactive bash sessions, be they login or not, and put in ~/.bash_login the stuff that only applies to login shells. (For example, it's only at login time that you need to futz around with $PATH.) Then make ~/.bash_login invoke ~/.bashrc.

By the way, I agree with ganbustein above. Although having only a ~/.profile will work when you follow the rules, it's a sh shell file, not a bash file. My preferred method is a minimalist ~/.bash_profile that looks like this:

Code:
#------------------------
#
# $HOME/.bash_profile
# 
#------------------------

source $HOME/.bashrc
...and then I put all the important stuff into ~/.bashrc

Trevor
trevor is offline   Reply With Quote
Reply

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



All times are GMT -5. The time now is 06:59 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.