Reputation: 13
I am trying to convert a variable to a string format so I can use java runsql utility to insert its value into a database later. The database needs the value to be in a char format, hence string.
This is a dumbed down version of my code so I can get to the heart of what I'm asking -
#!/bin/ksh -x
value1=2018-01-01
value2=2018-02-01
mystring=$value1,$value2
echo $mystring
stringify=${'value1'},${'value2'})
echo $stringify
What happens is I get no output for stringify or depending on how I switch up the arrangement of the symbols I get the literal string 'value1'
or 'value2'
.
What am I doing wrong? I feel like this is very simple, maybe I've just been staring at it too long, I dunno.
Upvotes: 1
Views: 19633
Reputation: 1581
You can just do like this, more simpler:
#!/bin/ksh -x
value1=2018-01-01
value2=2018-02-01
mystring=$value1,$value2
echo $mystring
stringify="'$value1','$value2'" #use double-quotes around the variables
echo $stringify
Output:
2018-01-01,2018-02-01
'2018-01-01','2018-02-01'
Upvotes: 2
Reputation: 4956
You can just escape quote like this:
mystring=\'$value1\',\'$value2\'
Output:
$ echo $mystring
'2018-01-01','2018-02-01'
A simpler option to get the same output (as suggested by @CharlesDuffy) is:
mystring="'$value1','$value2'"
Upvotes: 2