linux - 如何使用 bash 别名作为命令行参数

标签 linux bash shell ssh alias

我在谷歌上查过这个,但之前似乎没有人问过这个问题。有没有一种方法可以设置别名并将其用作程序的命令行参数?

例如

ssh myserver

并且myserver定义为我的服务器的ip地址

最佳答案

其实是有办法的。但是您需要定义两个别名。

首先:Bash 手册状态 (LESS=+/'^ALIASES' man bash)

ALIASES If the last character of the alias value is a blank, then the next command word following the alias is also checked for alias expansion.

还有 documented in POSIX规范Thanks to @gniourf_gniourf对于链接:

If the value of the alias replacing the word ends in a <blank>, the shell shall check the next command word for alias substitution; this process shall continue until a word is found that is not a valid alias or an alias value does not end in a <blank>.


所以,如果定义这两个别名(请保留末尾的空格):

$ alias ssh='ssh '
$ alias myserver='192.168.0.1'
$ set -x
$ ssh myserver
+ ssh 192.168.0.1

清除别名:

$ unalias ssh
$ unalias myserver

注意 1:也记录在 kshzsh 手册中

注意 2:经测试可在 dash 中正常工作(未找到文档)。

Wikipedia entry on "alias" chaining

关于linux - 如何使用 bash 别名作为命令行参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36243829/

相关文章:

c++ - syscalls.h 以及 minor() 和 major() 函数

linux - Bash 在 CSV 列中选择多个值

linux - 递归遍历文件并接收文件信息

linux - 如何在不使用find的情况下获取当前目录的直接子目录中名称包含某种模式的所有文件?

linux - 在 Linux 中重新启动文件更改进程

python - 使用 Anaconda 的 Python 版本创建一个 virtualenv

linux - 终端仿真器和 TTY 设备之间有什么关系?

linux - Linux shell 和文件系统如何识别 Unicode?

bash - 将文本从先前的标准输出拉到命令行上

linux - 如何打印仅包含字母的单词?