The alias
command in Unix-like operating systems, including Linux, allows you to create shortcuts or alternate names for other commands. It’s a way to define your own custom commands by assigning a specific string to represent a longer or more complex command with various options.
alias Command in Linux
The basic syntax of the alias
command is as follows:
alias new_command='original_command'
Here, new_command
is the alias you want to create, and original_command
is the command you want to substitute with the alias. The alias should be enclosed in single quotes ('
) to ensure that any spaces or special characters are interpreted correctly.
Examples of Using the alias
Command:
Creating a Simple Alias:
alias ll='ls -l'
After executing this command, whenever you run ll
, it will be equivalent to running ls -l
.
Creating an Alias with Options:
alias lla='ls -la'
Now, running lla
will list all files (including hidden ones) in long format.
Creating an Alias with Arguments:
alias grep='grep --color=auto'
This alias adds color highlighting to the grep
command’s output. Now, using grep
with the alias will produce colored results.
Using Multiple Commands in an Alias:
alias update='sudo apt update && sudo apt upgrade'
The update
alias will run both sudo apt update
and sudo apt upgrade
commands when executed.
Viewing Existing Aliases:
alias
Running the alias command without arguments will display a list of all currently defined aliases.
Removing an Alias:
unalias ll
This command removes the previously created ll alias.
Remember that aliases are only active for the duration of the shell session in which they are created. If you want to make an alias persistent across sessions, you can add the alias definition to your shell’s configuration file (e.g., .bashrc, .zshrc), which is loaded every time you start a new shell session.
Aliases can be a convenient way to save time and simplify commonly used commands, but they should be used judiciously to avoid confusion and maintain readability in scripts and shared environments