Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

I want to add aliases to my environment so that they are available in all Terminal instances.

I got this answer:

Yes, you can put it in your .bashrc, .bash_profile or .profile. On some systems the shell init scripts also source .bash_aliases or .aliases so if one of those files exists on your system it may make sense to use them.`

But I cannot find any of these files.

(I configured OS X to see hidden files, and yet I don't see any of the above 4.)

update

I have added to .bashrc and .bash_profile this line

alias clientdir="cd ~/MyWorkingSpace/Client"

but yet, when i open zshel, i get:

$ cdclient

zsh: command not found: cdclient
share|improve this question
    
Aliases only work in the shell (in Terminal), so what exactly do you mean by adding aliases to your OS X environment? Do you want to make an alias accessible to all users? –  patrix Aug 22 '13 at 8:55
    
I meant it will be set for every terminal instance I open in the future –  Elad Benda Aug 22 '13 at 9:02
1  
OK - so there's two things wrong there. .bashrc and .bash_profile are only used if your shell is bash. If you're using zsh, then you need to edit .zshrc and add the alias lines into that file instead (it's possible you won't have .zshrc - if that's the case you need to create it). The second thing that went wrong here is that your alias, as defined in the rc file, is clientdir but you were trying to execute cdclient instead. If your alias is set as clientdir, you should type in your shell that exact same command: clientdir. –  mike Aug 29 '13 at 11:51

3 Answers 3

up vote 6 down vote accepted

If you use bash, add the alias commands to ~/.bashrc and save a file like this as ~/.bash_profile:

. ~/.bashrc

When bash is invoked as an interactive non-login shell, it reads .bashrc but not .bash_profile. When bash is it is invoked as an interactive login shell, it reads .bash_profile but not .bashrc.

Terminal and iTerm open new shells as login shells by default, so many OS X users just use .bash_profile as their personal configuration file. For example tmux and the shell mode in Emacs open new shells as non-login shells though.

.profile is also read by other shells like ksh. If both .bash_profile and .profile exist, bash only reads .bash_profile when it is invoked as an interactive login shell.

See https://www.gnu.org/software/bash/manual/html_node/Bash-Startup-Files.html for more information.

share|improve this answer
    
please see my update –  Elad Benda Aug 29 '13 at 10:12
    
@EladBenda If you use zsh, add the alias commands to ~/.zshrc. ~/.zshrc is read by both (interactive) non-login and login shells, so you don't have to source it from ~/.zprofile. –  ؘؘؘؘ Aug 29 '13 at 10:50
    
Have always wanted to know the difference between .bash_profile and .bashrc, now I know. Thanks. –  Deesbek Oct 16 '13 at 1:10

If you need this machine wide you need to look in the /etc folder. However, they may not exist by default, in which case you simply need to create them in the above folder and restart terminal. You will most likely need to use sudo to create them as the /etc folder is owned by root.

If you need this for every terminal you open you don't need to do it machine wide. You can create these files in your own home folder and it will be read by terminal every time you start a terminal session.

Also as mentioned in comments, you will generally only be able to see these using terminal and using ls -lha.

share|improve this answer
    
please see my update –  Elad Benda Aug 29 '13 at 10:14

Open a new Terminal window and go to your home directory (just type cd and press Enter).

After that, type ls .bash* and also ls .profile (please note each of those file names starts with a dot). If you have those files (and you should have at least the .profile one) then you need to edit them and add your aliases to them. I use VI (or VIM) to edit those files, but you could use another editor (in fact typing in your Terminal window open .bash_profile will open it in TextEditor, which may be a bit more user-friendly if you're not familiar with VI). If, for some reason, you don't have any of those files, then create a new one (and call it .bashrc), add your alias lines into it (so this will be one command per line, as you would type them in shell) and then save the file. You can then create symlinks to .bash_profile and .profile by using ln -s .bashrc .bash_profile; ln -s .bashrc .profile.

What I typically do is I make .bashrc and .bash_profile symbolic links to .profile and then just have to worry about one file only.

The difference between those files is that bash will source different ones depending on whether it has been started as a login shell (typically a login process would start the shell as a login shell) or as just an interactive (but not login) shell, or a non-interactive shell. Having all three files essentially the same gives me consistent environment regardless of how was bash started.

You will get more information by doing man bash, but be prepared, it's a long man page. You will be able to see which of the three files (.bashrc, .profile and .bash_profile) is sourced depending on which mode was bash started in.

Hope that helps - if not, give me a shout.

share|improve this answer
    
please see my update –  Elad Benda Aug 29 '13 at 10:14

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.