Issue
I have declared the following strings in my shell script
#!/bin/bash
DATE= date +"%d.%m.%Y"
FILE_PREFIX="V_Articles_"
FILE_EXTENSION=".json"
I would like to combine these strings to generate a filename
echo "$FILE_PREFIX$DATE_$FILE_EXTENSION"
But no matter what I do, the date string would be printed on the first line and then the rest of the string would be printed on the second line.
Output:
01.10.2023
V_Articles_.json
If i try concatenating the strings like: echo $FILE_PREFIX+$DATE+$FILE_EXTENSION"
I get the following output:
01.10.2023
V_Articles_++.json
How can I get the desired output where the date would be included?
Solution
You need to use command substitution $() for the date. It allows you to execute a command and substitute its output in place of the $() expression
Try this:
#!/bin/bash
DATE=$(date +"%d.%m.%Y")
FILE_PREFIX="V_Articles_"
FILE_EXTENSION=".json"
echo "$FILE_PREFIX$DATE$FILE_EXTENSION"
Answered By - EeEmDee Answer Checked By - Robin (WPSolving Admin)