Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

escape curly braces in unix shell script

I have a string:

{2013/05/01},{2013/05/02},{2013/05/03}

I want to append a { at the beginning and a } at the end. The output should be:

{{2013/05/01},{2013/05/02},{2013/05/03}}

However, in my shell script when I concatenate the curly braces to the beginning and end of the string, the output is as follows:

{2013/05/01} {2013/05/02} {2013/05/03}

Why does this happen? How can I achieve my result? Am sure there is a simple solution to this but I am a unix newbie, thus would appreciate some help.

Test script:

#!/usr/bin/ksh 
valid_data_range="{2013/05/01},{2013/05/02},{2013/05/03}"
finalDates="{"$valid_data_range"}"
print $finalDates
like image 894
axm2064 Avatar asked Oct 10 '13 09:10

axm2064


People also ask

What are {} used for in bash?

Bash brace expansion is used to generate stings at the command line or in a shell script. The syntax for brace expansion consists of either a sequence specification or a comma separated list of items inside curly braces "{}".

What does curly brackets mean in shell script?

The end of the variable name is usually signified by a space or newline. But what if we don't want a space or newline after printing the variable value? The curly braces tell the shell interpreter where the end of the variable name is.


Video Answer


1 Answers

You can say:

finalDates=$'{'"$valid_data_range"$'}'
like image 109
devnull Avatar answered Oct 02 '22 14:10

devnull