How do I execute commands that I print out on the standard output using awk for example, I use awk to print this out:

grep -B100 some file1.tcl
grep -B100 some file2.tcl

Then store in it in a .sh file and then source the file. Is it possible to pipe it to somewhere so that the commands are executed?

awk '{print xx}' | pipeline

3 Answers 3


Just pipe them to your shell:

$ printf 'echo foo\necho bar\n' | bash
  • I'd strongly suggest process substitution instead so that some command doesn't gobble subsequent commands as part of its stdin.
    – muru
    Apr 27, 2018 at 5:30

For anything more advanced than a simple command (and simple commands too, of course):

eval "$( awk ... )"

This would run the code in the environment of the current shell and the code could therefore also set och modify variables in your shell.

However, since you are obviously wanting to parse other files using grep, why not do that with the same awk program? ... or with another awk program that reads patterns and filenames?


You don't even need to pipe it, since you're already living dangerously; just run your command that always only ever outputs safe commands to execute in a subshell expansion construct:

$ $( awk 'BEGIN { print "echo hello" }' )
  • This does not interact well with quotes and word-splitting. If awk were to print "touch \"Hello world.txt\"" the shell wouldn't honor the quoting – it would instead create files "Hello and world.txt". Apr 27, 2018 at 7:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .