[email protected]Support 24/7 · Billing 09:00 – 00:00LinkedInXFacebook
NexonHost
Netherlands Dedicated ServersAmsterdam · Equinix AM6Germany Dedicated ServersFrankfurt · Equinix FR4Romania Dedicated ServersBucharest · VoxilityAustria Dedicated ServersVienna · Interxion VIEBulgaria Dedicated ServersSofia · TelepointFrance Dedicated ServersParis · Interxion PAR — Marseille · Digital Realty MRS3Ireland Dedicated ServersDublin · Equinix DB2Italy Dedicated ServersMilan · Irideos AvalonPoland Dedicated ServersWarsawSpain Dedicated ServersMadrid · Equinix MD2United States Dedicated ServersAshburn · Equinix DC
All articles
Getting startedJune 26, 2023

How to create bash alliases.

How To Create Bash Alliases.

This article explains how to create bash aliases so you can be more productive on the command line.

 

Creating Bash Aliases

Creating aliases in bash is very straight forward. The syntax is as follows:

alias alias_name="command_to_run"

An alias declaration starts with the alias keyword followed by the alias name, an equal sign and the command you want to run when you type the alias.

The ls command is probably one of the most used commands on the Linux command line.

alias ll="ls -la"

Now, if you type ll in your terminal, you’ll get the same output as you would by typing ls -la.

The ll alias will be available only in the current shell session. If you exit the session or open a new session from another terminal, the alias will not be available.

To make the alias persistent you need to declare it in the ~/.bash_profile or ~/.bashrc file.

Open the file in your text editor :

nano ~/.bashrc

and add your aliases:

~/.bashrc
# Aliases
# alias alias_name="command_to_run"

# Long format list
alias ll="ls -la"

# Print my public IP
alias myip='curl ipinfo.io/ip'

The aliases should be named in a way that is easy to remember. It is also recommended to add a comment for future reference.

Once done, save and close the file. Make the aliases available in your current session by typing:

source ~/.bashrc

As you can see, creating simple bash aliases is quick and very easy.

 

Creating Bash Aliases with Arguments (Bash Functions)

Sometimes you may need to create an alias that accepts one or more arguments. That’s where bash functions come in handy.

The syntax for creating a bash function is very easy. They can be declared in two different formats:

function_name () {
  [commands]
}

or

function function_name {
  [commands]
}

To pass any number of arguments to the bash function simply, put them right after the function’s name, separated by a space. The passed parameters are $1, $2, $3, etc., corresponding to the position of the parameter after the function’s name. The $0 variable is reserved for the function name.

Let’s create a simple bash function which will create a directory and then navigate into it:

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

Same as with aliases, add the function to your ~/.bashrc file and run source ~/.bash_profile to reload the file.

Now instead of using mkdir to create a new directory and then cd to move into that directory , you can simply type:

mkcd new_directory

If you wonder what are and && here is a short explanation.

  • – makes sure you’re not accidentally passing an extra argument to the command. For example, if you try to create a directory that starts with (dash) without using the directory name will be interpreted as a command argument.

  • && – ensures that the second command runs only if the first command is successful.

 

Conclusion

By now you should have a good understanding of how to create bash aliases and functions that will make your life on the command line easier and more productive.

Keep reading
How to Set and List Environment Variables in Linux
May 20, 2023

How to Set and List Environment Variables in Linux

How To Set And List Environment Variables In Linux In this guide, we will explain to read and set environment and shell variables. In Linux and Unix based systems environment variables are a set of dynamic named values, stored within the system that are used by applications launched in shells or…

Read article
How to Install RPM Packages on CentOS.
May 20, 2023

How to Install RPM Packages on CentOS.

How To Install RPM Packages On CentOS. In this tutorial, we will show you two methods of how to install RPM packages on CentOS. RPM is a packaging system used by Red Hat and its derivatives such as CentOS and Fedora. The official CentOS repositories contain thousands of RPM packages that can be…

Read article
How to use ip Command.
May 19, 2023

How to use ip Command.

How To Use Ip Command. This article explains how to use the ip command through practical examples and detailed explanations of the most common options. The ip command is a powerful tool for configuring network interfaces that any Linux system administrator should know. It is used to bring…

Read article
Get in Touch

Together, Let’s Build a Faster, Safer Internet.

Build scalable infrastructure with NexonHost — high-performance dedicated servers, VPS hosting, cloud hosting, and DDoS protection across Europe.

Get Started →Contact Us
[email protected]Support 24/7 · Billing 09:00 – 00:00