How to Define Bash Aliases?

Coşqun Həsənov
2 min readFeb 25, 2023

--

bash aliases

Syntax:
alias [option] [name]=’[value]’

[option]: Enables the command to list all active aliases.

[name]: Specifies the name of the new shortcut for a command. A name is a user-defined string that does not include special characters, ‘alias’, or ‘unalias’, which are names that cannot be used.

[value]: Declares the command that the alias refers to. Options, arguments, and variables are additional components of commands. A path to a script you want to run can also be a value.

List all alias commands already defined:

ubuntu:~# alias
alias l='ls -CF'
alias la='ls -A'
alias ll='ls -alF'
alias ls='ls - color=auto'

Call ls command as an alias:

ubuntu:~# ls
snap ubuntu-22.10-live-server-amd64.iso

ubuntu:~# ls /
bin dev lib libx32 mnt root snap sys var
boot etc lib32 lost+found opt run srv tmp home lib64 media proc sbin usr

Call \ls the original command, not as an alias:

ubuntu:~# \ls
snap ubuntu-22.10-live-server-amd64.iso

ubuntu:~# \ls /
bin dev lib libx32 mnt root snap sys var
boot etc lib32 lost+found opt run srv tmp home lib64 media proc sbin usr

More additional custom aliases commands:

ubuntu:~# alias c="clear"
ubuntu:~# alias ports="ss -tunap"
ubuntu:~# alias sroot="sudo su"
ubuntu:~# alias slt="ls -hSF  --size -1"

Open another tab test defined c command:

ubuntu:~# c
c: command not found

Add this aliases for persistent .bashrc/.profile, then use source command:

ubuntu:~# echo "slt='ls -hSF --size -1'" >> .bashrc

ubuntu:~# source .bashrc

ubuntu:~# slt
total 1.6G
1.6G ubuntu-22.10-live-server-amd64.iso*

Finally, to remove the custom aliases. For instance, aliases stored persistent in .bashrc/.profile use text editor then remove manually.

ubuntu:~# alias -p
alias c='clear'
...

ubuntu:~# unalias c

ubuntu:~# alias -p
alias ports='ss -tunap'
...

Conclusion

You should be able to use the alias command to create and manage aliases on your Linux system after reading this article. I hope you will benefit from this.

--

--