0

Below is my simple if command which throws command not found but any it prints else part value...

-bash-3.00$ if ["$a"="10"]; then echo "hello"; else echo "hi"; fi;

output:

-bash: [10=10]: command not found
hi

Can any one please let me know what is the problem ?

1

1 Answer 1

3

Add spaces between [/] and predicate expression:

if [ "$a" = "10" ]; then echo "hello"; else echo "hi"; fi;
#   ^           ^

[ is a program:

$ which [
/usr/bin/[

Without space, [10=10] is recognized as a program name and is executed instead of [.


$ a=10
$ if [ "$a" = "10" ]; then echo "hello"; else echo "hi"; fi;
hello
$ a=20
$ if [ "$a" = "10" ]; then echo "hello"; else echo "hi"; fi;
hi
Sign up to request clarification or add additional context in comments.

5 Comments

I thought he also need to use -eq instead = . Is that right ?
@Priyatham51 since both arguments are quoted, = is fine
@KeithThompson It's actually the other way around. -eq is for numbers and = is for strings.
@thatotherguy: Yeah, I knew that (fumble fingers). I'll delete my previous comment and post a corrected version.
@Priyatham51: (Correcting my previous comment) -eq is for numeric comparisons. = is for string comparisons. man test or info coreutils test for details. Or info bash and look under "Bourne shell builtins" for test.

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.