2

I explained my question in the comments:

VAR=
INS="Installing $VAR"

echo $INS   
.           # In each echo command I want to dynamically substitute
.           # the $VAR variable in the $INS variable. I want to do
echo $INS   # the substitution of the variable on echo command.

Is this possible?

5 Answers 5

5

You need a function to do the job gracefully.

say() {
    echo "Installing $INS"
}

INS=hello
say

INS=world
say

Or just this:

say() {
    echo "Installing $@"
}

say hello

say world
Sign up to request clarification or add additional context in comments.

6 Comments

A much better solution to the underlying problem, though it doesn't answer the OP's question. I do wish more people would ask questions about the problems they want to solve rather than the solutions they think they want to implement.
This look more suitable for my purpose. Thank you.
@user1405441 - this is the right solution, but be sure to reward with upvotes the answers that actually answered your question.
@ghoti: You may have seen this before, but just in case.
You are right @ghoti. I want to solve a problem, but I'm also open to learning different methods. I didn't know that I could use functions, that's why I couldn't ask the question correctly. I will upvote you when I have enough reputation :)
|
3

For example:

[ghoti@pc ~]$ cat varins
#!/bin/bash

msg='Installing "$VAR"'

for VAR in foo bar baz; do
  eval echo "$msg"
done

[ghoti@pc ~]$ ./varins
Installing foo
Installing bar
Installing baz
[ghoti@pc ~]$ 

This relies on the fact that $VAR won't be expanded inside single quotes. The eval command will expand the $msg variable, in which the shell will find $VAR to replace.

Comments

1

Parameter substitution can do it also:

ins='aaa $var aaa'
var='xxx'
echo "'${ins//\$var/$var}'"

result:

'aaa xxx aaa'

1 Comment

I tried something like this, but when I want to overwrite the variable it doesn't work. When I do it repetedly, I always get the first variable as the output
0
INS='Installing $VAR'
result=`eval "echo $INS"`
echo $result

you have to use single quotes to not substitute $VAR on creation of INS
eval evaluates $INS at runtime and with echo and backticks it is returned as substituted string

2 Comments

You don't need to (and probably shouldn't) run eval in a sub-shell like this. eval result="echo \"$INS\"" would do.
yes would do, but I wanted to separate the solving 'eval'-part from the using 'result='-part to make clear, what solves OPs problem; in general I would prefer kev's function solution anyway
-2

As Hachi said, you need single quotes to prevent $VAR from premature evaluation. Graham suggests in the comments to avoid the subshell call like this:

#!/bin/bash
VAR=17
INS='Installing $VAR'
eval "echo \"$INS\""
VAR=42
eval "echo \"$INS\""

If you don't do it:

VAR=23
INS="Installing $VAR"
echo $(eval "echo $INS")
VAR=8
echo $(eval "echo $INS")

23 is bound immediately, and the assignment VAR=8 isn't noticed.

Result:

Installing 17
Installing 42
Installing 23
Installing 23

3 Comments

As with Hachi, you don't need to (and probably shouldn't) run eval in a sub-shell like this. eval "echo \"$INS\"" will do just fine.
@Graham: Why shouldn't I run eval in a subshell? If you have an alternative solution - why don't you post it as an answer?
A subshell in this case is a waste of a process. echo $(echo …) is pointless. And I don't need to post an answer, because ghoti already posted exactly what I would have posted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.