当文件在Unix/Linux上下文中是`source` -d时会发生什么? [英] What occurs when a file is `source`-d in Unix/Linux context?

查看:64
本文介绍了当文件在Unix/Linux上下文中是`source` -d时会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看过的shell脚本中包括如下一行:

I've seen shell scripts that include a line such as:

source someOtherFile

我知道这会导致someOtherFile的内容执行,但是source的意义是什么?

I know that causes the content of someOtherFile to execute, but what is the significance of source?

后续问题:可以获取任何脚本,还是仅获取某些类型的脚本?脚本的来源(与正常执行相反)是否有除环境变量以外的其他副作用?

Follow-up questions: Can ANY script be sourced, or only certain type of scripts? Are there any side-effects other than environment variables when a script is sourced (as opposed to normally executing it)?

推荐答案

在脚本上运行命令source会在当前进程的上下文中执行脚本.这意味着由脚本设置的环境变量在完成运行后仍然可用.这与正常运行脚本相反,在这种情况下,一旦脚本退出,新生成的进程中设置的环境变量将丢失.

Running the command source on a script executes the script within the context of the current process. This means that environment variables set by the script remain available after it's finished running. This is in contrast to running a script normally, in which case environment variables set within the newly-spawned process will be lost once the script exits.

您可以获取任何可运行的Shell脚本.最终效果将与您在终端中键入脚本中的命令一样.例如,如果脚本更改目录,则在完成运行后,您当前的工作目录将已更改.

You can source any runnable shell script. The end effect will be the same as if you had typed the commands in the script into your terminal. For example, if the script changes directories, when it finishes running, your current working directory will have changed.

这篇关于当文件在Unix/Linux上下文中是`source` -d时会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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