Comparing two string variables in an if statement in Bash

0 votes

So, I'm trying to compare 2 string variables using if statement and trying to get it to work in Bash.

#!/bin/bash

s1="hi"
s2="hi"

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

I've tried many things, using [["$s1" == "$s2"]], with and without quotes, using =, == and -eq, but I still get the following error:

[hi: command not found

May 15, 2019 in Linux Administration by Upasana
• 8,620 points
1,329 views

1 answer to this question.

0 votes

1. For string comparison, use:

if [ "$s1" == "$s2" ]

For the a contains b, use:

2. if [[ $s1 == *"$s2"* ]]

answered May 15, 2019 by Shubham
• 13,490 points

Related Questions In Linux Administration

0 votes
1 answer

How to check if a string contains a substring in Bash?

You ought to recollect that shell prearranging ...READ MORE

answered May 27, 2022 in Linux Administration by Korak
• 5,820 points
865 views
0 votes
0 answers

How to split a string into an array in Bash?

In a Bash script, I would like ...READ MORE

Jun 9, 2022 in Linux Administration by Rahul
• 3,380 points
397 views
0 votes
0 answers

if, elif, else statement issues in Bash

I can't seem to work out what ...READ MORE

Jun 22, 2022 in Linux Administration by Korak
• 5,820 points
363 views
0 votes
1 answer

How do I check if a directory exists in a Bash shell script?

To check if a directory exists in ...READ MORE

answered Jun 7, 2022 in Linux Administration by Korak
• 5,820 points
1,478 views
0 votes
1 answer

Script file name in a Bash script

$0 will give you the complete basename. ...READ MORE

answered Jun 20, 2019 in Linux Administration by Shubham
• 13,490 points
552 views
0 votes
1 answer
+1 vote
3 answers
0 votes
1 answer

Using shell variables in an awk script

Using -v is, imo, The best way because ...READ MORE

answered Jul 1, 2019 in Linux Administration by Shubham
• 13,490 points
2,374 views
0 votes
1 answer

How to extract file base name without path and extension in bash?

You don't actually have to call the ...READ MORE

answered May 31, 2019 in Linux Administration by Shubham
• 13,490 points
5,502 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP