0

Is there a shell (bash,zsh,??) that supports something like this?

git branch -D <feature1,feature2,feature3> 

that would effectively make a transformation to:

for BRANCH in feature1 feature2 feature3; do git branch -D $BRANCH; done 
7
  • I would doubt it. That's wildly unsafe. That comma delimited string could be a perfectly valid argument to that command. The shell can't know. Commented Jun 23, 2015 at 20:25
  • That reasoning is a bit weak. You can do mkdir f{1,2,3} and it will get expanded to mkdir f1 f2 f3. If you cared about commas you can just do mkdir 'f{1,2,3}' to escape expansion. I don't see why a shell couldn't expand something like ^1,2,3^ or w/e to a for loop. Commented Jun 23, 2015 at 20:41
  • printf '%s\0' feature1 feature2 feature3 | xargs -0 -n1 <command>? Commented Jun 23, 2015 at 20:42
  • I know about xargs, this is specifically about sugar to make this common case easier. I guess you could write a helper bash function.. Commented Jun 23, 2015 at 20:43
  • @WilliamCasarin That's not what you are asking for though. You could do exactly that with your git branch command too but it doesn't do what you want here. The difference is how it interacts with the rest of the command. Though I'll grant it is only a mostly compelling reason. Commented Jun 23, 2015 at 20:43

2 Answers 2

3

No, because this is not nearly as useful as it sounds. Most commands accept multiple arguments already, including git branch -D:

$ git branch -D foo bar baz Deleted branch foo (was 9e9d099). Deleted branch bar (was 9e9d099). Deleted branch baz (was 9e9d099). 

Commands that don't accept this typically have a good reason not to. For example, convert "$file" output.jpg has an explicit output path, and naively looping over filenames would just overwrite the output.

For these things, zsh has short form for loops if it helps:

for f (foo bar baz) convert "$f.png" "$f.jpg" 
Sign up to request clarification or add additional context in comments.

2 Comments

:( It's useful for cases that don't support multiple arguments like that
It's a shame there is no expansion syntax, but that short form is pretty nice. Thanks!
2
## # Run the given command (first args up to `--`) # on each of the following args, one at a time. # The name `map` is tongue-in-cheek. # # Usage: map [cmd] -- [args] map() { local -a cmd local arg while [[ $1 ]]; do case "$1" in --) break;; esac cmd+=( "$1" ) shift done shift for arg; do "${cmd[@]}" "$arg" done } 

3 Comments

Haha I had something similar half written but this looks a bit better. Thanks!
You're kind of reinventing xargs: printf "%s\n" "$@" | xargs -L 1 "${cmd[@]}"
@glennjackman That command looks familiar.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.