Skip to main content
How To

Create Custom Commands in Ubuntu

Save time by converting frequently used command and option combinations as custom command.

β€” Sagar Sharma

As a Linux user, I can relate to your urge to create a custom command that is specific to your needs.

Those commands could be anything from combining different commands to a script containing multiple arguments.

So in this tutorial, I will walk you through the following methods and both will include steps for the local user and using them globally:

  • Using alias
  • Using a bash script

Let's start with the first one.

How to create custom commands using an alias

To create a temporary alias (for the current session only), you can use the following command syntax:

alias short_name="your custom command here"

For example, here, I created an alias to update repositories and install new packages named update:

alias update="sudo apt update && sudo apt upgrade -y"

When executed, it gave me the following output:

Create a temporary custom command using alias in Ubuntu Linux
🚧
There must not be any space before and after = while using the alias command.

But as I said, it will only work for the current session. This means the reboot will wipe down the alias.

So if you want to make the alias permanent, here's how you do it.

How to create permanent custom commands using an alias

πŸ“‹
This method will only work for the current user so if you want to create a global alias, skip to the next section.

If you want to create a permanent alias for the currently logged-in user, you have to add the alias to the bashrc file.

First, open the bashrc file using the following command:

nano ~/.bashrc

Now, go to the end of the line in the nano text editor by pressing Alt + / and enter the alias for the command by following the given syntax:

alias custom_command='enter one or more commands with different flags'

For example, here, I created an alias for the ls command which will automatically execute ls -lah:

alias ls='ls -lah'
Create a permanent custom command using alias in Ubuntu Linux

Save changes and exit from the nano text editor.

To take effect from the changes you've made, source the bashrc file using the following command:

source ~/.bashrc

And here's what it gave me when I executed the ls command:

Use custom commands in Ubuntu Linux

How to create custom commands for all users

If you want to create custom commands for all the users on the system, you have to make changes to the /etc/bash.bashrc file.

So the first step would be to open the file:

sudo nano /etc/bash.bashrc

Now, press Alt + / to go to the end of the file and add the alias by following the given command syntax:

alias custom_command='enter one or more commands with different flags'

For your reference, here I created two aliases to open and edit the bashrc file:

# Edit your .bashrc file
alias ebashrc='nano ~/.bashrc'

# Source your .bashrc file to apply changes
alias rbashrc='source ~/.bashrc'
create custom commands for all users using alias on Ubuntu Linux

Save changes and exit from the nano text editor.

Finally, source the bashrc file using the following:

source /etc/bash.bashrc 

Now, you can use the custom commands systemwide.

How to create custom commands using scripts

For those who don't know, every command you have on Linux is a script itself so don't hesitate to create scripts for creating custom commands.

The first step is to create a directory where you will store the scripts that you later will be using as commands.

πŸ“‹
Make sure to create a directory and store scripts inside the root directory if you want to avail this to every user on the system.

To create a directory, use the mkdir command in the following manner:

mkdir /path/to/directory

I created a directory inside the /usr/local/bin/ directory so I will need superuser privileges (as it is in the root directory):

sudo mkdir /usr/local/bin/scripts

Now, create a script inside the recently created directory:

πŸ“‹
While creating a script, you can skip the .sh extension to have a similar execution syntax as normal commands.
nano /path/to/directory/to/store/script

For example, here, I created a script named update:

sudo nano /usr/local/bin/scripts/update

Which contains the following:

#!/bin/bash
sudo apt update && sudo apt upgrade -y

Sure, you can enter multiple arguments, and multiple commands with different arguments.

Once you are done, save changes and exit from the text editor.

Use the chmod command to make your file executable:

chmod +x /path/to/file

My script is inside the /usr/local/bin/scripts directory which simply means I have to use sudo with the chmod command:

sudo chmod +x /usr/local/bin/scripts/update

Next, text the script by executing it:

./script_name
Use bash scripts to create custom commands in Ubuntu Linux

If everything goes smoothly, in the next step, you can add this path to your bashrc file so you can use this command from everywhere without giving path.

First, change your directory to where the script is stored:

cd /path/to/directory

Now, use the pwd command to know the absolute path of the directory:

pwd
Use the pwd command to know the current working directory

Now, copy this path (the given output by the pwd command).

Use this command for a logged-in user only

If you want to use the script as a command for the currently logged-in user, then you have to insert the PATH variable in the ~/.bashrc file.

First, open the ~/.bashrc file:

nano ~/.bashrc

Go to the end of the file and add a line of the PATH variable in the following manner:

export PATH=$PATH:/path/to/script/directory

Make sure you change the /path/to/script/directory Β part with yours (output given by the pwd command).

For example, if you stored my script inside the /home/sagar/scripts directory, then, my command would look like this:

export PATH=$PATH:/home/sagar/scripts
Create custom command from script for logged-in user in Ubuntu Linux

Save changes, exit from the text editor, and source the bashrc file to take effect from the changes you've made:

source ~/.bashrc

Now, you can use the script from anywhere and it will behave like normal command:

Use bash script to create custom commands for local user

Use this command globally (for every user)

If you want this custom command accessible to every user on the system, you have to enter the PATH variable in the /etc/bash.bashrc file.

First, open the /etc/bash.bashrc file using the following command:

sudo nano /etc/bash.bashrc 

Go to the end of the file using Alt + / and add the line of the PATH variable as shown here:

export PATH=$PATH:/output/of/pwd/command

In my case, the command would look like this:

export PATH=$PATH:/usr/local/bin/scripts
Create custom command form bash script for all the users on Ubuntu Linux

Once done, save changes and exit from the text editor.

To enable changes you've made, source the /etc/bash.bashrc file using the following command:

sudo source /etc/bash.bashrc 

That's it! From now on, every user on the system can use the custom command that you've made.

For example, here I've executed the update command as a root user from the root directory:

Use bash script as a custom command which can be used by every user on system on Ubuntu Linux

Wrapping Up...

This was a detailed guide on how you can create a custom command using two different methods and both includes ways for local and global users.

I hope you will find this guide helpful.

Sagar Sharma