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,590 points
115 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,380 points

Related Questions In Linux Administration

0 votes
1 answer

How to take input from user in bash script?

You can use if-else branch to check ...READ MORE

answered Jan 31, 2019 in Linux Administration by Omkar
• 69,040 points
120 views
0 votes
1 answer

Setting environment variables in linux

You need to add more information about ...READ MORE

answered May 13, 2019 in Linux Administration by ajs3033
• 7,280 points
76 views
0 votes
1 answer
0 votes
1 answer

How to increase Swap Memory in CentOS 7?

Follow the below steps and procedures: Prerequisites Must have ...READ MORE

answered Oct 5, 2018 in Linux Administration by Frankie
• 9,810 points
4,535 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,380 points
67 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,380 points
488 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,380 points
1,558 views