in Web and Tech, Work

Linux CLI: Bash Alias

Bash aliases are kind of shortcuts for working on the command line on Unix-like systems.

Use case scenario: regularly typing commands with lengthy parameters, like so:

$sudo -- sh -c "/root/bin/chk_disk && dnf update'

This can be assigned an alias so it needs not be a lengthy typing each time by entering the following on the command line:

$alias update='sudo -- sh -c "/root/bin/chk_disk && dnf update'

However, this is assignment is temporary and will be lost after the terminal is closed. To make the assignment more permanent, a bash alias would do well.

Steps to create a permanent Bash alias:
1. Open the Terminal app

2. Edit ~/.bash_aliases or ~/.bashrc file using a text editor like Vi or Nano:

$vi ~/.bash_aliases

3. Append your bash alias assignment into the last line of the file, if the file already exists, as you would type it on the command line.

4. Save and close the file.

5. Activate alias by entering the following on the command line:

$source ~/.bash_aliases

Note that ~/.bash_aliases file only works if the following line is present in the ~/.bashrc file:

if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi

If those lines are missing in the ~/.bashrc file, they would need to be appended at the end of the ~/.bashrc, using a text editor.

Write a Comment

Comment