/

Linux commands: alias

Linux commands: alias

In this article, we will explore the alias command, which is used to create shortcuts to other commands. This can be incredibly useful when you frequently use specific command options or combinations.

Let’s consider the ls command as an example. By default, it provides minimal information. However, if you use the -al option, it will display more comprehensive information, including the file modification date, size, owner, permissions, and hidden files (files starting with a .).

To create a new command, such as ll, which is an alias for ls -al, you can use the following command:

1
alias ll='ls -al'

Once you’ve created this alias, you can simply use ll as if it were a regular UNIX command. It will execute the ls -al command, providing the desired output.

To view the aliases currently defined in your terminal session, you can run the alias command without any options:

1
alias

The defined aliases will be listed in the output.

It’s important to note that aliases are only valid for the current terminal session. If you want to make them permanent, you need to add them to your shell configuration file. The specific file to edit depends on your shell, such as ~/.bashrc, ~/.profile, or ~/.bash_profile for the Bash shell.

When defining aliases with variables, be cautious with the use of quotes. Using double quotes will resolve the variable at the time of alias definition, while using single quotes will resolve it at the time of invocation. Here’s an example to illustrate the difference:

1
2
alias lsthis="ls $PWD"
alias lscurrent='ls $PWD'

In the above example, $PWD refers to the current folder within the shell. If you navigate to a new folder after defining these aliases, the behavior will vary. The lscurrent alias will list the files in the new folder, while the lsthis alias will still list the files in the folder where the alias was defined.

The alias command is compatible with Linux, macOS, WSL, and any UNIX environment.

tags: [“Linux commands”, “alias”, “command shortcuts”, “shell configuration”, “terminal session”]