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
161 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,480 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,090 points
210 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
264 views
0 votes
1 answer

How can I find out which files differ in two directory trees?

Use this; diff -qr dir1/ dir2/ READ MORE

answered Jun 13, 2019 in Linux Administration by Upasana
• 8,620 points
148 views
0 votes
1 answer

Select sections of text from each line of files in a Bash shell.

Hi@akhtar, There is no inbuild command available for ...READ MORE

answered Oct 19, 2020 in Linux Administration by MD
• 95,040 points
75 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,480 points
110 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,480 points
838 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,480 points
2,953 views