• Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate
TecNStuff
Menu
  • Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate

How to Create Bash Aliases

Written by Admin, Updated On November 5, 2020
bash, terminal
Create Bash Aliases

Bash aliases are shortcuts for the long commands. It allows you to set a memorable shortcut command for a longer command. This guide explains how to create bash aliases on Linux system.

Creating Bash Aliases#

It is very easy to create bash aliases. Following is the general syntax:

alias alias_name="command_to_run"

You should put the alias keyword in starting and followed by the alias name, an equal sign. After that the command enclosed in double quotes for which you want to create alias. Make sure there should not white space around the equal sign. You must declare the each alias on a new line.

In Linux, the ls command is most used command. Generally, it is used to list out all files and directories. You can list the files with readable file size format using -lh command.

Let’s create a simple bash alias named llh which will be a shortcut for the ls -lh command . To do so type open a terminal window and type:

alias llh="ls -la"

Now, once you will type llh on your terminal, it will show the same output as the ls -lh command.

This llh 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.

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

To do that open the file in your text editor :

nano ~/.bashrc

Add your aliases:

# Aliases
# alias alias_name="command_to_run"

# Long format list with readable size
alias llh="ls -lh"

You can set any name for alias but it is recommended to set which is easy to remember. You also can add the comment for future reference.

Save and close the file. Check alias available in your current session by typing:

source ~/.bashrc

Creating Bash Aliases with Arguments (Bash Functions)#

It is common required to pass arguments with commands. When you have need to create alias that accepts one or more arguments, the bash function will be useful.

It’s easy to create bash function. You can create by multiple formats:

function_name () {
  [commands]
}
function function_name {
  [commands]
}

To pass multiple arguments to the bash function, just put right after the function name with space separated. Parameters will be $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.

For example, create a bash function to create a directory and navigate into it:

mkdircd ()
{
  mkdir -p -- "$1" && cd -P -- "$1"
}

Open ~/.bashrc file and add the function to it. After that run source ~/.bash_profile to reload the file.

Now you can use this bash function instead of using mkdir to create new directory and cd into it. You would just type:

mkdircd new_directory

Conclusion#

You have learned how to create bash aliases and functions for easily remember the one liner command.

If you have any question or feedback, please leave a comment below.

If our content helps you, please consider buying us a coffee

Thank you for your support.

Share On
Share on Facebook
Share on Twitter
Share on Reddit
Share on Tumblr
 Previous Article Bash while Loop
Next Article   Bash Functions

Related Posts

  • How to Use sed to Find and Replace String in Files

    How to Use sed to Find and Replace String in Files

    December 18, 2020
  • Linux Head Command

    Linux Head Command

    December 16, 2020
  • Rename Files and Directories in Linux

    How to Rename Files and Directories in Linux

    December 15, 2020

Leave a Reply Cancel reply

DigitalOcean Referral Badge

Popular Posts

© 2020 TecNStuff All rights reserved. This website is using and storing cookies on your browser. By using this website you agree our Privacy Policy.  Follow us -  Twitter | Facebook