tags:

views:

43

answers:

2

I have asked how to make any command "ask for yes/no before executing" in the question

http://stackoverflow.com/questions/3231804/in-bash-how-to-add-are-you-sure-y-n-to-any-command-or-alias

It seems like for the command

hg push ssh://[email protected]//somepath/morepath

I can also do this:

echo ssh://[email protected]//somepath/morepath | xargs -p hg push

The -p is the one that does the trick. This will be set as an alias, such as hgpushrepo. Is this a good way, any pitfalls, or any better alternatives to do it? I was hoping to use something that is standard Unix/Bash instead of writing a script to do it.

+1  A: 

The disadvantage to using an alias is that it won't take parameters. If you wanted to generalize that hg command so you could use it with any username, hostname or path, you'd have to use a script or a function.

By the way, using a script is "standard Unix/Bash". A simple script or function is just as easy (easier, really, because of the increased power and versatility) as an alias. Aliases are useful for very short, extremely simple command shortcuts. Frequently they're used to enable an option as a default (eg. alias ls='ls --color=auto').

For unchanging commands that you use frequently that don't need arguments (except those that can be appended at the end), aliases are perfectly fine. And there's nothing wrong with using xargs in the way that you show. It's a little bit overkill and it's an unnecessary call to an external executable, but that shouldn't be significant in this case.

Dennis Williamson
A: 

xargs has a nasty tendency to lead to uncormfortable surprises because of the separator problem http://en.wikipedia.org/wiki/Xargs#The_separator_problem

GNU Parallel http://www.gnu.org/software/parallel/ does not have that problem and thus may be a safer choice.

Watch the intro video for GNU Parallel: http://www.youtube.com/watch?v=OpaiGYxkSuQ

Ole Tange