20

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.

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

6 Answers 6

18

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

5
  • 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 ; doneCommentedJan 9, 2013 at 18:11
  • What goes there can be a space separated list generated in many ways. So yeah, {a..z}, *.pdf, $(find ...)...
    – njsg
    CommentedJan 9, 2013 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
    CommentedJan 10, 2013 at 7:21
  • 1
    The one-liner only worked for me after removing the ; following the do keyword.
    – jimp
    CommentedNov 23, 2016 at 18:00
  • @jimp Thanks, I've changed the code to remove that ;.
    – njsg
    CommentedNov 24, 2016 at 8:37
11

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.

1
  • echo needs -n in order to avoid adding a new line character. causes issues with some commands while passing parameters with xargsCommentedSep 11, 2020 at 7:30
5

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 
    4

    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.

      2

      There is alias command in bash:

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

      The usage is: $ shotcommand a

      3
      • There are also functions, if the code is a bit more complex :-)
        – njsg
        CommentedJan 9, 2013 at 18:32
      • 2
        This solution only works if my changing parameter happens to be the last one, correct?
        – ashays
        CommentedJan 9, 2013 at 19:41
      • Yes. You are right.
        – dchirikov
        CommentedJan 9, 2013 at 19:45
      0

      xargs combined with your original suggestion of curly braces is handy here:

      echo Ahoy{1,2,3}|xargs -n1 echo

      Or with your example:

      echo {a,b} | xargs -d' ' -I% echo run-command --a --whole --lot --of --flags parameter %

      With the added %, you can place your parameter anywhere you want in the command.

      1
      • Welcome to the site, and thank you for your contribution. Please note however that this looks like a minor variation of the answer by @hobadee. If that was not the intention, please edit your post to make the difference in the approach more prominent. Otherwise, it should probably be an edit suggestion to that answer instead.
        – AdminBee
        CommentedJun 20, 2023 at 11:58

      You must log in to answer this question.

      Start asking to get answers

      Find the answer to your question by asking.

      Ask question

      Explore related questions

      See similar questions with these tags.