Suppose I have a bash variable like this:
tmp1='$(echo foo)'
or
tmp2='`echo foo`'
How to achieve foo
as result?
I know that removing the command substitution from the string should work, but is there any other way despite this?
Suppose I have a bash variable like this:
tmp1='$(echo foo)'
or
tmp2='`echo foo`'
How to achieve foo
as result?
I know that removing the command substitution from the string should work, but is there any other way despite this?
The answer is "don't do this". This is simply not a thing that Bash is designed to do:
PS1
), it's already a string, in a variable, and it works just fine. Just that they need to tell the shell to evaluate it as shell syntax, which the shell is perfectly capable of doing, and has a dedicated command for. Not a good idea in general, since it's hard to get right for arbitrary input, but here, it looks like they have a pre-existing command they made themselves, so no untrusted input.
– ilkkachu
Apr 21 '22 at 08:43
tmp1
ortmp2
with the valuefoo
? Or do you just want the stringfoo
outputted? – Kusalananda Apr 21 '22 at 06:07echo
) wrapped in a command substitution in the first place? Why not e.g. have the command by itself in a variablea='echo foo'
(but see here), or just run the command substitution immediatelyb="$(echo foo)"
? – ilkkachu Apr 21 '22 at 07:16foo
as output is enough for me. I only wantted to exam errors inside. @Kusalananda – simonmysun Apr 21 '22 at 07:59