My shell script:#!/bin/bashecho $1;startd=$(date -d $1 +%Y%m%d); echo $startd;My command:sudo ./test.sh 20151010The output:20151010 20150213it printed todays date instead of printing the input date any idea?
Convert input string to date in shell script
bash;shell script;osx;macintosh;parameter
The OS X version of date uses the -f option to parse a formatted date/time:date -j -f '%Y%m%d' $1 +'%Y%m%d'The -j option causes it to just print the time, not try to set the system clock. So the full script would be:#!/bin/bashecho $1;startd=$(date -j -f '%Y%m%d' $1 +'%Y%m%d'); echo $startd;Here's a transcript:$ ./testdate.sh 201510102015101020151010