0

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

1
  • 1
    date -d "$date_export" '+%Y%m%d' should work Commented Dec 1, 2021 at 15:30

3 Answers 3

2

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") 
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

0

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##*-} 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.