Bash:Test
lastmod 2024-12-05created 2015-08-246 edits
Tests (Test Constructs).
- if/then tests whether the exit status of a command list is 0 (since by Unix convention 0 means "success"), and if so, runs the following commands.
- There is a dedicated test command,
[(the left bracket special character). It is a synonym for the test command and is a built-in for efficiency. This command treats its arguments as comparison expressions or file tests and returns an exit status according to the result of the operation (0 for true, 1 for false). - Also, since version 2.02, Bash provides
[[ ... ]], which offers comparison operations somewhat more familiar to programmers coming from other languages. Note that[[is a keyword, not a command. - Bash understands
[[ $a -lt $b ]]as a single element that returns an exit status.
Operator
Returns true if the following condition holds
-
-e: file exists -
-f: regular file (not a directory or device file) -
-s: file size is not 0 -
-d: file is a directory -
-b: file is a block device (floppy, CD-ROM, etc.) -
-c: file is a character device (keyboard, modem, sound card, etc.) -
-p: file is a pipe -
-h: file is a symbolic link -
-L: file is a symbolic link -
-S: file is a socket -
-t: file descriptor is associated with a terminal device. Can be used to check whether a script's standard input ([ -t 0 ]) or standard output ([ -t 1 ]) is a terminal. -
-r: the user running the test has read permission -
-w: the user running the test has write permission -
-x: the user running the test has execute permission -
-g: the set-group-id (sgid) flag is set on the file or directory -
-u: the set-user-id (suid) flag is set on the file -
-k: sticky bit (sticky bit) is set -
-O: you are the owner -
-G: group ID is the same as yours -
-N: modified since it was last read -
f1 -nt f2: file f1 is newer than file f2 -
f1 -ot f2: file f1 is older than file f2 -
f1 -ef f2: files f1 and f2 are hard links to the same file -
!: negation. -
-n: the string length is not 0. -
-z: the string length is 0.
[ vs [[
-
[[ ]]is the same as[ ]in the shell. This command is an extended test command borrowed from ksh88. - Between
[[and]], filename expansion and word splitting do not occur, but parameter expansion and command substitution do. - Using
[[ ... ]]instead of[ ... ]can prevent many logic errors. For example, the&&,||,<,>operators cause errors in[ ]but work fine in[[ ]].
Another advantage of using double brackets is that it is a bit easier to read when there are two or more conditions combined with AND/OR.
num=1
name="john"
# 조건식: num=1 AND name="john"
if [ $num -eq 1 -a $name = "john" ]
then echo "$num: $name"
fi
# 대괄호 두개 사용
if [[ $num -eq 1 ]] && [[ $name = "john" ]]
then echo "$num: $name"
fi
((
-
(( ... ))andlet ...return exit status 0 if the arithmetic expression they evaluate has a nonzero value. Therefore, this arithmetic expansion can be used for arithmetic comparisons.
Check broken symlink
# test if file exists (test actual file, not symbolic link)
if [ ! -e "$F" ] ; then
# code if the symlink is broken
fi