A Bash alias
is a unix based command line shortcut. Example I’ve used in
previous post,
is ls -laFh
, which I use all the time. Just imagine typing that hundreds of times a day (if you’re job is to work on
unix based operating systems). To remove the complexity, you can declare an alias, simply typing alias ll='ls -laFh'
.
Now, instead of typing ls -laFh
bunch of times, I just type ll
and get the same result.
Making an alias is as simple as alias alias_name='command(s)'
.
Besides, you don’t have to remember all flags and parameters when you use alias
, which I tend to forget a lot. Then
you have to go through man
pages and look for flags/params you need, or search the internet for the info you need.
Alias helps you concentrate on important things and saves you time in the long run.
Making alias permanent
By default, making an alias will make it visible just in current shell session. This means if you close the session, the alias you’ve made won’t exist in other sessions. To make alias persistent, you have to store it in a configuration file.
Before updating config file content, you can back it up first.
cp ~/.bashrc ~/.bashrc.bak
The extension .bak
isn’t strict, you can use anything you’d like, you can use .backup
for instance.
After writing alias to ~/.bashrc
, save the file and type source ~/.bashrc
. Other way that you can do this is by
typing dot (.), which does the same thing as source. So instead of source ~/.bashrc
type . ~/.bashrc
.
This will reload the configuration file.
If you want, you can put this into ~/.bashrc
, so you can load aliases from another file. I’ve named it
.bash_aliases
, but you can name it anything you want:
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
Now you can type all your aliases in a single place, so you don’t clutter your ~/.bashrc
file. For me, I don’t use
this method, since I like to have everything in one place.
A word about config file
Different types of operating systems have different places where you can store your configs (like aliases). For MacOS
that would be in ~/.bash_profile
, for Debian based Linux operating systems that would be in ~/.bashrc
.
Unalias
To remove an existing alias, simply type unalias alias_name
. If you persisted alias, you should delete it from the
file you persisted it in as well. I usually just comment aliases I don’t use, so I can remove the comment and get them
back.
List aliases
To list aliases that you have in you current session or that are persisted, type alias
, and you will get the
result. If you have a lot of aliases, you can grep through them, so something like alias | grep search_term
.
Bypass alias
If you have alias same as some command, for example grep
, which for me is alias grep='grep --color=auto -i'
, you can
use regular grep
typing \grep search_term
.
Useful aliases
These are some aliases I use on my mac, I have about 30 aliases, but most of them are for Docker and ssh, which I’ve left out.
alias new_8_char_pass='openssl rand -base64 8' # gives you a new 8 char password
alias new_16_char_pass='openssl rand -base64 16' # gives you a new 16 char password
alias mv='mv -v' # verbose mv
alias mkdir='mkdir -v' # verbose mkdir
alias rm='rm -v' # verbose rm
alias chmod='chmod -v' # verbose chmod
alias cp='cp -v' # verbose cp
alias grep='grep --color=auto -i' # grep with highlight and case insensitive search
alias less='less -iN' # -i ignore case, -N line numbers
alias ll='ls -laFh' # ls list all with displaying symbols if it's dir, file, executable.. and human readable memory
alias lsOpenPorts='sudo lsof -i -P -n | grep LISTEN' # lists open ports on Mac
alias net-util='open -a /System/Library/CoreServices/Applications/Network\ Utility.app/' # opens Network Utility app
alias tree='tree -aC --dirsfirst' # shows tree view of filesystem, -a all files, -C color, --dirsfirst for listing the directories first, then files
ssh alias
For ssh
, I’ve used alias for some time, until I moved every server information to ~/.ssh/config
. Populate config
file for new servers like:
Host server1
Hostname 1.1.1.1
port 22
user milos
IdentityFile ~/.ssh/id_rsa
For me, I have few servers, now I can connect with any of them with ssh server1
(no alias). Now, since I’m using some
terminal configuration - I’m using iTerm2 - I’ve made a function to change the profile. I
have 3 profiles, which are basically the same, just colors are different. Default profile is with some theme. Production
profile is like Default profile, but has the tab and cursor in red color, and Development profile is like Production,
just the colors are orange (ubuntu orange).
Function is like this:
function setProfile {
PROFILE=$1;
if [ -z "$PROFILE" ]; then
PROFILE="Default";
fi
echo -e "\033]50;SetProfile=$PROFILE\a"
}
function ssh-server {
SERVER=$1
PROFILE=$2
setProfile $PROFILE
ssh $SERVER
setProfile
}
First function tells iTerm2 to set the profile to some value. If argument isn’t provided, it will go back to Default
profile. Second function I use for aliases. Typing ssh-server server1 Production
would color my tab and cursor to red,
and then ssh into that server. Once I exit server, it will go back to Default profile. Or you can make an alias
alias ssh-production='ssh-server server1 Production'
and save it to ~/.bashrc
, like I did.
Other way you can do it is to ignore the ~/.ssh/config
file and just write an alias:
alias ssh-production='ssh -i ~/.ssh/id_rsa milos@1.1.1.1'
Tip to add alias fast
You can use the command below to add aliases without entering the ~/.bash_aliases
(or ~/.bashrc
or
~/.bash_profile
).
echo alias alias_name='command' >> ~/.bash_aliases && source ~/.bashrc'
To break it down echo alias alias_name='command'
will print everything after echo
. Next, >>
will append output of
echo to ~/.bash_aliases
file. Once that is complete (for which &&
stands for), you should source
the ~/.bashrc
file. With that line of code, you can create and use alias right away. You can probably make a function, but that’s not
necessary since you probably won’t be making that much aliases anyway. Or you can source it with the dot:
echo alias alias_name='command' >> ~/.bash_aliases && . ~/.bashrc'
Final words
I hope this post helped you get into the alias
command a bit, and hopefully you’ve made some custom aliases. Go and
experiment with things you use day to day, if you find some pattern that you use a lot, put it into config file and use
the alias in the future. It will save you time, and you can concentrate on important things.