리눅스 set -u


개요

리눅스 set -u
  • Treat unset variables as an error when substituting.
  • 설정되지 않은 변수를 사용하려고 하면 스크립트를 종료한다.
이때 오류 메시지는 unbound variable, EXIT CODE는 1

실습 1

user01@localhost:~$ cat test1.sh 
HELLO="WORLD"
echo [$HELLO]
echo [$LOREM]
echo [$HELLO]
user01@localhost:~$ sh test1.sh
[WORLD]
[]
[WORLD]
→ 선언되지 않은 변수 $LOREM을 사용했지만 끝까지 수행되었다.
user01@localhost:~$ cat test2.sh 
set -u
HELLO="WORLD"
echo [$HELLO]
echo [$LOREM]
echo [$HELLO]
user01@localhost:~$ sh test2.sh
[WORLD]
test2.sh: line 4: LOREM: unbound variable
→ 선언되지 않은 변수 $LOREM을 사용하려고 하자, 오류 메시지와 함께 즉시 종료되었다.

실습 2

user01@localhost:~$ cat test3.sh 
HELLO="WORLD"
unset HELLO
echo [$HELLO]
echo [$HELLO]
user01@localhost:~$ sh test3.sh 
[]
[]
→ 해제된 변수 $HELLO를 사용했지만 끝까지 수행되었다.
user01@localhost:~$ cat test4.sh 
set -u
HELLO="WORLD"
unset HELLO
echo [$HELLO]
echo [$HELLO]
user01@localhost:~$ sh test4.sh 
test4.sh: line 4: HELLO: unbound variable
→ 해제된 변수 $HELLO를 사용하려고 하자, 오류 메시지와 함께 즉시 종료되었다.

같이 보기