Skip to main content
removing unuseful information
Source Link

Suppose I have a bash variable like this:

tmp1='$(echo foo)' 

or

tmp2='`echo foo`' 

How to achieve foo as result?

Things I've tried so far (with POSIX command subsitution as example):

> tmp='$(echo foo)' > echo $tmp $(echo foo) > echo "$tmp" $(echo foo) > echo $"$tmp" $(echo foo) > echo $($tmp) bash: $(echo: command not found > echo `$tmp` bash: $(echo: command not found > echo $("$tmp") bash: $(echo foo): command not found > bash -c $tmp foo): -c: line 1: unexpected EOF while looking for matching `)' foo): -c: line 2: syntax error: unexpected end of file > bash -c "$tmp" bash: line 1: foo: command not found > bash -c "\"$tmp\"" bash: line 1: foo: command not found > echo $(bash -c "$tmp") bash: line 1: foo: command not found > eval $tmp bash: foo: command not found > eval "$tmp" bash: foo: command not found > eval '$tmp' bash: $(echo: command not found 

none of them gets foo and I am very much surprised and confused about the difference between echo "$tmp" and bash -c "$tmp".

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?

Things I've tried so far (with POSIX command subsitution as example):

> tmp='$(echo foo)' > echo $tmp $(echo foo) > echo "$tmp" $(echo foo) > echo $"$tmp" $(echo foo) > echo $($tmp) bash: $(echo: command not found > echo `$tmp` bash: $(echo: command not found > echo $("$tmp") bash: $(echo foo): command not found > bash -c $tmp foo): -c: line 1: unexpected EOF while looking for matching `)' foo): -c: line 2: syntax error: unexpected end of file > bash -c "$tmp" bash: line 1: foo: command not found > bash -c "\"$tmp\"" bash: line 1: foo: command not found > echo $(bash -c "$tmp") bash: line 1: foo: command not found > eval $tmp bash: foo: command not found > eval "$tmp" bash: foo: command not found > eval '$tmp' bash: $(echo: command not found 

none of them gets foo and I am very much surprised and confused about the difference between echo "$tmp" and bash -c "$tmp".

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?

more failed examples
Source Link

Suppose I have a bash variable like this:

tmp1='$(echo foo)' 

or

tmp2='`echo foo`' 

How to achieve foo as result?

Things I've tried so far (with POSIX command subsitution as example):

> tmp='$(echo foo)' > echo $tmp $(echo foo) > echo "$tmp" $(echo foo) > echo $"$tmp" $(echo foo) > echo $($tmp) bash: $(echo: command not found > echo `$tmp` bash: $(echo: command not found > echo $("$tmp") bash: $(echo foo): command not found > bash -c $tmp foo): -c: line 1: unexpected EOF while looking for matching `)' foo): -c: line 2: syntax error: unexpected end of file > bash -c "$tmp" bash: line 1: foo: command not found > bash -c "\"$tmp\"" bash: line 1: foo: command not found > echo $(bash -c "$tmp") bash: line 1: foo: command not found > eval $tmp bash: foo: command not found > eval "$tmp" bash: foo: command not found > eval '$tmp' bash: $(echo: command not found 

none of them gets foo and I am very much surprised and confused about the difference between echo "$tmp" and bash -c "$tmp".

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?

Things I've tried so far (with POSIX command subsitution as example):

> tmp='$(echo foo)' > echo $tmp $(echo foo) > echo "$tmp" $(echo foo) > echo $"$tmp" $(echo foo) > echo $($tmp) bash: $(echo: command not found > echo `$tmp` bash: $(echo: command not found > echo $("$tmp") bash: $(echo foo): command not found > bash -c $tmp foo): -c: line 1: unexpected EOF while looking for matching `)' foo): -c: line 2: syntax error: unexpected end of file > bash -c "$tmp" bash: line 1: foo: command not found > bash -c "\"$tmp\"" bash: line 1: foo: command not found > echo $(bash -c "$tmp") bash: line 1: foo: command not found 

none of them gets foo and I am very much surprised and confused about the difference between echo "$tmp" and bash -c "$tmp".

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?

Things I've tried so far (with POSIX command subsitution as example):

> tmp='$(echo foo)' > echo $tmp $(echo foo) > echo "$tmp" $(echo foo) > echo $"$tmp" $(echo foo) > echo $($tmp) bash: $(echo: command not found > echo `$tmp` bash: $(echo: command not found > echo $("$tmp") bash: $(echo foo): command not found > bash -c $tmp foo): -c: line 1: unexpected EOF while looking for matching `)' foo): -c: line 2: syntax error: unexpected end of file > bash -c "$tmp" bash: line 1: foo: command not found > bash -c "\"$tmp\"" bash: line 1: foo: command not found > echo $(bash -c "$tmp") bash: line 1: foo: command not found > eval $tmp bash: foo: command not found > eval "$tmp" bash: foo: command not found > eval '$tmp' bash: $(echo: command not found 

none of them gets foo and I am very much surprised and confused about the difference between echo "$tmp" and bash -c "$tmp".

I know that removing the command substitution from the string should work, but is there any other way despite this?

Source Link

How to execute a string of bash command with command substitution

Suppose I have a bash variable like this:

tmp1='$(echo foo)' 

or

tmp2='`echo foo`' 

How to achieve foo as result?

Things I've tried so far (with POSIX command subsitution as example):

> tmp='$(echo foo)' > echo $tmp $(echo foo) > echo "$tmp" $(echo foo) > echo $"$tmp" $(echo foo) > echo $($tmp) bash: $(echo: command not found > echo `$tmp` bash: $(echo: command not found > echo $("$tmp") bash: $(echo foo): command not found > bash -c $tmp foo): -c: line 1: unexpected EOF while looking for matching `)' foo): -c: line 2: syntax error: unexpected end of file > bash -c "$tmp" bash: line 1: foo: command not found > bash -c "\"$tmp\"" bash: line 1: foo: command not found > echo $(bash -c "$tmp") bash: line 1: foo: command not found 

none of them gets foo and I am very much surprised and confused about the difference between echo "$tmp" and bash -c "$tmp".

I know that removing the command substitution from the string should work, but is there any other way despite this?