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

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

问题描述

我不能让expand_aliases采取在bash生效。我已经尝试了很多不同的事情,并没有什么工作。

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;'

和输出:

$ /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.

(是的,我使用禁用了javascript,而不是-o选项庆典,只是为了证明它正在做的。)

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

任何想法?

推荐答案

别名不可在同一行或在定义它们的功能相同。

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

从Bash的手册页:


       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.

借助猛砸参考手册说:

有关几乎每一个宗旨,外壳的功能是preferred 的超过别名。

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

而不是[重点煤矿]上面的最后一句话。我认为别名是一个命令行的方便,而不是东西,应该在脚本(包括那些只包括的bash -c 单行)一起使用。

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天全站免登陆