2019-08-13
How to restructure a date variable in bash
unix.stackexchange
Question

I have a bash script that takes user-inputed dates in this format: dd.mm.yyyy and saves it as a variable $date

Now I need to modify this variable to be in this format: yyyymmdd for further use.

So no more dots and back to front for the values between the dots.

Is there an easy way to do this kind of thing with sed for example?

Answer
1

Here's a pure bash solution:

$ # input is dd.mm.yyyy
$ input_date="dd.mm.yyyy"
$ # output will be yyyymmdd
$ output_date=${input_date:6:4}${input_date:3:2}${input_date:0:2}
$ echo $output_date
yyyymmdd
How to restructure a date variable in bash
See more ...