繁体   English   中英

从另一个Shell脚本运行别名

[英]run alias from another shell script

我的.bash_profile中有一个别名,如alias cdpy="cd python" ,我已经找到了它。 但是我仍然不能在我的另一个shell脚本pygitup中使用它。

我用shopt -s expand_aliases搜索并得到了一些答案,例如添加shopt -s expand_aliases 我已将其添加到pygitup中,但仍然无法正常工作。 我使用错了吗? 这是我的用法:

# some code
shopt -s expand_aliases
cdpy
# some code

别名不会被外部命令继承,因此没有别名可以扩展。 你得source是再次定义脚本别名的文件:

# some code
shopt -s expand_aliases
source ~/.bash_profile
cdpy
# some code

如果您从环境中使用前导“点空间”运行pygitup ,它将继承您外壳程序的配置,包括别名。

一个带有bash脚本的简单示例:

user@pc:~ $ alias e='echo alias is set'
user@pc:~ $ e
alias is set
user@pc:~ $ vim pygitup.sh
user@pc:~ $ cat pygitup.sh 
#!/bin/bash
e
user@pc:~ $
user@pc:~ $ ./pygitup.sh 
./pygitup.sh: line 2: e: command not found
user@pc:~ $ 
user@pc:~ $ 
user@pc:~ $ . ./pygitup.sh       # <--- notice the leading dot
alias is set
user@pc:~ $ 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM