I\'m well aware of the source
(aka .
) utility, which will take the contents from a file and execute them within the current shell.
Now, I\'
I believe this is "the right answer" to the question:
ls | sed ... | while read line; do $line; done
That is, one can pipe into a while
loop; the read
command command takes one line from its stdin
and assigns it to the variable $line
. $line
then becomes the command executed within the loop; and it continues until there are no further lines in its input.
This still won't work with some control structures (like another loop), but it fits the bill in this case.
To use the mark4o's solution on bash 3.2 (macos) a here string can be used instead of pipelines like in this example:
. /dev/stdin <<< "$(grep '^alias' ~/.profile)"
Why not use source
then?
$ ls | sed ... > out.sh ; source out.sh