Logo
  • Ubuntu
  • CentOS
  • Debian
  • Fedora
  • RedHat

How to Create Bash Aliases - DesignLinux

Nov 05 2020
designlinux 0 Comments
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.

Related

Tags: bash, terminal

How to Manage Containers Using Podman and Skopeo in RHEL 8

Prev Post

How to Undo Last Git Commit

Next Post
Archives
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • July 2022
  • June 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
Categories
  • AlmaLinux
  • Android
  • Ansible
  • Apache
  • Arch Linux
  • AWS
  • Backups
  • Bash Shell
  • Bodhi Linux
  • CentOS
  • CentOS Stream
  • Chef
  • Cloud Software
  • CMS
  • Commandline Tools
  • Control Panels
  • CouchDB
  • Data Recovery Tools
  • Databases
  • Debian
  • Deepin Linux
  • Desktops
  • Development Tools
  • Docker
  • Download Managers
  • Drupal
  • Editors
  • Elementary OS
  • Encryption Tools
  • Fedora
  • Firewalls
  • FreeBSD
  • FTP
  • GIMP
  • Git
  • Hadoop
  • HAProxy
  • Java
  • Jenkins
  • Joomla
  • Kali Linux
  • KDE
  • Kubernetes
  • KVM
  • Laravel
  • Let's Encrypt
  • LFCA
  • Linux Certifications
  • Linux Commands
  • Linux Desktop
  • Linux Distros
  • Linux IDE
  • Linux Mint
  • Linux Talks
  • Lubuntu
  • LXC
  • Mail Server
  • Manjaro
  • MariaDB
  • MongoDB
  • Monitoring Tools
  • MySQL
  • Network
  • Networking Commands
  • NFS
  • Nginx
  • Nodejs
  • NTP
  • Open Source
  • OpenSUSE
  • Oracle Linux
  • Package Managers
  • Pentoo
  • PHP
  • Podman
  • Postfix Mail Server
  • PostgreSQL
  • Python
  • Questions
  • RedHat
  • Redis Server
  • Rocky Linux
  • Security
  • Shell Scripting
  • SQLite
  • SSH
  • Storage
  • Suse
  • Terminals
  • Text Editors
  • Top Tools
  • Torrent Clients
  • Tutorial
  • Ubuntu
  • Udemy Courses
  • Uncategorized
  • VirtualBox
  • Virtualization
  • VMware
  • VPN
  • VSCode Editor
  • Web Browsers
  • Web Design
  • Web Hosting
  • Web Servers
  • Webmin
  • Windows
  • Windows Subsystem
  • WordPress
  • Zabbix
  • Zentyal
  • Zorin OS
Visits
  • 2
  • 608
  • 1,055,380

DesignLinux.com © All rights reserved

Go to mobile version