3

I'm trying to debug an existing program and found an if condition without $ symbol prefixed to it.

Values are:

dt_val=1234
prev_dt_val=1234

If condition goes like:

if [ dt_val -eq prev_dt_val ]
then
  echo "Equal"
else
  echo "Not equal"
fi

Result:
Equal

Anyone throw some light on how the condition is working fine without a $ symbol?
Shouldn't that be..?

[ $dt_val -eq $prev_dt_val ]

The same condition fails when comparing string values. Does that mean, this condition does not require a $ symbol for number?

Additional info:
Comparing Strings with == as suggested:

dt_val="abcd"
prev_dt_val="abcd"

if [ dt_val == prev_dt_val ]
> then
> echo Equal
> else
> echo Not equal
> fi
Not equal

Shell Info:

echo $SHELL
/usr/bin/ksh
Version M-11/16/88f

Wondering why there's no error either.

  • It might help illustrate the situation better if you demonstrate unequal values comparing (successfully) as unequal, without $ – Jeff Schaller Jan 15 at 13:55
  • Really similar, but talks about [[ instead of [ -- unix.stackexchange.com/q/244685/117549 – Jeff Schaller Jan 15 at 14:02
  • If I was to guess, ksh is using the same code as [[ and is performing variable expansion "for" you. – Jeff Schaller Jan 15 at 14:22
  • Could you split the question into separate examples of string versus numeric values for testing? Using -eq would be wrong for strings (use = or != for strings). – Jeff Schaller Jan 15 at 14:41
  • 1
    The [ is a built-in command, and it may treat the -eq test as a proper arithmetic context. $ is not needed on variables in an arithmetic context. – Kusalananda Jan 15 at 16:14
4

In ksh, the builtin [ takes the operands of -eq as in an arithmetic context, just like Bash does for [[ and -eq. And in an arithmetic context, variables don't need the $ sign.

$ ksh -c 'a=1 b=1; if [ a -eq b ]; then echo equal; else echo different; fi'
equal
$ ksh -c 'a=1 b=2; if [ a -eq b ]; then echo equal; else echo different; fi'
different

or even:

$ ksh -c 'a=2 b=8; if [ a*4 -eq b ]; then echo equal; else echo different; fi'
equal
$ ksh -c 'a=2 b=9; if [ a*4 -eq b ]; then echo equal; else echo different; fi'
different

(However, the * still globs, so a*4 should be quoted there.)

That's ksh93, ksh --version shows sh (AT&T Research) 93u+ 2012-08-01, it's from Debian's package (ksh, package version 93u+20120801-3.1). I get the same result with Debian's mksh, so I suppose ksh88 is close enough here.


For comparison, in Bash that gives an error:

$ bash -c 'a=1 b=1; if [ a -eq b ]; then echo equal; else echo different; fi'
bash: line 0: [: a: integer expression expected
different

(it prints different, since [ returns a falsy value on error).

With [[ it works:

$ bash -c 'a=1 b=1; if [[ a -eq b ]]; then echo equal; else echo different; fi'
equal

Zsh is like Bash here, [ errors on a -eq b, [[ works.

0

Firstly, the script is not evaluatling the variables as it's missing the $ in front of the variable names, like you also discovered.

Secondly, it's outputting "Equal" because it's using the numeric compare operator -eq in the test ([) command. As it's passed two non-numerical strings as parameters, they both evaluate to the same numeric value and hence the "Equal" string is output.

Note that at least by bash version complains:

$ if [ 0 -eq bla ]; then echo yes; fi
-bash: [: bla: integer expression expected

My dash version also complains:

$ if [ 0 -eq bla ]; then echo yes; fi
dash: 1: [: Illegal number: bla

I'm interested what shell you are using that doesn't complain.

Even if there are variables present with the names of those strings I get the same result.

  • but if you set dt_val=5, the test fails... – Jeff Schaller Jan 15 at 13:55
  • I'm using ksh. It gives me the error when i'm comparing string values such as "abcd" ksh: abcd: 0403-009 The specified number is not valid for this command – Santhosh Ram Jan 15 at 14:11
  • @JeffSchaller, No, it's not failing for me. I get the result as Equal – Santhosh Ram Jan 15 at 14:12
  • dt_val=5 prev_dt_val=6 if [ dt_val -eq prev_dt_val ] then echo equal else echo not equal fi not equal – Santhosh Ram Jan 15 at 14:15
  • @SanthoshRam what version of ksh? – Jeff Schaller Jan 15 at 14:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.