Test for a point in bash

How to check if there is any line in bash? I tried this:

if [ "$var" = "." ]

and this:

if [ "$var" = "\." ]

But that will not work.

+3
source share
3 answers

Works for me:

$ d=.
$ if [ $d = '.' ];then echo 'yes'; else echo 'no'; fi
yes
$ d=foo
$ if [ $d = '.' ];then echo 'yes'; else echo 'no'; fi
no
+4
source

My code is:

#!/bin/bash

var="."

[ $var = "." ] && echo "Yup, it equals '.'"

exit 0

What prints:

Yup, it equals '.'

Debugged:

tpost@tpost-desktop:~$ /bin/bash -x ./foo.sh
+ var=.
+ '[' . = . ']'
+ echo 'Yup, it equals '\''.'\'''
Yup, it equals '.'
+ exit 0

You probably have a space in $var(or maybe $varempty?), Run it through /bin/sh -x ./yourscript.shto see what it actually compares.

, == - , Bash. , =, . , bash, , /bin/sh . = .

+1

Try doubling the equals ( ==) values ; comparing is not the same as the purpose.

0
source

Source: https://habr.com/ru/post/1743519/


All Articles