0

I am trying to execute variable concated from other variables and strings as command in shell-script. could you help me?


inside file (executable shell-script):

#!/bin/bash


var1=aaa

var2=bbb

var3=ccc


var=$var1$var2' '$var3' > '$var3'.out &'

echo $var


desired output:    aaabbb ccc > ccc.out &

aaabbb ccc > ccc.out &   ...execute as command???

echo ... 
eval ...
???

How can I write it in shell-script for output of echo $var to be executed as command?

When I run this command (aaabbb ccc > ccc.out &) "manualy" in terminal it works fine, but when I use it as variable in shell-script and execute it, then I do get an error - command not found.

I had tried many combinations with/without quotes, but I have had no luck. Also tried eval and echo.

Scripter
  • 19
  • 5
  • 3
    Note that you **really shouldn't** do this. Feeding variables in at the top of the shell parser introduces serious security concerns you don't have when starting them at the parameter expansion stages, which are designed to be safe. – Charles Duffy Sep 04 '20 at 16:11
  • 3
    See [BashFAQ #48: `eval` command and security issues](https://mywiki.wooledge.org/BashFAQ/048); and [BashFAQ #50: I'm trying to put a command in a variable, but the complex cases always fail!](http://mywiki.wooledge.org/BashFAQ/050) for discussion of the practices to use instead. – Charles Duffy Sep 04 '20 at 16:11
  • 2
    ...really, read BashFAQ #50; it addresses each of the reasons you might want to put code in a variable, and discusses what you should be doing instead for each case. – Charles Duffy Sep 04 '20 at 16:13

0 Answers0