Bash Aliases
Bash aliases allow you to define shortcut names for often-used commands. They can save a lot of typing, especially for long command names.
Here's how you define a bash alias:
alias alias_name='command'
For example, to define an alias for the ls
command:
alias ll='ls -l'
Now you can just type ll
instead of ls -l
to get a long listing.
You can make aliases for any command. Some common examples are:
alias la='ls -a' # List all files including hidden ones
alias grep='grep --color=auto' # Color grep output
alias c='clear' # Clear the terminal
Aliases are stored in your .bashrc or .bash_profile file. So they are available every time you open a new terminal.
You can also define aliases with arguments. For example:
alias l='ls -l $1'
Now you can use:
l
to list the current directoryl foo
to list thefoo
directory
Use-Cases
Here are some interesting use cases where command aliases make sense:
For frequently used long commands: If you have some commands that you use frequently but have long names, aliases can make them easier to type. This saves time and reduces typos.
For commands with lots of options: If a command has a lot of options you often use together, an alias can group those options into a single name. This cleans up your commands.
For piping multiple commands: If you often pipe multiple commands together, you can alias the whole chain. This makes the full pipeline easier to execute.
For debugging purposes: You can define aliases for debugging commands you often use. This makes it easy to enable or disable those debugging aliases.
For custom commands: Aliases let you define your own custom commands by wrapping existing ones. This can simplify your workflow.
To avoid typos: Since aliases are shorter, they reduce the chances of typos when executing commands. This improves reliability.
For readability: Aliases that have meaningful names can make your commands more readable and self-documenting. This improves the maintainability of your scripts.
So in general, aliases are useful anytime you have commands that:
Are long
Have many options
Are used frequently
Are part of multi-command pipelines
Are custom commands you've defined
Tricks
Here are some tricks for using command aliases with pipelines and string interpolation:
- Alias a full pipeline:
alias longop='grep -E -i "pattern" * | sort | uniq'
Now you can run the full 3 command pipeline with: longop
- Pass arguments with $1, $2 etc:
alias grepall='grep -E -i $1 * | sort | uniq'
Now you can run: grepall "pattern"
- Use double quotes for string interpolation:
alias grepdir="grep -E -i $1 */$2"
Now you can run: grepdir "pattern" "code"
# Searches all 'code' subdirectories
- Pass multiple arguments with $*:
alias cpmany="cp $* /backup"
Now you can run: cpmany file1 file2 file3
# Copies all files to /backup
- Combine string interpolation and arguments:
alias grepdir="grep -E -i $1 */"$2"/*"
Now you can run: grepdir "pattern" "code"
# Searches all subdirectories of code dir
So in summary, aliases let you:
Define full pipelines
Pass arguments with $1, $2 etc
Use string interpolation with " "
Access multiple arguments with $*
Combine string interpolation and arguments
This allows you to create very powerful and flexible aliases!
Disclaim: This article is created by Rix (HashNode AI tool) with my help.
Subscribe to my newsletter
Read articles from Elucian Moise directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Elucian Moise
Elucian Moise
Software engineer instructor, software developer and community leader. Computer enthusiast and experienced programmer. Born in Romania, living in US.