Program Tip

Bash에서 연산자 "="와 "=="의 차이점은 무엇입니까?

programtip 2020. 12. 4. 20:23
반응형

Bash에서 연산자 "="와 "=="의 차이점은 무엇입니까?


이 두 연산자는 거의 동일한 것 같습니다. 차이점이 있습니까? 언제 사용해야 =언제 ==?


==에서 숫자 비교에 사용해야합니다 (( ... )).

$ if (( 3 == 3 )); then echo "yes"; fi
yes
$ if (( 3 = 3 ));  then echo "yes"; fi
bash: ((: 3 = 3 : attempted assignment to non-variable (error token is "= 3 ")

[[ ... ]]또는 [ ... ]또는 에서 문자열 비교에 사용할 수 있습니다 test.

$ if [[ 3 == 3 ]]; then echo "yes"; fi
yes
$ if [[ 3 = 3 ]]; then echo "yes"; fi
yes
$ if [ 3 == 3 ]; then echo "yes"; fi
yes
$ if [ 3 = 3 ]; then echo "yes"; fi
yes
$ if test 3 == 3; then echo "yes"; fi
yes
$ if test 3 = 3; then echo "yes"; fi
yes

"문자열 비교?"라고 말합니까?

$ if [[ 10 < 2 ]]; then echo "yes"; fi    # string comparison
yes
$ if (( 10 < 2 )); then echo "yes"; else echo "no"; fi    # numeric comparison
no
$ if [[ 10 -lt 2 ]]; then echo "yes"; else echo "no"; fi  # numeric comparison
no

POSIX와 관련하여 미묘한 차이가 있습니다. Bash 참조 에서 발췌 :

string1 == string2
문자열이 같으면 참. 엄격한 POSIX 준수 =대신 사용할 수 있습니다 ==.

참고 URL : https://stackoverflow.com/questions/2600281/what-is-the-difference-between-operator-and-in-bash

반응형