-1
var_1="hi"
var_2="bye,hi,hello"
if [[ "$var_1" == *"$var_2"* ]];
then
   echo "value of var_1 in var_2";
else
   echo "sad =(";
fi

I would like check second variable contains first. That prints "Sad =(", why?

2
  • It's a typo: you should revert var1 and var2 as it's var_1 that should be into var_2 Commented Feb 28, 2023 at 13:49
  • 1
    if [[ "$var_2" =~ "$var_1" ]]; Commented Feb 28, 2023 at 13:49

1 Answer 1

0

The condition checks that $var_1 consists of $var_2, possibly preceded and followed by something. But it's the other way round.

if [[ $var_2 = *"$var_1"* ]]
#          ~          ~
Sign up to request clarification or add additional context in comments.

5 Comments

it should be == not = but yes (it's a typo question)
@Elikill58: Why? They both work (and it's documented).
it will change the variable, no ?
@Elikill58 No. Read choroba's comment again.
@Elikill58 perhaps you're thinking of awk where x=3 is an assignment and x==3 is a test/conditional ... ?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.