Bash Aliases

Elucian MoiseElucian Moise
3 min read

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 directory

  • l foo to list thefoo directory


Use-Cases

Here are some interesting use cases where command aliases make sense:

  1. 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.

  2. 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.

  3. 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.

  4. For debugging purposes: You can define aliases for debugging commands you often use. This makes it easy to enable or disable those debugging aliases.

  5. For custom commands: Aliases let you define your own custom commands by wrapping existing ones. This can simplify your workflow.

  6. To avoid typos: Since aliases are shorter, they reduce the chances of typos when executing commands. This improves reliability.

  7. 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:

  1. Alias a full pipeline:
alias longop='grep -E -i "pattern" * | sort | uniq'

Now you can run the full 3 command pipeline with: longop

  1. Pass arguments with $1, $2 etc:
alias grepall='grep -E -i $1 * | sort | uniq'

Now you can run: grepall "pattern"

  1. Use double quotes for string interpolation:
alias grepdir="grep -E -i $1 */$2"

Now you can run: grepdir "pattern" "code" # Searches all 'code' subdirectories

  1. Pass multiple arguments with $*:
alias cpmany="cp $* /backup"

Now you can run: cpmany file1 file2 file3 # Copies all files to /backup

  1. 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.

0
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.