linux - 传递别名作为函数参数linux bash

标签 linux bash bash-function

大家好,我正在学习如何在 Linux 中使用 .bashrc 文件,正如我的标题所述,我想知道如何让函数将参数识别为别名

我有一个名为 home 的别名定义为:alias home=$HOME

一个函数定义为

function go(){
cd $1
ls $1
}

但是当我回家 我明白了

bash: cd: home: 没有那个文件或目录 ls: 无法访问 home: 没有那个文件或目录

当我想要它的时候去$HOME

我将如何实现它?

最佳答案

别名不是单词替换而是一个新创建的小命令:

$ alias bla=ls
$ bla
file1
file2
file3
…

因此,它不能按照您假设的方式使用。

你可能想为此使用变量替换:

$ home=$HOME
$ function go() {
  cd "$(eval echo \$"$1")"
}
$ go home

如果你想使用别名,尽管这是一种滥用,试试这个:

$ alias home=$HOME
$ function go() {
  cd "$(type "$1" | sed -e 's/.*is aliased to .//' -e 's/.$//')"
}
$ go home

关于linux - 传递别名作为函数参数linux bash,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16764682/

相关文章:

android - 如何在 Linux (Ubuntu) 上启用 Kindle Fire 的 USB 调试?

linux - grep命令查找字符出现n次

linux - bash 变量替换与 grep 默认行为

bash - 如何更改文本中特定行的单词?

bash - 如何覆盖 pushd 和 popd 对 dirs 的自动调用?

linux - 无法在 Amazon Linux 实例上禁用 SSLv3

c - 如何确保分配缓冲区在内存中

php - 如何从 php.net 获取最新的 php 版本信息

bash - 从子 shell 返回值并输出到局部变量

bash - 定时 bash 函数