tags:

views:

15

answers:

2

hi

Subject shell script

if I perform compare on the following

 [[ 2 -eq 2 ]] && print OK

I get OK

But how to compare if the "-eq" is in my param

for example

param="-eq"

  [[ 2 $param 2 ]] && print OK

Obvious that not illegal

but I wonder if it possible anyway with some changes?? Lidia

A: 

I'm not sure why you would want to do this, but you could use eval for this:

eval [[ 2 $param 2 ]] && print OK
Peter van der Heijden
nice -:) and what about if i perform ( if [[ ....... ]] then ) in this case where I need to put the eval command?
lidia
@lidia: You can put the `eval` right after the `if`: `if eval [[ ... ]]`
Peter van der Heijden
+1  A: 

The [[ construct was designed precisely so that operators (such as -eq) must be specified directly, to avoid strings coming from parameter expansion being accidentally interpreted as parameters.

Use [, which is an ordinary command with fairly similar syntax to [[. Since it's an ordinary command, parameter expansion happens normally.

[ 2 $param 2 ] && print OK

Gilles

related questions