Creating and Using Bash Alias
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 guide covers the Bash alias syntax, how to create temporary and permanent aliases, and how to use aliases with functions, arguments, and conditional logic inside scripts. By the end, you'll have reusable aliases loaded automatically in every new shell session.
1. The Bash Alias Syntax
The following is a basic Bash alias syntax:
alias alias-name="command" 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.
2. Create Bash Aliases
1. Create a sample alias application, such as hello-world, that runs the echo command:
$ alias hello-world='echo "Hello, World!"' 2. Run the hello-world command to display the result:
$ hello-world Output:
Hello, World! 3. Create another list-files alias to substitute the ls command:
$ alias list-files='ls' 4. Run the list-files command:
$ list-files Verify that the command lists all files in your working directory.
Create Permanent Bash Aliases
Bash aliases are temporary in a shell environment unless you save them in the .bashrc file.
1. Switch to your user's home directory:
$ cd 2. Create a new myaliases file using a text editor like nano:
$ nano myaliases 3. Add the following directives to the myaliases file:
alias hello-world='echo' alias list-files='ls' alias newfile='touch' 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.
4. Open the .bashrc file:
$ nano .bashrc 5. Add the following directives at the end of the file:
if [ -f myaliases ]; then . myaliases fi 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.
6. Apply the .bashrc changes to your shell environment:
$ source .bashrc 7. Run the newfile alias command and specify sample-file as an argument to create a new file:
$ newfile sample-file 8. List files using the list-files alias in your working directory and verify that sample-file is available:
$ list-files Output:
... sample-file .... 9. Run the hello-world alias and specify Greetings from the team as an option:
$ hello-world Greetings from the team Output:
Greetings from the team 3. Use Bash Aliases with Functions and Arguments
You can combine aliases with command arguments and functions in Bash scripts to perform specific tasks, such as creating and managing files.
1. Create a new bashalias.sh script:
$ nano bashalias.sh 2. Add the following contents to the bashalias.sh file:
#!/bin/bash myfunction () { mkdir -p "$1" && cd "$1" touch hello_bash } myfunction $1 echo "New Directory and File Created." 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.
3. Run the script using Bash and specify test_directory as the command option:
$ bash bashalias.sh test_directory 4. List the files in the test_directory:
$ ls test_directory/ Output:
hello_bash 4. Use Bash Alias in Bash Script
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.
1. Create a new advanced-alias.sh script:
$ nano advanced-alias.sh 2. Add the following contents to the advanced-alias.sh file:
#!/bin/bash shopt -s expand_aliases alias sysinfo='uname -a && lscpu && free -h' alias lamp='sudo apt update && sudo apt install apache mysql php' alias lemp='sudo apt update && sudo apt install nginx mysql php' alias mean='sudo apt update && sudo apt install -y mongodb npm && sudo npm install -g @angular/cli && sudo npm' echo "Select an option:" echo "1. Show System Information" echo "2. Install LAMP" echo "3. Install LEMP" echo "4. Install MEAN" read -p "Enter your selection (1-4): " selection if [ "$selection" -eq 1 ]; then echo -e "\nGathering system information..." sysinfo elif [ "$selection" -eq 2 ]; then echo -e "\n Installing the LAMP Stack" lamp elif [ "$selection" -eq 3 ]; then echo -e "\n Installing LEMP" lemp elif [ "$selection" -eq 4 ]; then echo -e "\nInstalling MEAN:" mean else echo "Invalid selection. Please select a valid option." exit 1 fi 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.
3. Run the script using Bash:
$ bash advanced-alias.sh 4. Enter a selection such as 2 to run the lamp alias command:
Select an option: 1. Show System Information 2. Install LAMP 3. Install LEMP 4. Install MEAN Enter your selection (1-4): 2 Output:
Installing the LAMP Stack ................................. Next Steps
- Organize related aliases into separate files and source them conditionally for different projects.
- Share a team-wide aliases file via a dotfiles repo for consistent shortcuts across machines.
- Combine aliases with Bash functions for more complex argument handling.
- Explore
completeandcompgento add tab-completion to custom aliases.
For the full guide with additional tips, visit the original article on Vultr Docs.
原文:https://www.a8q9.cn/en/article/creating-and-using-bash-alias-78bf5b.html