lidia
lidia

Reputation: 3193

ksh + argument in PARAM

is it posible to print the value NUM (100) , while I must use only ' and not "

 # NUM=100
 # PARAM='some text $NUM'
 # echo $PARAM
   some text $value

the right print

some text 100

Upvotes: 1

Views: 137

Answers (2)

glenn jackman
glenn jackman

Reputation: 247022

extremely unsafe, but: eval echo $PARAM

I would strongly advise against this: what if param contains some destructive command in backticks? Either re-think your design or re-think your implementation language.

You could at least escape backticks and $() constructs first:

NUM=100
PARAM='this is $NUM -- `rm foo` -- $(rm bar)'
param_safer=$(echo "$PARAM" | sed 's/`/\\`/g; s/\$(\([^)]\+\))/\\$\\(\1\\)/g')
eval echo "$param_safer"

outputs: this is 100 -- `rm foo` -- $(rm bar)

Upvotes: 0

codaddict
codaddict

Reputation: 455282

Use double quotes in place of single quotes as single quotes do not allow variable interpolation:

NUM=100
PARAM="some text $NUM"
echo $PARAM

EDIT: Since I'm not allowed to use double quote, you can use concatenation as:

NUM=100
PARAM='some text '$NUM
echo $PARAM

Upvotes: 1

Related Questions