从bash脚本设置bash别名(不是命令行)

bash shell不同(用于创build新的别名):

alias test="echo test" 

如果我使用bash脚本做同样的事情,别名不会运行:

 #!/bin/bash alias test="echo test" 

也出口它,同样的问题:

 alias test="echo test" ; export test 

也许是正确的:真正的“testing”不是一个variables。 那么,如何把任何别名bash脚本,让他们在我的环境中可用?

这个问题已经在这里回答了 。

helloworld.bash

 #!/bin/bash alias helloworld="echo helloworld" 

执行脚本如下:

 [vagrant@localhost ~]$ . helloworld.bash 

[vagrant@localhost ~]$ helloworld结果是:

 [vagrant@localhost ~]$ helloworld helloworld 

alias在非交互式shell中不可用。 使用bash的选项-i 。 如果存在-i选项,则该shell是交互式的。

 #!/bin/bash -i alias test="echo test" test 

输出:

testing