After set -u in the shell, using variables that are not initialized will cause an error
set -u: means that after this command, when the command uses an undefined variable, the script exits directly, and subsequent commands are not executed.
Let's try it.
Prepare a script:
echo "1:$1"
Before set -u, $1 can exist or not, and no error will be reported when running:
$ /bin/bash 1: $ /bin/bash 2 1:2
/bin/bash -u executes the script equivalent to doing set -u and /bin/bash -u in the subshell. If $1 does not exist, an error will be reported:
$ /bin/bash -u 2 1:2 $ /bin/bash -u : OK 1: $1: Unbound variables
Let's try the impact of set +u | set-u on the current process and child processes:
Prepare another script:
#!/bin/bash echo "aaa : $aaa"
run:
$ set -u $ echo $bbb -bash: bbb: Unbound variables $ ./ aaa : $ . -bash: aaa: Unbound variables
$ set +u $ echo $bbb $ ./ aaa : $ . aaa :
It can be seen that set +u | set -u only acts on this process and will not act on child processes.
This is the article about the specific use of shell set -u and set +u. For more related shell set -u and set +u, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!