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

1 Answer 1


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 '

  • 2
    You need curly braces in ${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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.