Linux: source vs. ./execution

15 篇文章 0 订阅
本文详细介绍了在Linux shell中使用source(或dot运算符)与直接执行脚本的区别。source命令会在当前shell环境中运行脚本,其修改的变量和环境设置会保留在当前shell中;而直接执行脚本会创建新的子shell,脚本的更改仅在子shell内生效,关闭后即消失。通过示例展示了source和执行脚本对环境变量和工作目录的影响,帮助理解两者的不同用法。
摘要由CSDN通过智能技术生成

short answer: source or dot operator Man Page - Linux - SS64.com

Read and execute commands from the filename argument in the current shell context.

Syntax
      . filename [arguments]

      source filename [arguments]

source is a synonym for dot/period '.' in bash, but not in POSIX sh, so for maximum compatibility use the period.

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. In contrast if the script is run just as filename, then a separate subshell (with a completely separate set of variables) would be spawned to run the script.

detailed and very well versed answer: linux - What is the difference between executing a Bash script vs sourcing it? - Super User

Both sourcing and executing the script will run the commands in the script line by line, as if you typed those commands by hand line by line.

The differences are:

  • When you execute the script you are opening a new shell, type the commands in the new shell, copy the output back to your current shell, then close the new shell. Any changes to environment will take effect only in the new shell and will be lost once the new shell is closed.
  • When you source the script you are typing the commands in your current shell. Any changes to the environment will take effect and stay in your current shell.

the "environment" are things like the current working directory and environment variables. also shell settings (among others history and completion features). there are more but those are the most visible.

Use source if you want the script to change the environment in your currently running shell. use execute otherwise.

If you want more details please read on.

Terminology

To clarify some common confusion about the syntax to execute and the syntax to source.

Syntax to execute:

./myscript

This will execute myscript provided that the file is executable and located in the current directory. The leading dot and slash (./) denotes the current directory. This is necessary because the current directory is usually not (and usually should not be) in $PATH.

myscript

This will execute myscript if the file is executable and located in some directory in $PATH.

Syntax to source:

source myscript

This will source myscript. The file need not be executable but it must be a valid shell script. The file can be in current directory or in a directory in $PATH.

. myscript

This will also source myscript. This "spelling" is the official one as defined by POSIX. Bash defined source as an alias to the dot.

and for completeness:

exec myscript

This will terminate the current shell and then execute myscript in place of the terminated shell. That means when myscript is done there no shell to return to. exec is powerful but rarely needed.

I put some links at the end for more information on these topics.

Demonstration

Consider myscript.sh with following content:

#!/bin/sh
# demonstrate setting a variable
echo "foo: "$(env | grep FOO)
export FOO=foo
echo "foo: "$(env | grep FOO)
# demonstrate changing of working directory
echo "PWD: "$PWD
cd somedir
echo "PWD: "$PWD

Before we execute the script first we check the current environment:

$ env | grep FOO
$ echo $PWD
/home/lesmana

The variable FOO is not defined and we are in the home directory.

Now we execute the file:

$ ./myscript.sh
foo:
foo: FOO=foo
PWD: /home/lesmana
PWD: /home/lesmana/somedir

Check the environment again:

$ env | grep FOO
$ echo $PWD
/home/lesmana

The variable FOO is not set and the working directory did not change.

The script output clearly shows that the variable was set and the directory was changed. The check afterwards show that the variable is not set and the directory not changed. What happened? The changes were made in a new shell.

The current shell spawned a new shell to run the script. The script is running in the new shell and all changes to the environment take effect in the new shell. After the script is done the new shell is destroyed. All changes to the environment in the new shell are destroyed with the new shell. Only the output text is printed in the current shell.

Now we source the file:

$ source myscript.sh
foo:
foo: FOO=foo
PWD: /home/lesmana
PWD: /home/lesmana/somedir

Check the environment again:

$ env | grep FOO
FOO=foo
$ echo $PWD
/home/lesmana/somedir

The variable FOO is set and the working directory has changed.

Sourcing the script does not create a new shell. All commands are run in the current shell and changes to the environment take effect in the current shell.

Note that in this simple example the output of executing is the same as sourcing the script. This is not necessarily always the case.

Another Demonstration

Consider following script pid.sh:

#!/bin/sh
echo $$

(the special variable $$ expands to the PID of the current running shell process)

First print the PID of the current shell:

$ echo $$
25009

Source the script:

$ source pid.sh
25009

Execute the script, note the PID:

$ ./pid.sh
25011

Source again:

$ source pid.sh
25009

Execute again:

$ ./pid.sh
25013

You can see that sourcing the script runs in the same process while executing the script creates a new process everytime. That new process is the new shell which was created for the execution of the script. Sourcing the script does not create a new shell and thus the PID stays the same.

Summary

Sourcing a script will run the commands in the current shell process. Changes to the environment take effect in the current shell.

Executing a script will run the commands in a new shell process. Changes to the environment take effect in the new shell and is lost when the script is done and the new shell is terminated.

Use source if you want the script to change the environment in your currently running shell. use execute otherwise.


See also:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值