Operating System - Linux
1753498 Members
4616 Online
108794 Solutions
New Discussion юеВ

Shell script: Brace expansion

 
SOLVED
Go to solution
SGK
Occasional Advisor

Shell script: Brace expansion

Hi,

The following two brace expansion working differently. Can someone explain how this works?

# echo {file1,file2}\ :{\ A," B",' C'}
file1 : A file1 : B file1 : C file2 : A file2 : B file2 : C

# echo {file1,file2} :{\ A," B",' C'}
file1 file2 : A : B : C

TIA.
8 REPLIES 8
Ivan Ferreira
Honored Contributor

Re: Shell script: Brace expansion

I just tested both expressions and I have the same result. That is, both returns the same output:

file1 : A file1 : B file1 : C file2 : A file2 : B file2 : C
Por que hacerlo dificil si es posible hacerlo facil? - Why do it the hard way, when you can do it the easy way?
SGK
Occasional Advisor

Re: Shell script: Brace expansion


Under which shell you tried?

# echo $SHELL
/bin/bash

# echo {file1,file2} :{\ A," B",' C'}
file1 file2 : A : B : C
Jeroen Peereboom
Honored Contributor

Re: Shell script: Brace expansion

>echo $SHELL
/bin/bash
> echo {file1,file2}\ :{\ A," B",' C'}
file1 : A file1 : B file1 : C file2 : A file2 : B file2 : C
>echo {file1,file2}\ :{\ A," B",' C'}
file1 : A file1 : B file1 : C file2 : A file2 : B file2 : C

No differences?

JP
Alexander Chuzhoy
Honored Contributor

Re: Shell script: Brace expansion

I get the same behavior as you did.
Use BASH shell on rhel4.2
Jeroen Peereboom
Honored Contributor
Solution

Re: Shell script: Brace expansion

Oops...

>echo {file1,file2}\ :{\ A," B",' C'}
file1 : A file1 : B file1 : C file2 : A file2 : B file2 : C
>echo {file1,file2} :{\ A," B",' C'}
file1 file2 : A : B : C

Difference in commands: the back-slash to escape the space before the colon.
Man page:
A correctly-formed brace expansion must contain unquoted opening and closing braces, and at least one unquoted comma. Any incorrectly formed brace expansion is left unchanged.
I don't think that helps.

If you escape the space with a backslash, the space is part of the string. If you do not escape the space, it is a separator and there is nothing to expand in the two separate brace lists...

JP
Jeroen Peereboom
Honored Contributor

Re: Shell script: Brace expansion

And also:

echo {file1,file2}" : "{\ A," B",' C'}
file1 : A file1 : B file1 : C file2 : A file2 : B file2 : C

JP
Ryan Goh
Frequent Advisor

Re: Shell script: Brace expansion

Try this

# echo {file1,file2}:{\ A," B",' C'}

You should get the result below:

file1: A file1: B file1: C file2: A file2: B file2: C

Because of additional space in between of } and : , that why get the problem look like below:


file1 file2 : A : B : C






SGK
Occasional Advisor

Re: Shell script: Brace expansion

Thanks all to clarify my doubts