Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to execute the output of a command within the current shell?
    text
    copied!<p>I'm well aware of the <code>source</code> (aka <code>.</code>) utility, which will take the contents from a file and execute them within the current shell.</p> <p>Now, I'm transforming some text into shell commands, and then running them, as follows:</p> <pre><code>$ ls | sed ... | sh </code></pre> <p><code>ls</code> is just a random example, the original text can be anything. <code>sed</code> too, just an example for transforming text. The interesting bit is <code>sh</code>. I pipe whatever I got to <code>sh</code> and it runs it.</p> <p>My problem is, that means starting a new sub shell. I'd rather have the commands run within my current shell. Like I would be able to do with <code>source some-file</code>, if I had the commands in a text file.</p> <p>I don't want to create a temp file because feels dirty.</p> <p>Alternatively, I'd like to start my sub shell with the exact same characteristics as my current shell.</p> <h3>update</h3> <p>Ok, the solutions using backtick certainly work, but I often need to do this while I'm checking and changing the output, so I'd much prefer if there was a way to pipe the result into something in the end.</p> <h3>sad update</h3> <p>Ah, the <code>/dev/stdin</code> thing looked so pretty, but, in a more complex case, it didn't work.</p> <p>So, I have this:</p> <pre><code>find . -type f -iname '*.doc' | ack -v '\.doc$' | perl -pe 's/^((.*)\.doc)$/git mv -f $1 $2.doc/i' | source /dev/stdin </code></pre> <p>Which ensures all <code>.doc</code> files have their extension lowercased.</p> <p>And which incidentally, can be handled with <code>xargs</code>, but that's besides the point.</p> <pre><code>find . -type f -iname '*.doc' | ack -v '\.doc$' | perl -pe 's/^((.*)\.doc)$/$1 $2.doc/i' | xargs -L1 git mv </code></pre> <p>So, when I run the former, it'll exit right away, nothing happens.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload