Jason (jcreed) wrote,

Bash question: I want to copy a bunch of files with the same basename and different extensions.

I know I can do
bash$ for x in tex pdf ps; do cp icfp2010.$x icfp46-reed.$x; done
but I thought I remembered hearing about something syntactically snappier, along the lines of
bash$ cp icfp2010.{tex,pdf,ps} icfp46-reed.{tex,pdf,ps}
being possible. Am I crazy? Did I just dream this? Clearly the thing I wrote down does not actually do what I want; braces expand 'horizontally' to a list of things, and this would have the same effect as the fairly meaningless
bash$ cp icfp2010.tex icfp2010.pdf icfp2010.ps icfp46-reed.tex icfp46-reed.pdf icfp46-reed.ps

I think what I want is actually two features: one, a kind of braces that expands vertically, so that
bash$ echo [a,b,c]
bash$ echo a
bash$ echo b
bash$ echo c
and secondly a kind of backreference to a previously listed vertical expansion, so that maybe (just recklessly making up some more syntax) I could say
bash$ cp icfp2010.[tex,pdf,ps] icfp46-reed.[\1]
to mean

bash$ icfp.2010.tex icfp46-reed.tex
bash$ icfp.2010.pdf icfp46-reed.pdf
bash$ icfp.2010.ps icfp46-reed.ps

Presumably if you had more than one set of [braces] in the same expression, it would do like a cartesian product over them.

Does something like this feature already exist? It really wouldn't surprise me, since I don't really know how to search for, like, abstract concepts in the bash manual, yet a quick skim didn't turn up anything.

Putting my math-nerd hat on, it's like the shell has two list monads going on, one over the list of tokens in a command, and one over the list of commands, and I want to be able to create effects in both of them...
Tags: shell

  • Post a new comment


    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded