Muna writes

29 Jun 2020 - 3 min read The alias command - Doing more by typing less

The alias command to me is the most powerful command in the Linux system. From saving you time by substituting complex commands, to not so funny pranks the alias command lives up to the Unix philosophy of doing one thing and doing it well.


Perhaps you would like to slightly alter the definition of a command, or maybe you always want to use a particular option on a command. The alias command enables the replacement of any command by another string.

How does it work?


To create an alias, the user supplies name/value pairs as arguments for the alias command.

$alias h='ls $HOME'


When the command h is read in the shell, it will be substituted with ls $HOME and that command will be executed. The substitution occurs very early in the cli processing, so all other variables will be evaluated after.


To view all applied aliases , just type in the command

$alias 


If you don’t like what your alias does and want to get rid of it, just use unalias and the name of the alias that you no longer want. For example:

$unalias h

My approach to using aliases


To use alias on my system, I firstly make them permanent by creating ~/.bash_aliases file

 $touch ~/.bash_aliases


Secondly I add the following code to the ~/.bashrc file :

if [ -e $HOME/.bash_aliases ]; then
  source $HOME/.bash_aliases
fi
   


remember to reload your shell to load the changes you made to .bashrc, you can do this with the following command:

 $exec bash


All thats left is to then come up with memorable aliases for long and complex commands I use often.

My list of handy aliases


Here is a list of aliases currently configured I my system.

1. history


Shorten searching commands on history.

$alias gh='history | grep'
 

2. left


Left provides a convenient way of checking which files you worked on last.

$alias left='ls -t -1'

3. python


If you use python for development you will definitely relate to the neck breaking work of typing python3 each time you want to run a python script.

$alias py='python3'

4. today


A handy way of showing todays date.

$alias today='date +"%A, %B %-d, %Y"'
 

5. apt


Updates on linux (unlike other systems) are a regular thing , you can easily make work easier:

$alias sysupdate='apt update && apt upgrade'
 


When using sudo, use alias expansion (otherwise sudo ignores your aliases)

$alias sudo='sudo '


Bash only checks the first word of a command for an alias, any words after that are not checked. Note the trailing space after the value sudo.

6. cat


I find cat awkward to use, I find pr more intuitive and memorable.

$alias pr='cat'

7. jekyll


I use jekyll for my blog and shortening regular commands saves me a lot of time

$alias jekylls='bundle exec jekyll serve' 
$alias jekyllb='bundle exec jekyll build'
$alias jekylld='bundle exec jekyll serve --drafts'
$alias jekyllc='bundle exec jekyll clean'
 

8. security


You can also make not so safe commands unavailable

$alias dd='echo "dd command has been disabled"'
 

Conclusion


Whether you are a system administrator, a programmer, or an end user, there are certainly occasions where a simple (or perhaps not so simple) shell hacking can save you time and effort, or facilitate consistency and repeatability for some important task.


Even using an alias to change or shorten the name of a command you use often can have a significant effect.


You can find out more on alias and other commands on these resources:

  1. ” Bash Cookbook “ a book by by Carl Albing, JP Vossen, and Cameron Newham Beijing
  2. ” Bash aliases you cant live without “ by Seth Kenion
You have reached the end of the post , you can continue the discussion on twitter . I have also created a repo with my bash_aliases file, you can check it out on github.

If you have enjoyed what you just read, consider making a donation to help keep the lights up around here