[sf-lug] curly braces {} in bash/shell: Re: Meeting notes 2.0

Ronald Barnes ron at ronaldbarnes.ca
Wed Feb 8 15:01:14 PST 2023


Michael Paoli wrote on 08/02/2023 05.21:

> On 2023-02-07 08:15, aaronco36 wrote:
>> Got onto the virtual meeting late; maybe a bit before 12Noon(?)
>> IIRC, Victor was asking questions about ternary operators and the use
>> of curly brackets {} in bash scripts.
> 
> So, curly braces {} in POSIX (and bash) shell.
> Most notably they're used for two different things,
> though in both cases, one can think of them as a form of grouping.


That was a terrific write-up, thanks Michael!


The specific list grouping Victor was asking about was the form of:

touch {file1,file2}

and asking if there was another way to do that.

I replied that I would (and do) use exactly that format.


But your email fleshed out all the other use cases quite nicely.


Maybe I'll mention arrays, where the use of braces are similar to this 
example you gave:

 > echo ${foo}bar $foobar


$ declare -a my_array
$ my_array+=(Hello World)
$ echo "The 0th element of my_array is ${my_array[0]}"
Hello
$ echo "The length (number of items) in my_array is ${#my_array[@]}"
2



Cheers,

rb




More information about the sf-lug mailing list