Home > Net >  How convert Date variable format in bash
How convert Date variable format in bash

Time:12-02

I have bash variable named date_export that have 2021-09-22 as a value. I want assign it to other bash variable named DATE_EXPORT but having other format 20210922 I tried :

DATE_EXPORT=$(date -d ${date_export}   '%Y%m%d')

But, it does not work, any help, please

CodePudding user response:

The output format is a single word starting with , not two separate arguments and the format.

DATE_EXPORT=$(date -d "$date_export"  "%Y%m%d")

CodePudding user response:

date_export=2021-09-22
DATE_EXPORT=${date_export//-/}
echo $DATE_EXPORT # prints 20210922

CodePudding user response:

Clearing the dashes using POSIX-shell grammar:

DATE_EXPORT=$(IFS=-; printf %s $date_export)

Or without spawning a sub-shell:

DATE_EXPORT=${date_export%-*}
DATE_EXPORT=${DATE_EXPORT%-*}${DATE_EXPORT#*-}${date_export##*-}
  • Related