Combining brace expansion and output redirection

Often, I use parameter expansion on the command-line, such as

cp a.{ext1,ext2}

which is expands to

cp a.ext1 a.ext2

However, in many cases I do not use cp, but another tool, let’s call it cat, and I would do some output redirection, like

cat a.ext1 > a.ext2

In my case a can vary, or be quite long, so I would like to be able to use some sort of brace expansion, that at least needs only one occurrence of a in the one-liner. How can I obtain that?

I am specifically using

$ bash --version
GNU bash, version 3.2.51(1)

but answers using other shells are also appreciated.

Asked By: Bernhard

||

What about:-

f=really-long-filename; cat "$f.ext1" > "$f.ext2"
Answered By: garethTheRed

You can type:

$ cat very-long-filename.ext1Ctrl+WCtrl+Y>Ctrl+YBackspace2

Or:

$ cat very-long-filename.Ctrl+WCtrl+Yext1>Ctrl+Yext2

To really use brace expansion, note that:

cat a.ext1 > a.ext2

Can also be written:

cat > a.ext2 a.ext1

However you cannot do:

cat > a.ext{2,1}

However, you could do:

eval cat > a.ext{2,1}

In zsh, you could also use an anonymous function:

(){cat $1 > $2} a.ext{1,2}

Or:

(){cat ${1}1 > ${1}2} a.ext
Answered By: Stéphane Chazelas

Use a shell function

 xyx () { cat "$1".$2 > "$1".$3; }

then

xyz fie foo fum

expands to

cat fee.foo > fee.fum
Answered By: Lenne