Run same command with different parameters

You could do:

echo -a -b -c | xargs -n 1 command

Or:

xargs -n1 command <<< '-a -b -c'

with some shells.

But beware that it affects the stdin of command.

With zsh:

autoload zargs # best in ~/.zshrc
zargs -n 1 -- -a -b -c -- command

Or simply:

for o (-a -b -c) command $o

None of those would abort if any of the command invocations failed (except if it fails with the 255 exit status).

For that, you'd want:

for o (-a -b -c) command $o || break

That still fails to the $? to the exit status of the failed command invocation). You could change that to:

(){for o (-a -b -c) command $o || return}

But by that time, it's already longer than:

command -a && command -b && command -c

You can refer to the first word of the previous command line (command in your case) by history expansion !!:0 and then you can just add necessary arguments to it.

command -a && !!:0 -b && !!:0 -c

For example:

% echo foobar && !!:0 spamegg && !!:0 baz    
echo foobar && echo spamegg && echo baz
foobar
spamegg
baz

Note that, as Stéphane Chazelas has pointed out, this can result in unexpected expansions too.


How about a shell function wrapping a loop?

yourcmd() {
    local arg
    for arg; do
        thing "$arg" || return
    done
}

Where "thing" is the actual command you want to invoke. Then simply

yourcmd -a -b -c

You can even generalize this to any command:

splitargs() {
    local cmd arg 
    cmd="$1"
    shift
    for arg; do
        "$cmd" "$arg" || return
    done
}

Then

splitargs somecommand -a -b -c

Tags:

Command Line