bash alias_Linux Bash Alias命令教程

bash alias

bash alias

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.

Linux Bash提供了一些有关操作的捷径。 有时我们需要在shell中运行一个容易出错的日志命令。 每次编写或记住命令都不是可行的方法。 Bash具有alias功能,该功能用于设置有关用户命令的一些快捷方式。

alias命令语法 (alias Command Syntax)

The syntax of the 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.

别名定义的语法如下。 ALIAS是我们要使用的快捷方式,可以是我们想要的任何东西,但可以使用Linux命令以外的唯一名称。 COMMAND是我们在调用ALIAS时要使用的命令。

alias ALIAS COMMAND

创建别名 (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 /. Every time we call mls ls / command will be executed.

命令和变量可能很长。 使用长命令和变量会很麻烦。 Bash可以为它们创建一些别名并以简短的方式使用它们。 在此示例中,我们将创建一个名为mls的别名,等效于ls / 。 每次我们调用mls ls /命令时都会执行。

$ alias mls="ls /"
$ mls
1  bin  boot  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var

使别名持续存在 (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 the user home directory. We can add our alias named mls to the .bashrc of the current user with the following command.

重新启动后,创建的别名将被删除,或者无法从其他Shell会话访问。 我们通常需要使定义的别名持久化。 为了使别名持久化,我们通常使用位于用户主目录中的.bashrc文件。 我们可以使用以下命令将名为mls的别名添加到当前用户的.bashrc中。

$ echo "alias mls='ls /'" >> ~/.bashrc

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.

如果要使别名对所有系统用户可用,则应将此别名添加到系统范围内的bash配置文件/etc/bash.bashrc或类似名称的文件中。

LEARN MORE  Php - Syntax Overview
了解更多信息-语法概述

别名参数(Alias Parameters)

Linux bash and similar shells provide a 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 a single parameter. This parameter provided to the ls command as path. If we do not provide a parameter it will act as space.

Linux bash和类似的shell提供了一种参数机制来动态提供数据。 我们可以使用这种机制使别名动态化。 在bash中, $1指定第一个参数, $2指定第二个参数,..在此示例中,我们将定义名为mls的别名,该别名接受单个参数。 该参数作为路径提供给ls命令。 如果我们不提供参数,它将充当空格。

$ alias mls='ls $1'
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 the root directory and then print some text with multiple commands in the alias.

猛击; 用于定界命令。 通常用于在一行中提供多个命令。 我们可以使用t ; 也是别名。 在此示例中,我们将列出根目录,然后在别名中使用多个命令打印一些文本。

$ alias mls='ls /;echo "Root directory listed";'

翻译自: https://www.poftut.com/linux-bash-alias-and-shortcuts/

bash alias

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值