Alias Shell Command – Raspberry Pi Terminal

Alias Shell Command
Raspberry Pi Terminal alias bash shell command.

Introduction to Alias Shell Command:

The Alias shell command in the Raspberry Pi Terminal can be used to add shortcuts to your most used shell commands. In addition, Alias can be used for emulating some of your favorite DOS commands like DIR or DEL.

After years of working with different terminal and shell scripting environments it can get confusing which shell commands to use. Equally important maybe you have an old favorite shell command with specific parameters that provide the output you like.

Therefore, would not it be sweet if there was a solution to this problem? Well, my brave friends, there is a way you can have your Raspberry Pi and eat a “byte” out of DOS and Windows too.

How to use Alias:

For example, on the Raspberry Pi Desktop, open the Terminal application as seen below:

Open Raspberry Pi Terminal from Desktop.

Raspberry Pi Terminal:

As a result, the terminal window will appear with a command prompt. Consequently, the command prompt interface provides the ability to type and enter names of programs for execution. Accordingly, the results of program execution are displayed as output in the Raspberry Pi terminal window. In fact, both the Terminal and ssh are command line interfaces. In contrast, the Raspberry Pi Desktop is a graphical user interface (GUI) for managing files and execution of programs.

Shell Commands:

Generally speaking, several programs included in the Operating System are Shell Commands. Shell commands are used in shell scripting and batch jobs. Additionally, shell commands are entered at the command prompt in the Raspberry Pi Terminal to perform various tasks. In fact, for more information on Shell Commands, follow the link below:

Raspberry Pi Terminal – Shell Commands – Pi With Vic

alias Shell Command Example:

Notably, the alias shell command creates alternative names for commands that can be shorter or longer. In addition, each command can be spelled differently than the original shell command.

For example, an alias named dir used for the ls command can look like the following:

dir = ls

Then once the alias is active entering dir in the Raspberry Pi Terminal Shell would output the same results as typing ls.

Shell Configuration File for Aliases:

Open a new Terminal window from the Raspberry Pi Desktop. Edit the shell command aliases file using nano, or your favorite text editor as shown below:

nano ~/.bash_aliases

Note: The ~ is an shell command alias for your home directory.

Using nano or your favorite text editor to add or modify aliases for the bash shell.

Type the following and save file:

alias dir=ls
alias del=rm
alias cls=clear
Editing aliases bash shell configuration file for adding shortcuts and DOS like commands.

Sourcing Shell Aliases File:

Enter the following shell command in terminal as show below:

. ~/.bash_aliases

Or

source ~/.bash_aliases

NoteThe period in this case is an alias for source.

As a result of executing the source shell command as shown above, new alias commands entered are now available to use. Alternatively closing the Terminal and starting a new Terminal yields the same results.

Whenever the Raspberry Pi Terminal is opened your bash shell profile is setup. Part of this setup process is sourcing and loading your aliases configuration file. Each Alias shell command is executed in the order you entered them.

Generally speaking, it is possible to re-type the alias commands each time the Raspberry Pi Terminal is opened. However, this would quickly get old faster than watching paint dry. For this reason, when aliases are entered in the configuration file you automate this process.

This is what I like to call “Auto-Magic“.

Executing Your Custom Shell Commands:

Henceforth new alias shell commands may entered such as the dir command shown below:

dir
Testing new alias for DOS like dir shell command using ls.

Pretty cool eh?

Parameters for shell commands being aliased with parameters must be included within double quotes as shown below:

alias dir=”ls -al”

Modify the ~/.bash_aliases file and change the alias for dir as shown above and re-source like:

source ~/.bash_aliases

Consequently, executing the dir command should now display files as shown below:

dir
Using alias shell command ls for DOS like Dir.

More Information:

For more information enter the following manual page for the bash shell in the Raspberry Pi Terminal:

man bash

For a list of shell commands and how to use them please see my page:

Raspberry Pi Terminal – Shell Commands – Pi With Vic

Additional information on GNU coreutils online help can be found at: gnu.org.

Summary:

But wait, there is still more…

Remember you also now have aliases for rm and clear so you may use del and cls too!

Now you have the power to create new Raspberry Pi Terminal Shell Commands and make shortcuts for the ones you use often. As I often say, Life is too short so type less…

When you come up with some cool aliases come back and share…

Have fun and always remember:

Do not underestimate the power of the Raspberry Pi…

Regards,

Vic