How to create an alias Bash

Do you often find yourself typing a long command on the command line or search previously typed commands in bash history? If your answer to these questions is yes, then you will find it easy to bash aliases. Bash alias command allows you to set longer can remember shortcut commands.

Bash is a shortcut on the alias essentially allows you to not have to remember long command, and use the command line to eliminate a lot of input. For example, you can set an alias as a shortcut tgz tar -xvfz command.

This article describes how to create bash aliases, in order to improve efficiency in the command line.

Creating Bash alias

Very simple to create an alias in bash. The syntax is as follows:

alias alias_name="command_to_run"

To create a new bash aliases by typing aliaskeyword. Then declare an alias, followed by an equal sign and the command you want to run when you type an alias. The command requires quotes and no space around the equal sign. Each alias need to declare on a new line.

The ls command Linux command line is probably one of the most commonly used commands. I usually use this command to switch -la lists all files and directories, including hidden files and directories in a long list of formats.

Let's create a simple bash alias called ll, it will be ls -la command shortcuts. To do this, type open a terminal window and type:

alias ll="ls -la"

Now, if you enter ll console, you will get the same output and input ls -la.

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

To make an alias persistent, you need to declare it in ~ / .bash_profile or ~ / .bashrc file. ~ / .Bashrc opens in a text editor:

nano ~/.bashrc

And add your alias:

In ~ / .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'

You should be easy to remember are named alias. Also proposed to add a comment for future reference.

When finished, save and close the file. Type the following, the aliases in the current session are available:

source ~/.bash_profile

As you can see, creating a simple bash alias is quick and easy.

Use parameters to create an alias Bash (Bash function)

Sometimes you may need to create an acceptance of one or more parameters of the alias, which is the bash function comes in handy.

Create a bash function syntax is very simple. They can be declared in two different formats:

function_name () {
  [commands]
}

Or

function function_name {
  [commands]
}

To any number of parameters passed to a bash function, just after the function name thereof, can be separated by spaces. Transmission parameters are $ 1, $ 2, $ 3, etc., corresponding to the position after the function name parameter. The $ 0 variable is reserved for the function name.

Let us first create a simple bash function, it creates a directory, then navigate to it:

In ~ / .bashrc

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

The same as when you create a new alias, add a function to ~ / .bashrc file and run source ~ / .bash_profile to reload.

Now, you just type: instead of using the mkdir create a new directory and then cd into the directory.

mkcd new_directory

If you want to know what is -, && Here is a brief explanation.

  • - - make sure you do not accidentally pass additional parameters to the command. For example, if you try to create - at the beginning (dash) instead of using - directory directory name, it will be interpreted as a command parameter.
  • && - to ensure that the second command runs only if the first command is successful.

to sum up

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 efficient.

If you encounter problems or have feedback, please leave a message below.

Guess you like

Origin www.linuxidc.com/Linux/2019-08/159951.htm