Mr. Demetrius Michael
Mr. Demetrius Michael

Reputation: 2406

Shell/Bash - pipe output into another script's input via a variable

Normally I would break things into separate actions and copy and paste the output into another input:

$ which git
/usr/local/bin/git
$ sudo mv git-credential-osxkeychain /usr/local/bin/git

Any quick hack to get output into input? something like: $echo which wget | sudo mv git-credential-osxkeychain

Upvotes: 1

Views: 930

Answers (4)

Stephen Garle
Stephen Garle

Reputation: 299

If you want to use the pipe syntax, then you should look at xargs.

Upvotes: 0

shellter
shellter

Reputation: 37268

set -vx
myGit=$(which git)
gitDir=${myGit#/git} ; gitDir=${gitDir#/bin}/git
echo sudo mv git-credential-osxkeychain ${gitDir}

Remove the set -vx and the echo on the last line when you're sure this performs the action that you require.

It's probably possible to reduce the number of keystrokes required, but I think this version is easier to understand what techniques are being used, and how they work.

IHTH

Upvotes: 1

doubleDown
doubleDown

Reputation: 8398

The answer would be what Chirlo and shellter said.

Why $echo which wget | sudo mv git-credential-osxkeychain wouldn't work is because piping redirect the stdout from a previous command to the stdin of the next command. In this case, move doesn't take input from stdin.

A curious thing is that which git returns

/usr/local/bin/git

but you are moving git-credential-osxkeychain to

/usr/local/git/bin/

Those two don't match. Is there a typo or something?

Upvotes: 0

Chirlo
Chirlo

Reputation: 6132

use command substitution with $(command)

sudo mv git-credential-osxkeychain $(which git)

This substitutes the command for its output. You can find all about it in http://tldp.org/LDP/abs/html/commandsub.html

Upvotes: 0

Related Questions