I want to write a bash script that takes in two parameters, running them as commands and dump their output into files. myscript.sh contents:$1 > file1.tmp$2 > file2.tmpThis works fine for the following example:myscript.sh 'echo Hello World' 'echo Hello World 2'file1.tmp now contains the text Hello World and file2.tmp contains text Hello World  2However this breaks down when using a command that contains a pipe:myscript.sh 'ls | grep stuff' 'ls | grep morestuff'ls: cannot access |: No such file or directoryls: cannot access grep: No such file or directoryls: cannot access stuff: No such file or directoryls: cannot access |: No such file or directoryls: cannot access grep: No such file or directoryls: cannot access morestuff: No such file or directoryIt appears the pipe is being escaped because I get similar output as the first error if I for example run this:ls \|ls: cannot access |: No such file or directoryHow can I unescape the pipe in my script?
unescape pipe in parameters
shell script;pipe;escape characters
You're passing a piece of shell code into the script. To the script, this is just text. For it to be interpreted as a full-blown shell command, you will have to eval it:eval $1 >file1.tmpeval $2 >file2.tmpThis works when $1 and $2 are simple things like echo hello because they are simple commands (not lists or compound commands).An analogy with another programming language would be if you passed a piece of C code into a C program as a text string. The code would have to be compiled (and linked) somehow before it could be executed as part of the program.