子shell & 父shell(当前shell) relationship between subshell and shell

Solemnly declare that the shell script is executed carefully, executed carefully, executed carefully! ! !

First define the variables in the current shell:

some_dir=/home/jiku (a non-existent directory!!!)

#Start writing script

#touch subshell.sh

#vi subshell.sh

cd $some_dir

#!/bin/bash

cd $some_dir

if [ $? -eq 0 ]; then
    rm -rf *
else
    echo 'cannot change directory!'

exit 1
fi

#chmod +x subshell.sh

#./subshell.sh

Then it was a tragedy! ! ! !

the reason:

Under normal circumstances, the script should report an error, but

Yes./subshell execution mode is based on the current shell and then start a subshell to execute this script

so

The variable $some_dir is not defined in the subshell, so it is empty

#cd (empty) will return to the user's home directory, the cd command is also executed successfully, the return value is 0

So I sent away the home directory directly away. . . . . .

 

========================================================

Solution 1:

Define variables in the script

cd $some_dir

#!/bin/bash
some_dir=/home/jiku
cd $some_dir

if [ $? -eq 0 ]; then
    rm -rf *
else
    echo 'cannot change directory!'

exit 1
fi

Solution 2:

#some_dir=/home/jiku

#export some_dir

Define variables in the current shell

Then use the export command to set the variables of the current shell to environment variables

=========================================

conclusion:

The attributes inherited by the child Shell from the parent Shell are as follows:

               Current working directory

               Environment variable

               Standard input, standard output, and standard error output

               Identifiers of all opened files

Properties that the child shell cannot inherit from the parent shell:

Shell variables other                than environment variables and variables defined in the .bashrc file      (that is, temporarily defined variables)

==========================================

Excerpt from:

 

There are two ways to execute Scripts in Linux, the main difference is whether to establish a subshell

1. source filename or. Filename
does not create a subshell, reads and executes the commands in filename in the current shell environment, which is equivalent to executing the commands in filename sequentially

2.
Create a subshell with bash filename or ./filename , and create a new subshell in the current bash environment to execute the commands in filename. The
subshell inherits the variables of the parent shell, but the subshell cannot use the variables of the parent shell unless export is used.
[Remarks: This is similar to namespaces, and even similar to functions in c]

The attributes inherited by the child Shell from the parent Shell are as follows:

Current working directory
Environment variables
Standard input, standard output, and standard error output
All opened file identifiers
Ignored signals
The attributes that the child shell cannot inherit from the parent shell are summarized as follows:

Shell variables other than environment variables and variables defined in the .bashrc file are
not ignored signal processing
3. $ (commond)
Its role is to make the command execute in the subshell

4. `commond` is
similar to $(commond).
[The "` "symbol here is an apostrophe (back quote), not a single quote, but the key below the Esc key on the keyboard.

5. exec commond
replaces the current shell but does not create a new process. The pid of the process remains unchanged.
Role:
The built-in command exec of the shell will not start a new shell, but replace the current shell process with the command to be executed, and clean up the environment of the old process, and other things after the exec command The command will no longer be executed.
After executing exec ls in a shell, the current directory will be listed, and then the shell will exit by itself. (Subsequent commands will no longer be executed)
Because this shell has been replaced with a process that only executes the ls command, it naturally exits when the execution ends.
When needed, you can use sub shell to avoid this effect. Generally, you put the exec command in a shell script, call this script with the main script, and use bash a.sh at the calling point (a.sh is the script that stores the command). This will create a sub shell for a.sh to execute, and when executed to exec, the sub script process is replaced with the corresponding exec command.
————————————————
Copyright Statement: This article is the original article of the CSDN blogger "peacewind", which follows the CC 4.0 BY-SA copyright agreement. Please attach the original source link and this statement for reprinting .
Original link: https://blog.csdn.net/NOStandby/article/details/82914930

 

Guess you like

Origin blog.csdn.net/Vast_Wang/article/details/106171252