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.

  • That depends on run-command, many Unix commands handle several file arguments in sequence. – vonbrand Jan 13 '16 at 19:36

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

Perhaps a for loop?

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

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

  • 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
  • 1
    The one-liner only worked for me after removing the ; following the do keyword. – jimp Nov 23 '16 at 18:00
  • @jimp Thanks, I've changed the code to remove that ;. – njsg Nov 24 '16 at 8:37

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 -n parameter a,parameter b | xargs -n 1 -d , run-command --a --whole --lot --of --flags

Echo's -n flag avoids printing a trailing new line character (caused issues with some commands while passing parameters through).

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.

  • echo needs -n in order to avoid adding a new line character. causes issues with some commands while passing parameters with xargs – Savvas Radevic Sep 11 '20 at 7:30

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

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

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.


There is alias command in bash:

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

The usage is: $ shotcommand a

  • There are also functions, if the code is a bit more complex :-) – njsg Jan 9 '13 at 18:32
  • 2
    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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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