micgeronimo
micgeronimo

Reputation: 2149

Execute line given in output of another command

Warning: this question is not about usage of git, but about usage of pipes in Linux, git 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

Upvotes: 1

Views: 72

Answers (2)

bgoldst
bgoldst

Reputation: 35314

Rather than piping to another command, you can instead wrap the pipeline in a command substitution and eval it. This will execute the command in your current shell session. For example:

eval "$(printf 'some output 1\nsome output 2\necho execute me'| tail -1)";
## execute me

Upvotes: 1

fedorqui
fedorqui

Reputation: 289625

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
hello
$ echo "uptime" | bash
 16:22:37 up  7:31,  3 users,  load average: 0,03, 0,14, 0,23

Upvotes: 2

Related Questions