如何列出所有的Linux别名?

huangapple go评论79阅读模式
英文:

How to list all the linux aliases

问题

我知道在Linux中可以使用alias命令获取已定义的别名列表。现在我正在尝试通过Go代码实现相同的功能:

func ListAlias() error {
    out, err := exec.Command("alias").Output()
    if err != nil {
        fmt.Println(err)
        return err
    }
    fmt.Println(out)
    return nil
}

但是返回的结果是:

exec: "alias": 在$PATH中找不到可执行文件

我尝试查找alias的实际二进制文件,但是没有找到任何结果:

$whereis alias
alias:

我考虑的另一种方法是解析~/.bashrc文件以获取已定义的别名列表,但是我遇到了这样的情况:bashrc文件列出了另一个custom_aliases.sh文件,并且所有的别名都在那里列出。这就是为什么我尝试使用alias命令列出所有别名的原因。

英文:

I am aware that in Linux I can use the alias command to get a list of defined aliases. I am now trying to do the same through Go code with:

func ListAlias() error {
	out, err := exec.Command("alias").Output()
	if err != nil {
		fmt.Println(err)
		return err
	}
	fmt.Println(out)
	return nil
}

but all that were returned were:

exec: "alias": executable file not found in $PATH

I tried looking for where the actual binary of alias is but that leads nowhere either:

$whereis alias
alias:

The alternative I've considered is to parse the ~/.bashrc file for the list of aliases defined but I have encountered this scenario where the bashrc lists another custom_aliases.sh file and all the aliases are listed there. That's why I am trying to use the alias command to list all the aliases.

答案1

得分: 5

alias不是一个可执行文件,而是一个shell内置命令。你可以通过运行以下命令轻松查看:

$ type alias
alias is a shell builtin

因此,你需要根据你使用的shell来调用shell的alias命令。例如,如果你使用的是bash,你需要使用以下命令:

out, err := exec.Command("/bin/bash", "-c", "alias").Output()

但是,这仍然无法给你答案,因为bash在这种情况下不会source .bashrc文件,所以子shell中无法使用别名。你需要使用--rcfile--login/-l选项,并且还需要使用-i指定shell为交互式

out, err := exec.Command("/bin/bash", "-lic", "alias").Output()
// 或者
out, err := exec.Command("/bin/bash", "--rcfile", "~/.bashrc", "-ic", "alias").Output()

exec.Command("/bin/bash", "-ic", "alias")也可能适用,这取决于你的别名是从哪里加载的。其他像zsh、sh、dash等的shell可能使用不同的选项加载不同的文件,所以如果-ic-lic不起作用,可以查阅你的shell的文档。

英文:

alias isn't an executable but a shell builtin. You can easily see that by running

$ type alias
alias is a shell builtin

Therefore you need to call the shell's alias command depending on which shell you're using. For example with bash you'll need to use

out, err := exec.Command("/bin/bash", "-c", "alias").Output()

But that still won't give you the answer because bash doesn't source the .bashrc file in that case so aliases won't be available in the subshell. You'll need the --rcfile or --login/-l option and also need to specify the shell as interactive with -i

out, err := exec.Command("/bin/bash", "-lic", "alias").Output()
// or
out, err := exec.Command("/bin/bash", "--rcfile", "~/.bashrc", "-ic", "alias").Output()

exec.Command("/bin/bash", "-ic", "alias") would also possibly work depending on where your aliases are sourced. Other shells like zsh, sh, dash... may source different files with different options, so check your shell's documentation if -ic or -lic doesn't work

huangapple
  • 本文由 发表于 2023年5月16日 01:14:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76256462.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定