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 $p开发者_JS百科aram 2 ]] && print OK
Obvious that not illegal
but I wonder if it possible anyway with some changes?? Lidia
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
I'm not sure why you would want to do this, but you could use eval
for this:
eval [[ 2 $param 2 ]] && print OK
精彩评论