Linux offers powerful ways to customize and optimize the command line experience. One such method is using the alias command to create shortcuts for frequently used commands. Aliases allow users to define custom commands, making repetitive tasks more efficient.
This article explores how to create, use, and manage aliases, as well as how to check if a command name is already in use.
Executing Multiple Commands in One Line
Linux allows users to combine multiple commands in a single line using a semicolon (;):
command1; command2; command3
For example, the following changes the directory to /usr, lists its contents, and returns to the original directory:
cd /usr; ls; cd -
This technique is useful when creating aliases for multi-step operations.
Checking If a Command Name Exists
Before creating an alias, it's best to check if the command name is already in use using type:
type command_name
For example, checking test:
type test
Output:
bash
test is a shell builtin
Since test is already assigned to a shell builtin, we choose another name, such as foo:
type foo
Output:
bash
bash: type: foo: not found
Since foo is not already used, we can safely assign an alias to it.
Creating an Alias
The syntax for creating an alias is:
alias name='command'
Where:
name is the alias name.
command is the sequence of commands to execute.
Example: Assigning an Alias to foo
alias foo='cd /usr; ls; cd -'
Now, running foo will execute the three commands:
foo
Checking Defined Aliases
To verify a specific alias, use type:
type foo
Output:
bash
foo is aliased to `cd /usr; ls; cd -'
To see all defined aliases, run:
alias
Common aliases (Fedora default):
alias l.='ls -d .* --color=tty' alias ll='ls -l --color=tty' alias ls='ls --color=tty'
These customize behavior, such as enabling colored output for ls.
Removing an Alias
To delete an alias, use unalias:
unalias alias_name
For example, removing foo:
unalias foo type foo
Output:
bash
bash: type: foo: not found
Now, foo no longer exists.
Persisting Aliases Across Sessions
Aliases defined in the command line exist only for the current session. To make an alias permanent, add it to ~/.bashrc or ~/.bash_aliases, so it is loaded at every login.
Conclusion
The alias command allows users to create custom shortcuts, improving efficiency in Linux.