无法让expand_aliases生效 [英] Can't get expand_aliases to take effect

查看:34
本文介绍了无法让expand_aliases生效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法让 expand_aliases 在 bash 中生效.我尝试了很多不同的东西,但没有任何效果.

这是一个简单的测试用例:

/bin/bash -c 'shopt -s expand_aliases;别名 cdtmp="cd/tmp";别名;cdtmp;密码;'

和输出:

$/bin/bash -c 'shopt -s expand_aliases;别名 cdtmp="cd/tmp";别名;cdtmp;密码;'别名 cdtmp='cd/tmp'/bin/bash: cdtmp: 命令未找到/家/用户$/bin/bash --versionGNU bash,版本 3.2.25(1)-release (i686-redhat-linux-gnu)版权所有 (C) 2005 Free Software Foundation, Inc.

(是的,我使用 shopt 而不是 -O 选项来 bash,只是为了证明它正在完成.)

有什么想法吗?

解决方案

别名在定义它们的同一行或同一函数中不可用.

来自 Bash 手册页:

<前>关于别名的定义和使用的规则有点令人困惑.Bash 总是至少读取一行完整的输入在执行该行上的任何命令之前.别名是在读取命令时展开,而不是在执行时展开.所以,别名定义与另一个命令出现在同一行在读取下一行输入之前不会生效.命令在该行上跟随别名定义不受新的影响别名.执行函数时,此行为也是一个问题.别名在读取函数定义时展开,而不是在函数被执行,因为函数定义本身就是一个com-磅命令.因此,函数中定义的别名不是直到该函数执行后才可用.为了安全,永远将别名定义放在单独的行上,并且不要在 com 中使用别名磅命令.对于几乎所有目的,别名都被 shell 函数取代.

Bash 参考手册

<块引用>

对于几乎所有用途,shell 函数都优于别名.

而不是上面的最后一句[强调我的].我认为别名是一种命令行便利,而不是应该在脚本中使用的东西(包括那些仅由 bash -c 单行代码组成的东西).

I can't get expand_aliases to take effect in bash. I've tried a lot of different things, and nothing works.

Here's the simple test case:

/bin/bash -c 'shopt -s expand_aliases; alias cdtmp="cd /tmp"; alias; cdtmp; pwd;'

And the output:

$ /bin/bash -c 'shopt -s expand_aliases; alias cdtmp="cd /tmp"; alias; cdtmp; pwd;'
alias cdtmp='cd /tmp'
/bin/bash: cdtmp: command not found
/home/user

$ /bin/bash --version
GNU bash, version 3.2.25(1)-release (i686-redhat-linux-gnu)
Copyright (C) 2005 Free Software Foundation, Inc.

(Yes, I'm using shopt instead of the -O option to bash, just to prove it's being done.)

Any ideas?

解决方案

Aliases aren't available on the same line or in the same function where they are defined.

From the Bash man page:

       The rules concerning the definition and use  of  aliases  are  somewhat
       confusing.   Bash  always  reads  at  least  one complete line of input
       before executing any  of  the  commands  on  that  line.   Aliases  are
       expanded  when  a command is read, not when it is executed.  Therefore,
       an alias definition appearing on the same line as another command  does
       not  take  effect  until  the next line of input is read.  The commands
       following the alias definition on that line are not affected by the new
       alias.   This  behavior  is  also an issue when functions are executed.
       Aliases are expanded when a function definition is read, not  when  the
       function  is  executed,  because a function definition is itself a com‐
       pound command.  As a consequence, aliases defined in a function are not
       available  until  after  that function is executed.  To be safe, always
       put alias definitions on a separate line, and do not use alias in  com‐
       pound commands.

       For almost every purpose, aliases are superseded by shell functions.

The Bash Reference Manual says

For almost every purpose, shell functions are preferred over aliases.

instead of the last sentence above [emphasis mine]. I consider aliases to be a command-line convenience rather than something that should be used in scripts (including those which consist solely of bash -c one-liners).

这篇关于无法让expand_aliases生效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆