How to persistently define aliases in Terminal
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.
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.
Enter the following commands in Terminal:
cd /etc
sudo vi bashrc
add the following like:
alias ll="ls -lrt"
Finally restart Terminal.
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
.