开发者

ksh not evaluating variable within backticks

开发者 https://www.devze.com 2023-01-25 07:19 出处:网络
This one has me stumped. #!/bin/ksh AWKSCRIPT=\'END { print \"all done\"; }\' OUTPUT=`echo hello world | awk \'$AWKSCRIPT\'`

This one has me stumped.

#!/bin/ksh

AWKSCRIPT='END { print "all done"; }'

OUTPUT=`echo hello world | awk '$AWKSCRIPT'`

RETVAL=$?

echo "running echo hello world | awk '$AWKSCRIPT'"
echo "Output = $OUTPUT"
echo "returned = $RETVAL"

The output is

$ ./kshawk.ksh
Output = hello world
returned = 0

(I was expecting to see "Output = all done")

It looks like the interpreter is not substituting the AWKSCRIPT variable when evaluating the expression (I get the same behaviour i开发者_StackOverflow中文版f I use $(...) instead of backticks).

While I could dump AWKSCRIPT to a temporary file - this would have to be hardcoded too?

Any ideas how to interpolate a variable within backticks?


The single quotes around '$AWKSCRIPT' prevent the interpolation of the variable. Double quotes do allow interpolation:

$ OUTPUT=`echo hello world | awk "$AWKSCRIPT"`
$ echo $OUTPUT
all done
0

精彩评论

暂无评论...
验证码 换一张
取 消