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 in Linux Administration by Upasana
• 8,550 points
66 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 by Shubham
• 13,350 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 in Linux Administration by Omkar
• 68,180 points
49 views
0 votes
1 answer

Setting environment variables in linux

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

answered May 13 in Linux Administration by ajs3033
• 7,280 points
33 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
1,484 views
0 votes
1 answer

Script file name in a Bash script

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

answered Jun 20 in Linux Administration by Shubham
• 13,350 points
35 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 in Linux Administration by Shubham
• 13,350 points
87 views
0 votes
1 answer