Alias command in Unix is used to create alias for a command, lets say if you are using ‘ls -ltr’ frequently in your work and you want to replace this long command with the shorter version such as ‘l’, you can simply create alias for ‘ls -ltr’ as just ‘l’.
Shell aliases are an easy way to create new commands or to wrap existing commands with code of your own. Aliases
somewhat overlap with shell functions, which are however more different and should therefore often be preferred.
We will discuss the aliases step by step in the following sections and learn with alias Unix command examples below.
Create an Alias in Unix
Below is the alias Unix syntax.
alias word='command'Invoking word will run command. Any arguments supplied to the alias are simply appended to the target of the alias:
alias myAlias='some command --with --options' myAlias foo bar bazThe shell will then execute:
some command --with --options foo bar bazTo include multiple commands in the same alias, you can string them together with &&. For example:
alias print_things='echo "foo" && echo "bar" && echo "baz"'Add alias in .bashrc
These alias commands should be added in.bashrc file so that you can access these from next login. Please note that once add the alias in .bashrc file, you need to log off and log in to activate the commands.
Bypass an alias
Sometimes you may want to bypass an alias temporarily, without disabling it permanently. To work with an example,
consider this alias:
alias ls='ls -ltr'And let’s say you want to use the ls command without disabling the alias. You have below options:
- Use the ‘command’ builtin: command ls
- Use the full path of the command: /bin/ls
- Add a \ anywhere in the command name, for example, \ls, or l\s
- Quote the command: “ls” or ‘ls’
Remove an alias in Unix
To remove an existing alias, use:
unalias {alias_name}Example:
# create an alias $ alias now='date'# preview the alias $ now Thu Aug 21 16:11:23 EST 2019# remove the alias $ unalias now# test if removed $ now -bash: now: command not foundList all Aliases
alias -pwill list all the current aliases.