How to execute the output of a command within the current shell?

前端 未结 9 1158
渐次进展
渐次进展 2020-12-04 07:37

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\'

相关标签:
9条回答
  • 2020-12-04 08:16

    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.

    0 讨论(0)
  • 2020-12-04 08:16

    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)"
    
    0 讨论(0)
  • 2020-12-04 08:19

    Why not use source then?

    $ ls | sed ... > out.sh ; source out.sh
    
    0 讨论(0)
提交回复
热议问题