簡體   English   中英

在子shell linux 中運行的腳本

[英]Scripts running in subshell linux

export x=4

腳本1.sh

#!/bin/bash

echo $x

在我當前的 shell 中運行 script1.sh 輸出 4。

腳本2.sh

#!/bin/bash

cd ~

運行 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 ?

我怎么知道哪些腳本會對我當前的 shell 產生實際影響?

export的變量將傳遞給您啟動的子 shell 和其他進程。 它們作為“環境變量”對子進程可見。 這就是 script1.sh 知道 $x 值的方式。

我怎么知道哪些腳本會對我當前的 shell 產生實際影響?

對子外殼中的環境所做的任何更改都不會影響您當前的 shell。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM