micgeronimo micgeronimo - 1 year ago 77
Linux Question

Execute line given in output of another command

Warning: this question is not about usage of git, but about usage of pipes in Linux,

command is given here as example.

Having such output of
git push

fatal: The current branch my_branch has no upstream branch.
To push the current branch and set the remote as upstream, use

git push --set-upstream origin my_branch

I want to execute given command, i.e.
git push --set-upstream origin my_branch

By doing
git push 2>&1 | tail -3
I get
git push --set-upstream origin my_branch
printed on screen.

Question: What command should be added to next pipe so given
git push
will be executed. So I could do
git push 2>&1 | tail -3 | command_to_eval_given_string

Answer Source

Just pipe to bash itself:

git push ... | bash

This will send the stdout from the previous pipes to bash, that will then execute it.

$ echo "echo 'hello'" | bash
$ echo "uptime" | bash
 16:22:37 up  7:31,  3 users,  load average: 0,03, 0,14, 0,23
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download