help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] hi, doubt about xargs


From: Greg Wooledge
Subject: Re: [Help-bash] hi, doubt about xargs
Date: Mon, 3 Jun 2013 09:14:01 -0400
User-agent: Mutt/1.4.2.3i

On Sun, Jun 02, 2013 at 03:21:35PM +0200, JoaquĆ­n Villanova wrote:
> By the way, I've read your wiki and it is just Amazing, I didn't realize
> that it was you / your wiki when you sent me the reply.

Thanks.  There have been several contributors; it's not all my work.

> Just for taking this down definitely. I understand all the command  `find .
> -name '*.txt' -execdir sh -c 'for f; do cp "$f" "${f%.txt}.bak"; done' _ {}
> +'   but for what is `_' used? thanks in advance!

When you run a command like this:

sh -c 'some stuff' a b c ...

"a" becomes $0, "b" becomes $1, "c" becomes $2, and so on.

The "_" in the example will become the script's $0, which is ignored.
The other arguments after that (provided by find) will become the
positional parameters $1, $2 and so on.

"_" can be replaced by nearly anything.  However, don't use "-" because
some versions of sh interpret that like "--" and strip it out.  Thus:

imadev:~$ find .bashrc -exec sh -c 'printf "<%s> " "$@"; echo' - a b {} \;
<b> <.bashrc> 

In this case, "-" was stripped out completely, "a" became $0, "b" became
$1, and the filename passed by find became $2.  This behavior is not
consistent across shells, so using - is just a Bad Idea.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]