0

I am writing a shell script at which I am trying to compare 2 variables that are strings. Every thing works fine as in all the variables have a value from the commands however my if then else statement is not working.

#!/bin/bash

name=$1

for i in {1...10} 

do        
username=sudo cat /class/rolls/CSCE215-*|awk 'BEGIN {FIELDWIDTHS = "32 20"} {print $2}'|cut -d " " -f6 |sed -n '1,1p'

if ["$name" == "$username"] 
then  
echo Correct Username
else
echo Incorrect Username
fi  

done

All of the tutorials and help online appears to have what I have here but I cannot find out what is wrong with mine.

2
  • 2
    You should add spaces after [ and before ]: if [ "$name" == "$username" ] Commented Sep 16, 2014 at 22:16
  • 1
    Use ShellCheck Commented Sep 16, 2014 at 22:33

2 Answers 2

2

You are using the "classic test" but it's a good idea to ALWAYS use the newer conditional expression: http://wiki.bash-hackers.org/syntax/ccmd/conditional_expression

if [[ "$name" == "$username" ]]

As far as I know the test command (with a single bracket) doesn't even officially support the "==" operator..

Oh, and of course don't forget the spaces inside the brackets, bash needs them to break the line up into words.

Sign up to request clarification or add additional context in comments.

Comments

1

When using test or [, the correct comparison is:

test "$string1" = "string2"

or

[ "$sting1" = "$string2" ]

Note: the single = instead of ==, and always quote sting variables. Further, there is nothing wrong with using the test or [ operators, in fact, they are preferred when portability is needed. They simply lack some of the extended functionality of the [[ operator, such as character class comparison and the ability to use =~.

Now when using the [[ operator, the correct form is:

[[ "$sting1" == "$string2" ]]

Note: as pointed out, quotes are not required when using the [[ operator, but if you get in the habit of always quoting strings, you will be safe in both cases.

5 Comments

+1: s/sting/string and quotes are not required inside [[ .. ]].
Correct, but as a general rule, I prefer to explain it with quoted variables for the benefit of those that lack that depth of understanding on whether quotes are required with [ or [[. That way they default to a safe expression.
You are right that test is preferred for portability. It does have a lot of nasty pitfalls. In many cases portability is not an issue (most of the time you're pretty sure you're always going to run the script with bash) and so I'd say the conditional expression is actually preferred.
Just another thing: the conditional expression does not just extend functionality. It is a part of bash syntax - the test operator [ is not - it is a regular command. Word splitting and pathname expansion are not done on a conditional expression and that is why - in my opinion at least - it should be preferred over the test command whenever portability is not necessary.
@DavidC.Rankin = and == are synonymous in bash. = is only preferred if you want portability.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.