Bash linux if equal

How do I compare two string variables in an ‘if’ statement in Bash? [duplicate]

I’ve tried various forms of the if statement, using [[«$s1» == «$s2»]] , with and without quotes, using = , == and -eq , but I still get the following error:

I’ve looked at various sites and tutorials and copied those, but it doesn’t work — what am I doing wrong? Eventually, I want to say if $s1 contains $s2 , so how can I do that? I did just work out the spaces bit. :/ How do I say contains? I tried

12 Answers 12

For string equality comparison, use:

For string does NOT equal comparison, use:

For the a contains b , use:

(and make sure to add spaces between the symbols):

The double equals sign is an error in the first case. Bash tolerates it, but the portable variant is if [ «$s1» = «$s2» ] . See also Rahul’s answer

@Sangimed, [ is a command (actually, an alternate name for the command called test ); if you run which [ , you’ll see there’s actually an executable file for it on disk (even though the shell may provide a built-in implementation as a performance optimization). Just like you have to put a space between the name of the command ls before the name of the file you want it to print, you need to put a space after the name of the [ command and its first argument, and between each argument it’s passed (if invoked as [ rather than test , it expects its last argument to be ] ).

You should be careful to leave a space between the sign of ‘[‘ and double quotes where the variable contains this:

if [ "$s1" == "$s2" ]; then # ^ ^ ^ ^ echo match fi 

The ^ s show the blank spaces you need to leave.

Many thanks for pointing out the necessary space. Solved my problem. Just started bash today, seems to be a lot of times spaces can cause an error, i.e declaring variables etc.

== doesn’t work on ash, dash, or other places baseline POSIX implementations of test . Use = instead.

Just wanted to say to make sure to leave a space between the beginning and ending square brackets and the «$s1» == «$s2» statement or it will not work. Also, this works too: if test «$s1» = «$s2»

== doesn’t work on ash, dash, or other places baseline POSIX implementations of test . Use = instead.

Notice the white space between the openning/closing brackets and the variables and also the white spaces wrapping the ‘=’ sign.

Also, be careful of your script header. It’s not the same thing whether you use

Upvote, but always be careful when reading the ABS. Linking to a more authoritative source would probably be preferred.

@holms, that doesn’t happen with the OP’s code when used precisely as given here. You’ll need to show your exact usage.

Bash 4+ examples. Note: not using quotes will cause issues when words contain spaces, etc. Always quote in Bash IMO.

Here are some examples Bash 4+:

Example 1, check for ‘yes’ in string (case insensitive):

Читайте также:  Ping all hosts linux

Example 2, check for ‘yes’ in string (case insensitive):

if [[ "$(echo "$str" | tr '[:upper:]' '[:lower:]')" == *"yes"* ]] ;then 

Example 3, check for ‘yes’ in string (case sensitive):

Example 4, check for ‘yes’ in string (case sensitive):

Example 5, exact match (case sensitive):

Example 6, exact match (case insensitive):

This question has already great answers, but here it appears that there is a slight confusion between using single equal ( = ) and double equals ( == ) in

The main difference lies in which scripting language you are using. If you are using Bash then include #!/bin/bash in the starting of the script and save your script as filename.bash . To execute, use bash filename.bash — then you have to use == .

If you are using sh then use #!/bin/sh and save your script as filename.sh . To execute use sh filename.sh — then you have to use single = . Avoid intermixing them.

The assertion «you have to use == » is incorrect. Bash supports both = and == . Also, if you have #!/bin/bash at the start of your script, you can make it executable and run it like ./filename.bash (not that the file extension is important).

Perfect, I think I have to delete this answer now but it will be very helpful if you explain why this is not working without making the file executable and running by adding sh/bash before the filename?

This is confused about the significance of the shebang and the file name. If you have correctly put #!/bin/sh or #!/bin/bash as the first line of the script, you simply run it with ./filename and the actual file name can be completely arbitrary.

Now this is getting interesting,even if you don’t add any shebang and any extension and execute it using «bash/sh filename» it is working no matter what you use single equal or double equals.One more thing if you make the same file(without shebang and any extension) executable then you can execute it like ./filename (no matter of single or double equals).(Tried it on Arch linux with bash 4.3.46).

If you execute the file by running say «bash filename» — then you are just passing ‘filename’ as a parameter to the program ‘bash’ — which of course will result in bash running it. However if you set ‘filename’ execute permission, and try to run it by eg ‘./filename’ — then you are relying on the default ‘execute’ behaviour of your current command shell — which probably requires the «#!(shell)» line at the start of the script, in order to work.

#!/bin/bash s1="hi" s2="hi" if [ $s1 = $s2 ] then echo match fi 

Without the double quotes and with only one equals.

Why would you omit the double quotes? They are optional but harmless in this limited specific case, but removing them would be a serious bug in many real-world situations. See also stackoverflow.com/questions/10067266/…

I got same behavior with = or == regardless of quotes. I’m not sure if this is shell specific, I am using zsh on my mac OS version : 10.15.7 (19H15)

$ if [ "$s1" == "$s2" ]; then echo match; fi match $ test "s1" = "s2" ;echo match match $ 

The double equals sign is tolerated in Bash, but not in some other dialects. For portability, the single equals sign should be preferred, and if you target Bash only, the double brackets [[ extension would be superior for versatility, robustness, and convenience.

Читайте также:  Log об ошибках linux

I don’t have access to a Linux box right now, but [ is actually a program (and a Bash builtin), so I think you have to put a space between [ and the first parameter.

Also note that the string equality operator seems to be a single = .

The symbol [ was a link to /bin/test at one time (or perhaps vice versa). That is apparently no longer the case on ubuntu 16.04; no idea where or when the change occurred.

I believe Prisoner 13 was incorrect. ‘[‘ is equivalent to ‘test’ in Ubuntu v20, with the requirement that the last argument must be ‘]’.

This is more a clarification than an answer! Yes, the clue is in the error message:

which shows you that your «hi» has been concatenated to the «[«.

Unlike in more traditional programming languages, in Bash, «[» is a command just like the more obvious «ls», etc. — it’s not treated specially just because it’s a symbol, hence the «[» and the (substituted) «$s1» which are immediately next to each other in your question, are joined (as is correct for Bash), and it then tries to find a command in that position: [hi — which is unknown to Bash.

In C and some other languages, the «[» would be seen as a different «character class» and would be disjoint from the following «hi».

Hence you require a space after the opening «[«.

Источник

Bash linux if equal

if [ «$a» -eq «$b» ]

if [ «$a» -ne «$b» ]

if [ «$a» -gt «$b» ]

if [ «$a» -ge «$b» ]

if [ «$a» -lt «$b» ]

if [ «$a» -le «$b» ]

меньше или равно (внутри двойных круглых скобок)

больше (внутри двойных круглых скобок)

больше или равно (внутри двойных круглых скобок)

[[ $a == z* ]] # истина, если $a начинается с символа "z" (сравнение по шаблону) [[ $a == "z*" ]] # истина, если $a равна z* [ $a == z* ] # имеют место подстановка имен файлов и разбиение на слова [ "$a" == "z*" ] # истина, если $a равна z* # Спасибо S.C.

меньше, в смысле величины ASCII-кодов

больше, в смысле величины ASCII-кодов

Обратите внимание! Символ «>» необходимо экранировать внутри [ ].

См. Пример 25-6 относительно применения этого оператора сравнения.

строка «пустая» , т.е. имеет нулевую длину

Пример 7-5. Операции сравнения

#!/bin/bash a=4 b=5 # Здесь переменные "a" и "b" могут быть как целыми числами, так и строками. # Здесь наблюдается некоторое размывание границ #+ между целочисленными и строковыми переменными, #+ поскольку переменные в Bash не имеют типов. # Bash выполняет целочисленные операции над теми переменными, #+ которые содержат только цифры # Будьте внимательны! echo if [ "$a" -ne "$b" ] then echo "$a не равно $b" echo "(целочисленное сравнение)" fi echo if [ "$a" != "$b" ] then echo "$a не равно $b." echo "(сравнение строк)" # "4" != "5" # ASCII 52 != ASCII 53 fi # Оба варианта, "-ne" и "!=", работают правильно. echo exit 0

Пример 7-6. Проверка — является ли строка пустой

#!/bin/bash # str-test.sh: Проверка пустых строк и строк, не заключенных в кавычки, # Используется конструкция if [ . ] # Если строка не инициализирована, то она не имеет никакого определенного значения. # Такое состояние называется "null" (пустая) (это не то же самое, что ноль). if [ -n $string1 ] # $string1 не была объявлена или инициализирована. then echo "Строка \"string1\" не пустая." else echo "Строка \"string1\" пустая." fi # Неверный результат. # Выводится сообщение о том, что $string1 не пустая, #+не смотря на то, что она не была инициализирована. echo # Попробуем еще раз. if [ -n "$string1" ] # На этот раз, переменная $string1 заключена в кавычки. then echo "Строка \"string1\" не пустая." else echo "Строка \"string1\" пустая." fi # Внутри квадратных скобок заключайте строки в кавычки! echo if [ $string1 ] # Опустим оператор -n. then echo "Строка \"string1\" не пустая." else echo "Строка \"string1\" пустая." fi # Все работает прекрасно. # Квадратные скобки -- [ ], без посторонней помощи определяют, что строка пустая. # Тем не менее, хорошим тоном считается заключать строки в кавычки ("$string1"). # # Как указывает Stephane Chazelas, # if [ $string 1 ] один аргумент "]" # if [ "$string 1" ] два аргумента, пустая "$string1" и "]" echo string1=initialized if [ $string1 ] # Опять, попробуем строку без ничего. then echo "Строка \"string1\" не пустая." else echo "Строка \"string1\" пустая." fi # И снова получим верный результат. # И опять-таки, лучше поместить строку в кавычки ("$string1"), поскольку. string1="a = b" if [ $string1 ] # И снова, попробуем строку без ничего.. then echo "Строка \"string1\" не пустая." else echo "Строка \"string1\" пустая." fi # Строка без кавычек дает неверный результат! exit 0 # Спвсибо Florian Wisser, за предупреждение.

Пример 7-7. zmost

#!/bin/bash #Просмотр gz-файлов с помощью утилиты 'most' NOARGS=65 NOTFOUND=66 NOTGZIP=67 if [ $# -eq 0 ] # то же, что и: if [ -z "$1" ] # $1 должен существовать, но может быть пустым: zmost "" arg2 arg3 then echo "Порядок использования: `basename $0` filename" >&2 # Сообщение об ошибке на stderr. exit $NOARGS # Код возврата 65 (код ошибки). fi filename=$1 if [ ! -f "$filename" ] # Кавычки необходимы на тот случай, если имя файла содержит пробелы. then echo "Файл $filename не найден!" >&2 # Сообщение об ошибке на stderr. exit $NOTFOUND fi if [ $ != "gz" ] # Квадратные скобки нужны для выполнения подстановки значения переменной then echo "Файл $1 не является gz-файлом!" exit $NOTGZIP fi zcat $1 | most # Используется утилита 'most' (очень похожа на 'less'). # Последние версии 'most' могут просматривать сжатые файлы. # Можно вставить 'more' или 'less', если пожелаете. exit $? # Сценарий возвращает код возврата, полученный по конвейеру. # На самом деле команда "exit $?" не является обязательной, # так как работа скрипта завершится здесь в любом случае,

построение сложных условий проверки

Читайте также:  Linux чем занято устройство

exp1 -a exp2 возвращает true, если оба выражения, и exp1, и exp2 истинны.

exp1 -o exp2 возвращает true, если хотябы одно из выражений, exp1 или exp2 истинно.

Примечания

Как указывает S.C., даже заключение строки в кавычки, при построении сложных условий проверки, может оказаться недостаточным. [ -n «$string» -o «$a» = «$b» ] в некоторых версиях Bash такая проверка может вызвать сообщение об ошибке, если строка $string пустая. Безопаснее, в смысле отказоустойчивости, было бы добавить какой-либо символ к, возможно пустой, строке: [ «x$string» != x -o «x$a» = «x$b» ] (символ «x» не учитывается).

Источник

Оцените статью
Adblock
detector