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 know that I can run the following command

ls Some{File,Folder}

And it is equivalent to running this:

ls SomeFile SomeFolder

(I also use it a lot for things like mv place_{a,b})

However, I was wondering if there was a different shortcut where I could do something like this:

run-command --a --whole --lot --of --flags parameter a; \
     run-command --a --whole --lot --of --flags parameter b

And I only had to type run-command --a --whole --lot --of --flags parameter {a,b} or something similar.

share|improve this question
    
That depends on run-command, many Unix commands handle several file arguments in sequence. – vonbrand Jan 13 at 19:36
up vote 4 down vote accepted

Assuming that you are using the GNU bash or something similar:

Perhaps a for loop?

for x in a b
do
    run-command --a --whole --lot --of --flags parameter $x
done

which can also be written in one line as for x in a b; do ; run-command --a --whole --lot --of --flags parameter $x ; done

share|improve this answer
1  
or if you have a lot you want to run you can use bash expansion like so for x in {a..z}; do ; run-command --a --whole --lot --of --flags parameter $x ; done – h3rrmiller Jan 9 '13 at 18:11
    
What goes there can be a space separated list generated in many ways. So yeah, {a..z}, *.pdf, $(find ...)... – njsg Jan 9 '13 at 18:34
    
I don't know why my mind didn't jump to loops. Thanks! This will probably be what I use. The function idea is good, but I like this better. – ashays Jan 10 '13 at 7:21

You can also use a function, which doesn't limit you to having your changing argument at the end:

runcom() { run-command --a --whole --lot --of --flags parameter "$1" ; }

runcom a
runcom b
share|improve this answer

There is alias command in bash:

$ alias shotcommand='run-command --a --whole --lot --of --flags parameter'

The usage is: $ shotcommand a

share|improve this answer
    
There are also functions, if the code is a bit more complex :-) – njsg Jan 9 '13 at 18:32
    
This solution only works if my changing parameter happens to be the last one, correct? – ashays Jan 9 '13 at 19:41
    
Yes. You are right. – dchirikov Jan 9 '13 at 19:45

If you aren't scripting this and just want to run it easily on a command line, the xargs command is probably what you are looking for.

xargs will take a bunch of content from the standard input, and use it as arguments for a command, running that command multiple times if needed.

Try the following:

echo parameter a,parameter b | xargs -n 1 -d , run-command --a --whole --lot --of --flags

In this example, you are simply giving xargs the permutations you want it to run.

The "-n 1" flag to tell xargs to use just 1 of the arguments per run.

The "-d ," flag tells xargs to look for a "," on the input as the deliminator between arguments to run. (Note that if the argument has a "," in it you will need to change this to something else. If your arguments have no spaces in them you can omit this flag altogether.)

See man xargs for more info.

share|improve this answer

You can perform substitutions in the previous command. This won't work with your {a, b} example, because all instances of 'a' will be replaced with a 'b'. But imagine you want to execute the following commands:

run-command --a --whole --lot --of --parameter --format xml
run-command --a --whole --lot --of --parameter --format son

You can do it with

run-command --a --whole --lot --of --parameter --format xml
!!:s/xml/json/

Bash will do the substitution and run

run-command --a --whole --lot --of --parameter --format json

Use gs (global substitution) to perform several substitution, not only one.

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.