views:

24

answers:

2

hi

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
+1  A: 

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
codaddict
but the main question was about to use single quotesI cant to use " ,
lidia
@lidia: take a look at my edited answer.
codaddict
I look but the NUM must be in the middle its not good for me
lidia
@lidia: **in the middle** I don't get it.
codaddict
$NUM must be between the single quotes not after the quote
lidia
@lidia: AFAIK that is not possible. Lets wait for more answers.
codaddict
@lidia: Why these artificial restrictions?
Dennis Williamson
A: 

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)

glenn jackman