Custom commands on Linux
Create custom Linux commands using the .bashrc file.
Linux allows many modifications to help and simplify its users' daily routines. One way to create custom commands on Linux is by modifying the .bashrc file.
The .bashrc is a script file that runs every time a new interactive Bash shell starts on Linux. It contains various settings for the current terminal session.
In addition to the existing settings, you can add new commands according to your needs. Some examples are:
-
Showing the project's Git branch name in the terminal;
-
Extracting an archive;
-
Simplifying Git commands.
Editing .bashrc
For custom commands to work, they must be added to the .bashrc file. This file is located in /home/<your-username>/.
Note: The commands below use
~to refer to your home directory regardless of the current directory..bashrcis loaded by interactive Bash sessions, not by every terminal or shell.
There are many ways to edit this file. One of the simplest is to open it in VS Code:
code ~/.bashrc
After modifying the file, you can use the following command to make the open terminal recognize the changes:
Note: If you close the terminal and open a new one, your changes will also be recognized. However, it is worth using source first because it will show if there is a problem with the code you added.
source ~/.bashrc
Take care not to change or remove existing lines unless you know what they do.
Creating abbreviations (aliases)
An alias avoids repeating the same command every time you need to use it. Instead of typing the entire command, you can create an abbreviation and then invoke the command using that abbreviation.
For example, the first alias below prints the computer's IP address when you type ip. It also uses xclip to copy the address to the clipboard.
Note: A line beginning with
#is a comment.
# alias to show the computer's IP address
alias ip="echo $(hostname -I | awk '{print $1}') | xclip -sel clip && echo $(hostname -I | awk '{print $1}')"
# aliases for listing files in a directory
alias ls='ls -CF --color=auto'
alias la='ls -ACF --color=auto'
alias ll='ls -ACFl --color=auto'
# alias for docker-compose
alias dc='docker-compose'
# use Python 3 and pip 3 by default
alias python='python3'
alias pip='pip3'
Git aliases
These aliases make common Git commands shorter:
alias ga='git add'
alias gl='git pull'
alias gp='git push'
alias gs='git status'
alias gstash='git stash'
alias glog='git log'
Creating constants
The script also allows you to create constants. The following example defines colors for customizing the terminal:
Note: The uppercase variants display the colors in bold.
# some colors
red="\[\033[0;31m\]"
RED="\[\033[1;31m\]"
green="\[\033[0;32m\]"
GREEN="\[\033[1;32m\]"
yellow="\[\033[0;33m\]"
YELLOW="\[\033[1;33m\]"
purple="\[\033[0;35m\]"
PURPLE="\[\033[1;35m\]"
white="\[\033[0;37m\]"
WHITE="\[\033[1;37m\]"
blue="\[\033[0;34m\]"
BLUE="\[\033[1;34m\]"
cyan="\[\033[0;36m\]"
CYAN="\[\033[1;36m\]"
NO_COLOR="\[\033[00m\]"
Creating functions
Functions are invoked in the terminal like aliases, but they can handle more complex commands and accept arguments.
Start a Python application
The code block below is an example of a function. It first clears the terminal, then changes the current directory to /python-flask-api-rest, opens VS Code in the current directory, activates the Python virtual environment, and starts the Python application:
start-api() {
clear
cd ~/projects/python-flask-api-rest/
code .
source venv/bin/activate && python app.py
}
Stop all containers running on the machine
docker-stop-all() {
docker stop $(docker ps -aq)
}
Download a file from a URL
This function takes two arguments: the filename to save and the download URL.
download() {
wget -O ~/Downloads/$1 "$2"
}
Extract an archive
This function takes one argument: the name of an archive in the current directory.
extract() {
if [ -f $1 ]; then
case $1 in
*.tar.bz2) tar xvjf $1 ;;
*.tar.gz) tar xvzf $1 ;;
*.bz2) bunzip2 $1 ;;
*.rar) unrar x $1 ;;
*.gz) gunzip $1 ;;
*.tar) tar xvf $1 ;;
*.tbz2) tar xvjf $1 ;;
*.tgz) tar xvzf $1 ;;
*.zip) unzip $1 ;;
*.Z) uncompress $1 ;;
*.7z) 7z x $1 ;;
*) echo "don't know how to extract '$1'..." ;;
esac
else
echo "'$1' is not a valid file!"
fi
}
Remove branches that are no longer in use
After updating remote references, this function attempts to remove local branches whose remote tracking branches have been deleted (gone status).
Note: This function does not affect remote branches. The
-Doption force-deletes local branches, including unmerged ones. Review the branches listed before running it.
git-clean() {
git fetch -p
for branch in $(git branch -vv | grep ': gone]' | awk '{print $1}'); do
git branch -D $branch
done
}
Show the branch name in the terminal
If you work with Git, a great customization is to display the current branch name in the terminal.
Note: The branch name will appear only when you are inside a Git project.
# show the branch name in the terminal
get-branch-name() {
branch=$(git branch 2>/dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/')
# -n => the length of STRING is nonzero
if [[ -n "$branch" ]]; then
echo -e "\033[1;37m(\033[01;33m$branch\033[1;37m)"
fi
}
# custom terminal with new colors and branch name if exists
PS1="▫️ $PURPLE\u$CYAN\w\$(get-branch-name)\n$WHITE>$NO_COLOR "
After adding this code to .bashrc, the terminal should look like this:

Author
Mathias Artur Schulz
Senior Software Engineer
Bachelor's degree in Computer Science and Postgraduate degree in Software Engineering
Email:mathias@schulz.net.br