如何在 bash shell 脚本中设置别名,以便从外部可见?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2197461/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set an alias inside a bash shell script so that is it visible from the outside?
提问by phil swenson
OSX: This works from the command line:
OSX:这从命令行工作:
alias ruby="/opt/local/bin/ruby1.9"
but in side a shell script, it has no effect. I want to write a script that will switch between ruby 1.8 and ruby 1.9, so this needs to be a script - not in my profile.
但是在shell脚本中,它没有效果。我想编写一个可以在 ruby 1.8 和 ruby 1.9 之间切换的脚本,所以这需要是一个脚本 - 不在我的个人资料中。
It appears "source script.sh" works, but "./script.sh". Why is this? How can I replicate this in my script?
看起来“源脚本.sh”有效,但“./script.sh”。为什么是这样?如何在我的脚本中复制它?
回答by JoeB
The simple answer for you is that scripts create non-interactive shells and, by default, the expand_aliases option is often disabled.
对您来说,简单的答案是脚本创建非交互式 shell,并且默认情况下, expand_aliases 选项通常是禁用的。
You can fix this very simply by just adding the following line to the top of your script to enable the alias expansion:
您可以通过将以下行添加到脚本顶部以启用别名扩展来非常简单地解决此问题:
shopt -s expand_aliases
shopt -s expand_aliases
This problem has been bugging me, so I did research and then wrote a blog post once I figured out how to fix it for myself: Post about using alias from within Linux shell scripts.
这个问题一直困扰着我,所以我做了研究,然后在我想出如何为自己修复它后写了一篇博文: 发布关于在 Linux shell 脚本中使用别名。
Of course, right after I figured out that part, I found that, while it works for what you need, it will not work if you have a subshell within a a subshell. I am still looking into the fix for that problem, that is how I just came across your question. On the blog post, I mention a cheap hack that I use to grab the alias in a shell script. It isn't elegant, but it actually works even in this multiple subshell problem I have.
当然,在我弄清楚那部分之后,我发现,虽然它可以满足您的需要,但如果您在 aa 子shell 中有一个子shell,它就不起作用。我仍在寻找解决该问题的方法,这就是我刚刚遇到您的问题的方式。在博客文章中,我提到了一个廉价的 hack,我用它来获取 shell 脚本中的别名。它并不优雅,但即使在我遇到的这个多子壳问题中,它也确实有效。
回答by codaddict
./script.sh
will be executed in a sub-shell and the changes made apply only the to sub-shell. Once the command terminates, the sub-shell goes and so do the changes.
./script.sh
将在子外壳中执行,所做的更改仅适用于子外壳。一旦命令终止,子 shell 就会运行,更改也是如此。
sourcing the file using . ./script.sh
or source ./script.sh
will read and execute commands from the file-name argument in the current shell context, that is when a script is run using source
it runs within the existing shell, any variables created or modified by the script will remain available after the script completes.
使用. ./script.sh
orsource ./script.sh
将在当前 shell 上下文中从 file-name 参数中读取和执行命令来获取文件,也就是说,当使用source
它在现有 shell 中运行的脚本运行时,脚本创建或修改的任何变量在执行后仍然可用脚本完成。
回答by Dave Bacher
You can write a function in your .profile to switch the aliases
您可以在 .profile 中编写一个函数来切换别名
function toggle-ruby() {
if [ "" == "1.9" ]; then
alias ruby=/opt/local/bin/ruby1.9
else
alias ruby=/opt/local/bin/ruby1.8
fi
}
then run you can run:
然后运行你可以运行:
toggle-ruby 1.9
or
或者
toggle-ruby 1.8
to switch back and forth.
来回切换。