Suppose there are 2 variables as below
$sFileName="test"
sSysDate=date +%Y%m%d%H%M%S
How to concatenate the 2 variables to form test_20191030123803
Thanks Gautam
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communitySuppose there are 2 variables as below
$sFileName="test"
sSysDate=date +%Y%m%d%H%M%S
How to concatenate the 2 variables to form test_20191030123803
Thanks Gautam
There are a few ways.
sFileDate="${sFileName}_${sSysDate}"
Note the {}s are optional
sFileDate="$sFileName_$( date +%Y%m%d%H%M%S )
You can even do this in the date command, if you are careful. I have never tried to include a variable in the format. You could do it this way.
sFileDate=$( date +"test_"+%Y%m%d%H%M%S )
I do not know if this will work
sFileDate=$( date "+${sFileName}_%Y%m%d%H%M%S )
Some of these are OS and shell dependent.
If $( date ) does not work, you may have to enclose your command in back ticks instead: 'date '
${sFileName}_
, to keep it from thinking the "_" is part of the variable name. I'd also recommend not using ${sFileName}
as part of the format string for the date
command, just in case it contains anything weird that the date
command would try to parse and act on.
Oct 30, 2019 at 6:28