Reputation:
I am trying to print the day of the week of given date. This command works pretty well:
TARGET=$(date -u -d'2015-10-25' '+%u')
But inside my bash script there is an error, what should be wrong?
#!/bin/bash
day=25
month=10
year=2015
command1='date -u -d'
command3=''\'
command2=$year-$month-$day
fullcommand=$command1$command3$command2$command3' '$command3'+%u'$command3
echo $fullcommand
TARGET=$($fullcommand)
echo $TARGET
There is an error:
date: the argument ‘'+%u'’ lacks a leading '+';
Upvotes: 4
Views: 112
Reputation: 785098
No need to use so many temporary variables and definitely escaping single quote inside another single quote won't work in shell.
Simplify it like this:
#!/bin/bash
day=25
month=10
year=2015
command1='date -u -d'
TARGET=$(date -u -d "$year-$month-$day" '+%u')
echo $TARGET
Upvotes: 2
Reputation: 1601
This works:
#!/bin/bash
day=25
month=10
year=2015
command1='date -u -d'
command3=''\'
command2=$year-$month-$day
fullcommand="$command1 $command2 +%u"
echo $fullcommand
TARGET=$($fullcommand)
echo $TARGET
I do not have the answer to why, though
Upvotes: 1