简体   繁体   English

在子shell linux 中运行的脚本

[英]Scripts running in subshell linux

export x=4

script1.sh脚本1.sh

#!/bin/bash

echo $x

Running script1.sh outputs 4 in my current shell.在我当前的 shell 中运行 script1.sh 输出 4。

script2.sh脚本2.sh

#!/bin/bash

cd ~

Running script2.sh does not change my current directory to home directory.运行 script2.sh 不会将我的当前目录更改为主目录。

I know that scripts run in a subshell being separated from my current shell, but why does script1.sh output in my current shell, while script2.sh changes directory in the subshell, exits and then my working directory in the current shell stays the same? I know that scripts run in a subshell being separated from my current shell, but why does script1.sh output in my current shell, while script2.sh changes directory in the subshell, exits and then my working directory in the current shell stays the same ?

How do i know which scripts will actually have effect in my current shell?我怎么知道哪些脚本会对我当前的 shell 产生实际影响?

Variables that you export are passed to subshells and other processes that you start.export的变量将传递给您启动的子 shell 和其他进程。 They are visible to the subprocess as "environment variables."它们作为“环境变量”对子进程可见。 This is how script1.sh knows the value of $x.这就是 script1.sh 知道 $x 值的方式。

How do i know which scripts will actually have effect in my current shell?我怎么知道哪些脚本会对我当前的 shell 产生实际影响?

No changes made to the environment in a subshell affect your current shell.对子外壳中的环境所做的任何更改都不会影响您当前的 shell。

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

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