3

I would like to know if there is a zsh pattern (or bash command) for running the same command in multiple versions, eg.:

somelongcommandwithargs {a, b} furtherargs # Some magic syntax represented with {a, b}

That would result in zsh running the following commands consequitively:

somelongcommandwithargs a furtherargs
somelongcommandwithargs b furtherarg
1

You can use this pattern:

eval 'somelongcommandwithargs '{a, b}' furtherargs; '

This works with anything, as long as you make sure your eval string ends in ; (or whatever else you need to terminate your command). For example:

$ touch test
$ echo aaa >> test && echo bbb >> test && eval 'sed -i "" '{s/aaa/ccc/g,s/bbb/ddd/g}' test; '
$ cat test
ccc
ddd
4
  • I tried, this does not work: echo aaa >> test && echo bb >> test && eval 'sed -i "" '{s/aaa/ccc/g,s/bbb/ddd/g}' test' Jun 4 '20 at 7:50
  • The reason why your example works is because {a,b} is the pattern to substitute similar strings into the same command, and touch excepts multiple arguments. Your code gives the same result as touch a-xx b-xx; instead of what you cited. Jun 4 '20 at 7:54
  • You're doing it wrong. You need to add a semicolon to the end of your eval string, like this: echo aaa >> test && echo bbb >> test && eval 'sed -i "" '{s/aaa/ccc/g,s/bbb/ddd/g}' test; ' Jun 4 '20 at 9:11
  • I updated my answer with this new example. Jun 4 '20 at 9:15

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.