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:
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.
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:
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:
Note: The ~ is an shell command alias for your home directory.
Type the following and save file:
alias dir=ls alias del=rm alias cls=clear
Sourcing Shell Aliases File:
Enter the following shell command in terminal as show below:
Note: The 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:
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:
Consequently, executing the dir command should now display files as shown below:
For more information enter the following manual page for the bash shell in the Raspberry Pi Terminal:
For a list of shell commands and how to use them please see my page:
Additional information on GNU coreutils online help can be found at: gnu.org.
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…