Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've a not really permissive program to work with. Unfortunately, this program doesn't just allow the following

"command -a -b -c"

or

"command -abc"

So I've to always type the following

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

I am pretty sure there is more efficient way to type that, but can't figure it out.

share|improve this question
3  
This will totally depend on the command. Please include it in your question. – joepd 9 hours ago
1  
You have many different answers, but I bet you will stick to cmd -a; cmd -b; cmd -c in long run, as all human beings do. – jimmij 8 hours ago
up vote 8 down vote accepted

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
share|improve this answer
    
Thank you Stéphane for this option. The combinaison of !!:0 and xargs did the trick. – Douda 9 hours ago

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.

share|improve this answer
1  
Exactly what I needed ! Thank you vey much heemayl – Douda 9 hours ago
2  
!!:0 (in shells that support csh history expansion) will expand to the first word of the previous command-line, not previous command. – Stéphane Chazelas 9 hours ago
    
@StéphaneChazelas clarified.. – heemayl 9 hours ago
    
We don't know what the first word of the previous command-line was. Try to enter uname and then echo foobar && !!:0 spamegg && !!:0 baz, that will expand to echo foobar && uname spamegg && uname baz. – Stéphane Chazelas 9 hours ago
2  
One ! is enough: !:0 but with @StéphaneChazelas points better is use !#:0 where the !# mean The entire command line typed so far. – Costas 8 hours ago

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
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.