Reputation: 417
I am trying to create a quick script that will get a file from a backup folder. The backup file is generated every day and have the filename format of:
backup-sql-YYYYMMDD.zip
I am trying to get the filename by using:
#!/bin/sh
VAR_1="backup-sql-"
VAR_2=date +%Y%m%d
VAR_3=$VAR_1$VAR_2
echo "$VAR_3"
However the output is:
backup-sql-
When I run:
date +%Y%m%d
I get
20180704
So I am not sure why this is happening. Please can someone help me :)
Upvotes: 0
Views: 7677
Reputation: 26925
You could use:
$(command)
or
`command`
From your example give a try to:
VAR_2=$(date +%Y%m%d)
Check Command Substitution for more details:
When the old-style backquote form of substitution is used, backslash retains its literal meaning except when followed by ‘$’, ‘`’, or ‘\’. The first backquote not preceded by a backslash terminates the command substitution. When using the $(command) form, all characters between the parentheses make up the command; none are treated specially.
Upvotes: 1
Reputation: 472
You should be using backticks to capture the output of the date command and assign it to a shell variable.
VAR2=`date "+%Y%m%d"`
In case you want to make sure the variable value is passed on to subsequent child processes you may want to export it as:
export VAR2=`date "+%Y%m%d"`
Upvotes: 1