如何设置一个bash shell脚本里面的一个别名,这样是从外部看到? [英] How to set an alias inside a bash shell script so that is it visible from the outside?

查看:108
本文介绍了如何设置一个bash shell脚本里面的一个别名,这样是从外部看到?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

OSX:这个工程在命令行:

OSX: This works from the command line:

alias ruby="/opt/local/bin/ruby1.9"

但在一侧的shell脚本,它没有任何效果。我想编写一个脚本,将红宝石1.8和1.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.

看来源script.sh的作品,但./script.sh。为什么是这样?我怎么能在我的脚本复制呢?

It appears "source script.sh" works, but "./script.sh". Why is this? How can I replicate this in my script?

推荐答案

./ script.sh 将在一个子shell执行,并且所做的更改仅适用子壳。一旦终止命令,子shell那张等方面做了修改。

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

这篇关于如何设置一个bash shell脚本里面的一个别名,这样是从外部看到?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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