Most programmers always want to figure out a more efficient or easier way of doing something to save us time and effort. An alias in bash is meant for just that. An alias is essentially a keyboard shortcut or replacement mechanism for commonly typed commands in your shell. You are able to set them temporarily or permanently if placed in the correct file.
I will show you how to display current aliases already on your system, yes there are a few built-ins! I will also show you how to make your own and have them on your system during boot-up.
Show All Current Bash Aliases With ‘alias -p’
To display all the current aliases on your system as a normal user, type alias -p.
$ alias -p
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias ls='ls --color=auto'
And how! So how do these work? For instance, every time I type the ‘ls’ command the bash shell secretly replaces my ‘ls’ entry with ‘ls –color=auto’ so when I type ‘ls’ I get the pretty listing of my files all nicely colored. Awesome. So for us lazy people this can become quite handy! One command I type quite often is ‘ps aux wwwf’ to display all my processes current usage, thread output, etcetera. But that is a lot to type. So lets make an alias to save me time. I will call my alias ‘p’.
$ alias p="ps aux wwwf"
$ alias
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias ls='ls --color=auto'
alias p='ps aux wwwf'
Now you can see I have my process output alias. This is great, because anytime I need to check my systems process output I can just type ‘p’ instead of that lengthy spiel.
The lifetime of my new alias is only as long as the lifetime of my current shell. So when I exit the shell, the alias goes with it. If we want our alias to persist it must be stored in /etc/profile, .bash_aliases or your .bashrc file in your home directory.
Store Alias In File For Longevity
To create persistent aliases you must place them in one of three files:
- /etc/profile
- .bashrc
- .bash_aliases
For example, an excerpt from my .bashrc file:
# enable color support of ls and also add handy aliases
if [ -x /usr/bin/dircolors ]; then
test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
alias ls='ls --color=auto'
#alias dir='dir --color=auto'
#alias vdir='vdir --color=auto'
alias grep='grep --color=auto'
alias fgrep='fgrep --color=auto'
alias egrep='egrep --color=auto'
fi
# some more ls aliases
#alias ll='ls -l'
#alias la='ls -A'
#alias l='ls -CF'
# Alias definitions.
# You may want to put all your additions into a separate file like
# ~/.bash_aliases, instead of adding them here directly.
# See /usr/share/doc/bash-doc/examples in the bash-doc package.
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
You’ll notice at the bottom, if the .bash_aliases file exists, then Bash will include all aliases placed in that file for your shell. If you like cleanliness, I would suggest placing all your custom aliases in the .bash_aliases file. Otherwise, you are free to populate this file with any aliases you see fit.
Common Aliases
Here is a list of some common aliases I have come across:
alias ls='ls -aF --color=always'
alias ll='ls -l'
alias search=grep
alias mcd='mount /mnt/cdrom'
alias ucd='umount /mnt/cdrom'
alias mc='mc -c'
alias ..='cd ..'
alias ...='cd ../..'
alias L='ls -FLb'
alias LL='ls -FLlb'
alias a='alias'
alias h='history'
alias j='jobs -l'
alias l='ls -Fb'
alias la='ls -Flab'
alias ll='ls -Flb'
alias m='less'
alias ot='popd'
alias to='pushd'
alias zm='zless'
If you need a more complex alias, by all means. I created an alias to check the memory usage of my mysql database:
alias memu="ps --user mysql u | grep mysql | awk '{print $4;}' | awk '{split($0,a,"."); print a[1]}'"
Remove Alias With ‘unalias’
If you are now tired with a specific alias, you may unalias that command.
$ unalias menu
One thought on “Create Aliases in Bash Shell”