简体   繁体   中英

Scripts running in subshell linux

export x=4

script1.sh

#!/bin/bash

echo $x

Running script1.sh outputs 4 in my current shell.

script2.sh

#!/bin/bash

cd ~

Running script2.sh does not change my current directory to home directory.

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?

Variables that you export are passed to subshells and other processes that you start. They are visible to the subprocess as "environment variables." This is how script1.sh knows the value of $x.

How do i know which scripts will actually have effect in my current shell?

No changes made to the environment in a subshell affect your current shell.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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