Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xargs (or something else) without space before parameter

I would like to execute something like this (git squash):

git rebase -i HEAD~3

extracting the 3 from git log:

git log | blabla | xargs git rebase -i HEAD~

This does not work because xargs inserts a space after HEAD~.

The problem is that I want to alias this command, so I cannot just use

git rebase -i HEAD~`git log | blabla`

because the number would be evaluated just when I define the alias.

I don't have to use xargs, I just need an alias (preferably not a function).

like image 875
Gismo Ranas Avatar asked Jun 03 '15 09:06

Gismo Ranas


People also ask

What can I use instead of xargs?

If you can't use xargs because of whitespace issues, use -exec . Loops are just as inefficient as the -exec parameter since they execute once for each and every file, but have the whitespace issues that xargs have.

Can we use xargs and standard input?

xargs is a Unix command which can be used to build and execute commands from standard input.

Why is xargs necessary?

It converts input from standard input into arguments to a command. Some commands such as grep and awk can take input either as command-line arguments or from the standard input. However, others such as cp and echo can only take input as arguments, which is why xargs is necessary.

What is the difference between pipe and xargs?

pipes connect output of one command to input of another. xargs is used to build commands. so if a command needs argument passed instead of input on stdin you use xargs... the default command is echo (vbe's example). it breaks spaces and newlines and i avoid it for this reason when working with files.


2 Answers

You can use the -I option of xargs:

git log | blabla | xargs -I% git rebase -i HEAD~%
like image 154
choroba Avatar answered Sep 27 '22 21:09

choroba


Try this:

git log | blabla | xargs -i bash -c 'git rebase -i HEAD~{}'
like image 39
Juan Diego Godoy Robles Avatar answered Sep 27 '22 20:09

Juan Diego Godoy Robles