Linux Bash Alias and Shortcuts

RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/linux-bash-alias-and-shortcuts/
PINTEREST
PINTEREST
INSTAGRAM
  1. Introduction To Linux Bash Programming
  2. Linux Bash Directories and Files
  3. Linux Bash Environment Variables
  4. Linux Bash Variables
  5. Linux Bash Functions
  6. Linux Bash If Else Conditionals
  7. Linux Bash Create and Run Script Files
  8. Linux Bash Operators
  9. Linux Bash Arrays
  10. Linux Bash Input, Output and Redirection
  11. Linux Bash History
  12. Linux Bash Alias
  13. Linux Bash .bashrc File
  14. Linux Bash Debugging

Linux Bash provides some shortcuts about operations. Sometimes we need to run a log and error prone command in the shell. Every time writing or remembering the command is not a feasible way. Bash have alias feature which is used to set some shortcuts about user commands.

Syntax

Syntax of alias definition is like below. ALIAS is the shortcut we want to use and can be anything we want but use unique names other than Linux commands. COMMAND is the command we want to use when ALIAS is called.

Create Alias

Commands and variables can be very long. Using long commands and variables become a pain. Bash gives the ability to make some alias for them and use them in a short way.

In this example we will create an alias named mls which is equivalent ls / . Everytime we call mls ls / command will be executed.

Make Alias Persistent

Created alias will be removed after reboot or can not be accessed from different shell sessions. We generally need to make defined aliases persistent. In order to make alias persistent we generally use .bashrc file which is located in user home directory.

LEARN MORE  Linux Bash Define, Call Functions

We can add our alias named mls to the .bashrc of the current user with the following command.

If we want to make alias available to the all system users we should add this elias to the system wide bash configuration file /etc/bash.bashrc or similar name file.

Alias Parameters

Linux bash and similar shells provides parameter mechanism to make dynamically provide data. We can use this mechanism to make our alias dynamic. In bash $1 specifies the first parameter $2 specifies second, ..

In this example we will define our alias named mls which accept single parameter. This parameter provided to the ls command as path. If we do not provide a parameter it it will act like a space.

Alias Parameters

Alias Parameters

Multiline Alias Command

In bash ; used to delimit the commands. It is generally used to provide multiple commands in a single line. We can use t; in alias too. In this example we will list root directory and than print some text with multiple commands in alias.

Multiline Alias Command

Multi line Alias Command

  1. Introduction To Linux Bash Programming
  2. Linux Bash Directories and Files
  3. Linux Bash Environment Variables
  4. Linux Bash Variables
  5. Linux Bash Functions
  6. Linux Bash If Else Conditionals
  7. Linux Bash Create and Run Script Files
  8. Linux Bash Operators
  9. Linux Bash Arrays
  10. Linux Bash Input, Output and Redirection
  11. Linux Bash History
  12. Linux Bash Alias
  13. Linux Bash .bashrc File
  14. Linux Bash Debugging
RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/linux-bash-alias-and-shortcuts/
PINTEREST
PINTEREST
INSTAGRAM

You may also like...

Leave a Reply

Your email address will not be published.

Enjoy this blog? Please spread the word :)