
A Bash alias is a shortcut for substituting long commands with shorter commands to save time and avoid repetitive typing. In a shell environment, you should store Bash aliases in the .bashrc file under the user's home directory.
This article explains how to create and use Bash aliases.
The following is a basic Bash alias syntax:
In the above command:
alias: Creates a new alias command.alias-name: Specifies the alias name.command: Sets the command that you want to substitute. Enclose the command using single or double quotes if the command includes spaces or special characters to prevent shell interpretation.Follow the steps below to create and use Bash aliases in a shell environment.
Create a sample alias application, such as hello-world that runs the echo command.
Run the hello-world command to display the result.
Output:
Create another list-files alias to substitute the ls command.
Run the list-files command.
Verify that the command lists all files in your working directory.
Bash aliases are temporary in a shell environment unless you save them in the .bashrc file. Follow the steps below to make the Bash aliases permanent.
Switch to your user's home directory.
Create a new myaliases file using a text editor like nano.
Add the following directives to the myaliases file.
Save and close the file.
The hello-world, list-files, and newfile aliases in the above configuration execute the echo, ls, and touch commands respectively.
Open the .bashrc file.
Add the following directives at the end of the file.
Save and close the file.
The above if-then statement finds the myaliases file in the user's home directory and runs it to load all aliases in the .bashrc file.
Apply the .bashrc changes to your shell environment.
Run the newfile alias command and specify sample-file as an argument to create a new file.
List files using the list-file alias in your working directory and verify that sample-file is available.
Output:
Run the hello-world alias and specify Greetings from Vultr as an option.
Output:
You can combine aliases with command arguments and functions in Bash scripts to perform specific tasks, such as creating and managing files. Follow the steps below.
Create a new bashalias.sh script.
Add the following contents to the bashalias.sh file.
Save and close the file.
In the above script the myfunction function works as an alias for the mkdir, cd, and touch commands to create a new directory. The function then switches to the directory and creates a new hello_bash file. You can convert myfunction to a global alias command by appending it to the .bashrc file.
Run the script using Bash and specify test_directory as the command option.
List the files in the test_directory.
Output:
You can use Bash aliases in scripts to shorten long commands and allow code reusability with other Bash features such as conditional statements and loops. Follow the steps below to use aliases in a Bash script to validate and run specific tasks.
Create a new advanced-alias.sh script.
Add the following contents to the advanced-alias.sh file.
Save and close the file.
The shopt -s expand_aliases command in the above script enables alias expansion while the lamp, lemp, and mean Bash aliases shorten the full installation commands to install specific applications. The selection variable stores the user input and uses the if-elif conditional statement to run the Bash alias commands.
Run the script using Bash.
Enter a selection such as 2 to run the lamp alias command.
Output:
You have used aliases in a Bash shell environment to create reusable commands. The bash alias command allows you to run multiple commands, utilizing advanced Bash features such as conditional statements and loops to perform specific tasks.
0 Comments
Be the first to comment and share your perspective with the community.