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.)

  • 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? – nohillside 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
  • I've rolled back your edit because it changed the focus of the question. If you are still looking for an answer about how to add aliases to zsh, please ask a new question. – nohillside Feb 21 '16 at 18:27

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.

| improve this answer | |
  • 3
    @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. – Lri 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
  • A better option is to keep aliases is in a file called ~/.aliases if you are using oh-my-zsh. If this file doesnt exist, it can be added in the home folder and would be a hidden file. – Anusha Sep 18 '15 at 18:47

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.

| improve this answer | |

Enter the following commands in Terminal:

cd /etc
sudo vi bashrc

add the following like:

alias ll="ls -lrt"

Finally restart Terminal.

| improve this answer | |

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.

| improve this answer | |

if you have ruby installed, check out this ruby script https://github.com/ytbryan/aka

The script provides functionalities like persistency and auto sourcing of dot file. You can manage aliases via terminal without breaking your workflow and open a text editor.

disclaimer: I wrote this script and use it everyday. Let me know if there's any bug/idea. :)

| improve this answer | |

For those of you looking for the ll command like in Linux BASH, it is created like this...

alias ll="ls -laF"

Add that line to your .bashrc file, then duplicate the .bashrc file as .bash_profile, and you'll have ll always and forevermore.

For what it's worth, ll on linux BASH is just an alias of "ls -laF".

| improve this answer | |

i had the same Problem, that i had no .bash or .profile in my Profiles location. i finally found it in /etc

| improve this answer | |
  • 1
    /etc is used system-wide. I strongly recommend not editing these files, but instead using your own profile files in your home folder, e.g. ~/.bash_profile. – grg May 22 '18 at 10:06
  • i don't seam to have a ~/.bash_profile, it was the first thing i tryed and if i create one, it's not beeing used even after a restart, i belive my admins must have prevented it some how. i'm now using zsh and the .zshrc works well – Leo Boulanger May 29 '18 at 13:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .