Create your own Linux commands using aliases and Bash shell functions. Tame repetitive tasks, truncate long-winded processes, and configure standard commands with the options you always use and struggle to remember.
Aliases and shell scripts are powerful techniques in Linux and Unix-like operating systems that allow you to hone your command line experience to be just what you want. You can define your own commands suited to your specific needs, and to ease the burden of repetitive tasks.
Aliases and shell scripts do the same sort of job. They allow you to define—and name—a set of Bash shell functionality that is then callable by the name you’ve given to it. Typing the name is easier and more convenient than having to type out all of the steps or commands each time you want to use them.
The difference between an alias and a script is one of complexity and scale. Scripts are better at holding longer and more elaborate chunks of code. Aliases are perfect for holding shorter, more succinct, sets of commands.
Predefined Aliases
Some aliases are predefined for you. To see the list of aliases that are defined in your system, use the alias command with no parameters:
alias
These are the aliases that are defined on the Ubuntu test machine this article was researched on. If I’d defined any custom aliases, these would show up in this list too.
At the top of the listing, there’s a complicated-looking alias called alert
. We’ll come to that in a moment. There’s a bunch of different aliases for the ls
command, and there’s a group of aliases that provide color output to the grep
family of commands. For example, with these aliases defined, whenever you type:
grep
The system will interpret it as:
grep --color=auto
This shows an important point with aliases. They can have the same name as existing commands. They can even contain the original command within themselves.
Here is the definition of the grep
alias.
alias grep='grep --color=auto'
- The
alias
command is used to define an alias. - The name of the alias is given next. In this example it is
grep
. - The equals sign connects the name of the alias to the body of the alias. For all but very simple aliases, the body of the alias is enclosed within single quote marks
'
. - The body of the alias is the section which is executed when the alias is used on the command line.
The body of this alias simply calls the grep
command with the --color=auto
option.
The alert Alias
As a quick aside, and so that you know what it does, the alert
alias is used to let you know when a command has finished. It also indicates whether the command completed successfully or not. It provides a graphical system alert at the top of the screen.
Here’s a simple example. The sleep
command will sleep for five seconds., The alert
alias will then be called. The alias checks the response from the previous command. It extracts the last command from the history file. It determines whether the command completed successfully or not. It then presents the results in a system alert.
If the command completed as expected, the icon in the system alert is a small terminal window. If the command returned an error code, the icon in the system alert is a red error icon.
sleep 5; alert
After five seconds, we see this system alert:
The icon is a small terminal window, meaning everything went well. Let’s try that again with a command that we know will fail:
DoomedToFail; alert
Our system alert now has an error icon.
Defining a Trivial Alias
As we’ve seen, to define an alias, we use the alias
command.
We’re going to create a pseudonym for the clear
command. Our alias will be called cls
and it will call the clear
command.
Our alias definition is so trivial that it doesn’t warrant being wrapped in single quote marks. If the body of the alias is any more complex than this, or if it contains spaces, wrap it in single quotes. We’ll define the alias, use ls
to put some output in the terminal window and then use our new alias cls
to clear the screen.
alias cls=clear
ls -l
cls
The screen is cleared. Success, albeit shortlived. The alias will survive only as long as this terminal window remains open. Once the window is closed, the alias will vanish.
So how do we make our aliases permanent?
The .bashrc File and Aliases
You might be wondering where the pre-packaged aliases are defined. it is in the “.bashrc” file in your home folder. This file is read, and the commands inside it executed whenever you start an interactive shell. That is, when you open a terminal window.
Type the following command in your home folder to see the contents of the “.bashrc” file with syntax highlighting.
gedit .bashrc
This will launch the gedit
editor with the “.bashrc” file loaded into it.
The highlighted areas show two areas where aliases are defined.
Scrolling through the document will reveal two other sections related to aliases:
The first of these is the definition of the alert
alias. The second is an if
statement. It translates to, “if the file “.bash_aliases” exists, read it in.”
If you only have a few aliases that you wish to define, you might put them in your “.bashrc” file. Tuck them in below the section containing the ls
aliases.
If you are going to create a lot of aliases, or you just like the idea of having your aliases encapsulated within their own file, you can define them in your “.bash_aliases” file. One advantage of creating them in your “.bash_aliases” file is you can’t accidentally change any of the settings in the “.bashrc” file. Another advantage is your aliases are easily copied to new systems because they are totally divorced from the “.bashrc” file.
Storing Aliases in the .bash_aliases File
The “.bash_aliases” file will not exist until you create it. You can create the file with this command:
touch .bash_aliases
Let’s edit the file and add a few aliases to it. This command will open the “.bash_aliases” file in the gedit
editor.
gedit .bash_aliases
We’ve added three aliases. The first is our cls
alias which we used earlier. The next is called h.
and is a shorthand way of calling the history
command.
The third alias is called ftc
. This stands for “file type count.”
This alias is more involved, so it is wrapped in single quote marks. It uses a chain of commands linked together by pipes. It produces a sorted list of the different file extensions and directory names, with a count for each list entry.
RELATED: How to Use Pipes on Linux
When we have saved the “.bash_aliases” file, we might expect our aliases to be live and accessible. That’s not the case. The file has to be read in by the Bash shell before the alias definitions are live. This is done whenever an interactive shell is opened.
We can also use the Bash shell built-in .
to read and execute the commands in a file. Because our “.bash_alias” file is read in when “.bashrc” is processed, we ought to perform our test by calling “.bashrc”. That way we can check that the “.bash_alias” file is called from “.bashrc” and that our aliases are alive and well.
The commands we’ve used are:
gedit .bash_alias
To edit the “.bash_alias” file.
. .bashrc
This will read in and execute the commands within “.bashrc”, which will call “.bash_alias”.
ftc
This will call the ftc
alias.
Our alias responds which means Bash has read in both “.bashrc” and “.bash_aliases”, and our new aliases are now live.
You can now go ahead and add new aliases to the “.bash_aliases” file as they occur to you. If you find yourself doing things more than once or twice, consider making an alias for it.
Removing Aliases
There is a command to remove aliases so that BAsh doesn’t recognize them nor respond to them. Refreshingly forthright, the command is called unalias
.
To use it, give the name of the alias you wish to have Bash forget. To make Bash forget our ftc
alias, use unalias
like this:
unalias ftc
You can use unalias
to remove aliases you have defined and any of the predefined aliases.
To remove all of the aliases from your system, use the -a
(all) option:
unalias -a
Bash’s loss of memory will not be permanent, though. The next time you open a terminal window, the “forgotten” aliases will be back. To truly wipe them out you need to remove them from your “.bashrc” and “.bash_alias” files.
If you think you’d ever like to have them back, don’t delete them from your “.bashrc” file. Instead, comment them out by adding a hash #
to the start of each alias
line. To make your “.bash_alias” file ineffective, rename it. If your “.bashrc” file can’t see it, it won’t read it in. Reversing these steps to reinstate your aliases is a trivial matter.
Shell Functions
Lika aliases, Bash shell functions can be defined within the “.bashrc” file, but it is often neater to put them in their own definitions file. We’ll call it “.bash_functions”, following the convention used for the “.bash_aliases” file.
That means we need to tell the “.bashrc” file to read in our definitions. We can copy and amend the snippet of code that reads in the “.bash_aliases” file. Launch gedit
and load the “.bashrc” file with this command:
gedit .bashrc
You need to add the highlighted section shown below.
You can highlight the alias section and press Ctrl+C
and then move to where you’d like the new section and press Ctrl+V
to paste a copy of the text. Then all you need to do is change the two places where it says “.bash_aliases” to “.bash_functions.”
We can save those changes and close gedit
.
Now we are going to create and edit the “.bash_functions” file, and put a function definition in it.
touch .bash_functions
gedit .bash_functions
This will open the empty “.bash_functions” file in gedit
.
We’re going to add a simple function called up
. up
will take a single command line parameter, which is a digit. up
will then call cd ..
that number of times. So, if you used the command
up 2
up
would call cd ..
twice and would move up two levels in the directory tree.
There are different ways to define a function. Here’s one:
function up() {
The word function
is optional. If you’re a traditionalist, use it, if you can’t be bothered typing it in, leave it out.
Here’s our entire function in gedit
:
function up() {
This marks the start of our function definition, and it names the function up
.
levels=$1
This creates a variable called levels
and sets it to the value of the first parameter. This parameter is going to be a digit provided by the user when they call the function. The $1
means “first command line parameter.”
while [ "$levels" -gt "0" ]; do
We then enter a loop. This translates as “while the value of levels
is greater than zero, do what is contained in the body of the loop.”
Inside the body of the loop, we have two commands. They are:
cd ..
Move up a level in the directory tree.
levels=$(($levels - 1))
Set levels
to a new value, which is one less than its current value.
We then go back to the top of the loop, the comparison between the value of levels
and zero is made once more. If levels
is more than zero, the body of the loop is executed again. If it is not greater than zero, the loop is finished, and we drop through to the done
statement, and the function is over.
Save these changes and close gedit
.
We’ll read in and execute the commands in “.bashrc” which should read in and execute the commands in our “.bash_functions” file.
. .bashrc
We can test the function by moving to some location in the directory tree and using up
to move back to a “higher” point in the directory tree.
cd ./work/backup/
up 2
The function works. We’re moved two directory levels higher in the tree.
Keeping Track With type
As you build up a suite of aliases and a library of functions, it can become difficult to remember whether a particular command is an alias or a function. You can use the type
command to remind you. The cool thing here is that you also get to see the definition.
Let’s use type
on our ftc
alias and our up
function.
type ftc
type up
We get a very useful reminder of what type of command each one is, together with their definitions.
Start Collecting
Aliases and functions can speed up your use of the command line tremendously. They can shorten command sequences, and they let you bake-in the options you always use with standard commands.
Each time you see a nifty one-liner or useful function, you can adapt and personalize it, and then add it to your “.bash_aliases” or “.bash_functions” files.