Aliases in Bash

Introduction

Aliases are a way to replace complex commands with shorter ones.

They will be valid only for the system in which they are created.

There are a number of aliases that are often present in the system by default. Common examples are replacing ls -la or a similar command with ll, replacing vi with vim, and so on.

Aliases

If you never use the ls command, but often run ls -a the thought may have occurred to you - cannot ls be configured somehow so that it always runs ls -a

This is «not hard» to do, and such renamings are called aliases. With their help you can not only override existing commands, but also invent completely new ones.

export EDITOR=nano export PATH=$PATH~/new_bin alias ls="ls -la"

To get a list of the existing alias you can use the command

alias

alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"' alias egrep='egrep --color=auto' alias fgrep='fgrep --color=auto' alias grep='grep --color=auto' alias l='ls -CF' alias la='ls -A' alias ll='ls -alF' alias ls='ls --color=auto'

If you added an alias to .profile and it does not work - try restarting the terminal. If that does not help, add it to .bashrc . If that did not help - create the file .bash_aliases and add it there.

For those who write code in Python it is convenient to make the alias python - python3 to use Python 3 and not specify 3 everywhere.

alias python="python3" alias pip="pip3"

If Python is installed manually , for example to use a newer version, it is sometimes useful to specify the path to it in the new alias.

alias python="/usr/local/bin/python3.11"

Article author: Andrei Olegovich

Banner image