1

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

1
  • You want tail -1, not tail -3 ... it's just the last one line. Commented Aug 18, 2016 at 14:40

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.