Print day of week with exact date in bash

I am trying to print the day of the week of a specified date. This command works very well:

TARGET=$(date -u -d'2015-10-25' '+%u') 

But there is an error inside my bash script, 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

An error occurred:

date: the argument ‘'+%u'’ lacks a leading '+';
+4
source share
2 answers

You don’t need to use so many temporary variables and definitely avoiding the single quote inside another separate quote will not work in the shell .

Simplify this as follows:

#!/bin/bash
day=25
month=10
year=2015
command1='date -u -d'
TARGET=$(date -u -d "$year-$month-$day" '+%u')
echo $TARGET
+2
source

It 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 have no answer to the question why, though

+1
source

Source: https://habr.com/ru/post/1613115/


All Articles