如何将别名转换为真实文件?

How to translate an alias into a real file?

大多数情况下,别名效果很好,但有时,命令由其他程序执行,它们在 PATH 中找到它,在这种情况下,别名不如真实文件有效。

例如

我有以下别名:

alias ghc='stack exec -- ghc'

而且我想把它翻译成一个可执行文件,这样依赖它的程序就能正确找到它。该文件将像别名一样工作,包括它如何处理它的参数。

那么,有什么工具或脚本可以帮助做到这一点吗?

这是我的解决方案,我创建了一个名为 ghc 的文件,如下所示:

#!/bin/sh
stack exec -- ghc "$@"

$@两边有双引号的原因在这里解释一下:Propagate all arguments in a bash shell script

So, is there any tool or scripts can help doing this?

一个lazy问题的一个简单问题...这是一个函数:

alias2script() { 
    if type "" | grep -q '^'""' is aliased to ' ; then 
        alias | 
        { sed -n "s@.* ='\(.*\)'$@#\!/bin/sh\n \"${\@}\"@p" \
                 > "".sh
          chmod +x "".sh
          echo "Alias '' hereby scriptified.  To run type: './.sh'" ;}
    fi; }

让我们在常见的 bash 别名 ll:

上试试
alias2script ll

输出:

Alias 'll' hereby scriptified.  To run type: './ll.sh'

里面有什么ll.sh

cat ll.sh 

输出:

#!/bin/sh
ls -alF "${@}"