Hawken
Hawken

Reputation: 2119

What is the difference between bracketed and non-bracketed if-statements in bourne shell?

For instance:

if $input = 'y'

causes

./test.sh: line 5: y: command not found

While

if [ $tmp = 'y' ]

causes no error?

What is the purpose of an if statement that cannot evaluate conditions?

Upvotes: 0

Views: 203

Answers (1)

mike3996
mike3996

Reputation: 17517

if doesn't evaluate anything. It simply executes the predicate program and acts on its return code. [ is actually a program that does a handful of conditions, and returns an appropriate return code.

[ can be used with oneliners without if, if you don't need the else branch:

$ [ 1 -eq 2 ] && echo "foo"
# -> no output

Upvotes: 3

Related Questions