21

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

3
  • 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, 2013 at 8:55
  • I meant it will be set for every terminal instance I open in the future
    – Elad Benda
    Aug 22, 2013 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, 2016 at 18:27

8 Answers 8

27

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

. ~/.bashrc

When bash is invoked as an interactive non-login shell, it reads .bashrc but not .bash_profile. When bash 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.

3
  • 4
    @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, 2013 at 10:50
  • Have always wanted to know the difference between .bash_profile and .bashrc, now I know. Thanks.
    – Deesbek
    Oct 16, 2013 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, 2015 at 18:47
6

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.

0
3

Enter the following commands in Terminal:

cd /etc
sudo vi bashrc

add the following like:

alias ll="ls -lrt"

Finally restart Terminal.

2

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.

0
1

I'm on the new Mac M1 processor. I don't know if that's a factor in this case but I found that I had a file called .zprofile that actually got ran when a new terminal was created.

I think in most cases you could find a file like this if you go to

cd ~

and do

la -a

I wanted to load my aliases in .bash_profile on startup so I put this into .zprofile

source ~/.bash_profile

Now, I don't have to do that myself every time I get into a new terminal. Yay!

3
  • @Allan - your edit has altered what brando f was trying to say, and also doesn't make any sense. They have their aliases in .bash_profile and they want to source .bash_profile from .zprofile. Your edit now has .zprofile sourcing itself! This edit needs to be rolled back, or that part of the edit reverted to that which brando f wanted to say... Feb 9, 2023 at 19:26
  • @Greenonline, my mistake, but to be candid, sourcing .bash_profile from .zprofile is confusing both now and later on because there are subtle, but significant differences between Bash and Zsh where this method will cause headaches. This is not recommended!
    – Allan
    Feb 9, 2023 at 19:43
  • Oh ok thanks guys, I didn't know about this stuff to much. Would you recommend just putting aliases in .zprofile? thanks
    – brando f
    Feb 9, 2023 at 21:38
0

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

0

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

0
-2

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

2
  • 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, 2018 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 May 29, 2018 at 13:46

You must log in to answer this question.

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