A tutorial on creating an alias in Bash Linux Terminal

Do you often write long commands at the station? Or often looking at the bash registry because you forgot too long commands? If yes, then you will find bash aliases very helpful.

Bash Alias ​​allows you to assign easy-to-remember shortcut commands to longer commands.

Bash aliases are basically shortcuts that can help you from having to remember long commands and save time when working on the command line.

For example, to extract files tar , We use the command tar -xvfz namaarchive.tar.gz. , However, because we created an alias in bash to keep it simple tar -xvfz, We’re just using tgz namaarchive.tar.gz

This article demonstrates how to create a bash alias to be more productive at the command line.

Create an alias in bash

It is very easy to create an alias in bash. The syntax is as follows:

alias alias_name="nama_perintah"

To create a new bash alias start typing a keyword alias. Then mention the alias followed by the equal sign and the command you want to run when writing the alias. Orders must be enclosed in quotation marks and without spaces around the equal signs. Each pseudonym should be announced on a new line.

Command ls It is perhaps one of the most commonly used commands on the Linux command line. I usually use this command with options -la To show all files and directories including hidden ones in long list format.

Let’s create a simple alias bash ll Which would be the shortcut to command ls -la. To do this, type open a terminal and type:

alias ll="ls -la"

Now in writing case ll On the console, you will get the same result by typing ls -la.

Nick name ​​ll It will only be available in the current shell session. If you exit the session or open a new session from another station, then alias The one you just created will not be available.

See also  How To Install PHP 8 on Arch Linux | Manjaro

To manufacture alias This is permanent, we must declare it in the file~/.bash_profile or ~/.bashrc. To make it permanent, open it ~/.bashrc In your text editor:

nano ~/.bashrc

And add your nickname:

# Alias
# alias nama_alias="perintah"

# Untuk Perintah ;s
alias ll="ls -la"

# Menampilkan Alamat IP Public
alias myip='curl ipinfo.io/ip'

You should name the alias in a way that is easy to remember. It is also recommended that you add comments for your future reference.

When done, save and close the file. Make the alias available on your current session by typing:

source ~/.bash_profile

As you can see, creating a bash alias is simple, fast and very easy.

If you want to keep it .bashrc Keep it updated By default, you can save the alias in a separate file. It includes some distributions like Ubuntu and Debian .bash_aliasesIts source ~/.bashrc.

Create Bash Aliases Using Media (Bash Functions)

Sometimes you may need to create an alias that accepts one or more arguments, and this is where the bash function comes in handy.

The syntax for creating bash functions is straightforward. It can be announced in two different formats:

function_name () {


function function_name {

To pass any number of arguments to the bash function, place them directly after the job name, separated by a space. The parameter that is passed is $1And the $2And the $3, Etc., according to the position of the parameter after the job name. a variable $0 Job names are reserved.

Let’s create a simple bash function that will create a directory (with the mkdir command) and enter it (using the cd command):

nano ~/.bashrc
mkcd ()
  mkdir -p -- "$1" && cd -P -- "$1"

Just like with aliases, add functions to files ~/.bashrc And run away source ~/.bash_profile Reload the file.

See also  A tutorial on Fsck Command in Linux for file system repair

Now instead of using mkdir to create a new directory and then cd to enter that directory, just type:

mkcd direktori_baru

If you are wondering what it is -- And the && Do here? This is a brief explanation.

  • -- – In addition to the orders. For example, if you try to create a directory that begins with - (Hyphen), if not used - The directory name will be interpreted as a command argument.
  • && – Ensures that the second command is run only if the first command is successful.


Now you have a good understanding of how to create aliases and bash functions that make command line work easier and more productive.


Source link

Originally posted 2020-11-17 23:08:21.

About wahyuway

Check Also

How To Install PHP 8.0 on Amazon Linux 2

PHP 8.0 is a major release of the PHP programming Language mostly used in the Development …

Leave a Reply