Bash Script if Statements: Compare Operators

๐Ÿ’ก Bash Script if Statements: Compare Operators โ€” Executive Summary

Technical architectural analysis, practical configuration manifests, and deployment instructions for Bash Script if Statements: Compare Operators.

if
Expression Use Features POSIX Compatibility
if ; then Branch based on command execution result Judging by the exit code after executing the command fully compatible
if [ ]; then Basic conditional expression evaluation Use external command test, space strict POSIX standard
if [[ ]]; then Extended conditional expression evaluation More flexible and safer, string pattern matching possible Bash/Zsh only (incompatible)
if (( }}; then Evaluation of arithmetic conditional expressions Dedicated to arithmetic expressions, optimized for integer operations and comparisons, built-in Bash/Zsh only (incompatible)
Features
POSIX std
bash
# ๋นˆ ์กฐ๊ฑด
if [ -z "$1" ]; then
  echo "๋น„์—ˆ์Œ"
fi
if [ -n "$1" ]; then
  echo "๋น„์–ด์žˆ์ง€ ์•Š์Œ"
fi
bash/zsh
bash
# ์‚ผํ•ญ์—ฐ์‚ฐ์ž
BOOL=$( [[ <์กฐ๊ฑด๋ฌธ ์‚ฝ์ž…> ]] && echo "true" || echo "false" )

# ๋ฌธ์ž์—ด์ด ํŠน์ • ๋ฌธ์ž์—ด์„ ํฌํ•จํ•˜๋Š”์ง€ ํ™•์ธ
if [[ "$ORIGIN" == *"MyString"* ]]; then
  echo "๋ฌธ์ž์—ด์ด ํฌํ•จ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค."
else
  echo "๋ฌธ์ž์—ด์ด ํฌํ•จ๋˜์–ด ์žˆ์ง€ ์•Š์Šต๋‹ˆ๋‹ค."
fi

# ๊ฐ™์Œ
if [[ "$COMPLETION" -eq 1 ]]; then
fi
ETC
bash
# sudo/root ํ™•์ธ
if [[ $EUID -eq 0 ]]; then
  echo "sudo/root ๊ถŒํ•œ์ž…๋‹ˆ๋‹ค."
fi

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *