0

I need to change a date String from format YYYYMMDD to date YYYY-MM-DD without using date -d command. The only option I found is string manipulations.

1
  • How many tools can you name that perform text manipulation? If none, please go and do some basic research. Otherwise, tell us what you’ve tried. … … … … … … … … … … … … … … … … …  P.S. Technically, you have not asked a question. Apr 2, 2018 at 15:58

1 Answer 1

3

What about using bash parameter expansion?

$ var=20180402
$ echo "${var::4}-${var:4:2}-${var:6}"
2018-04-02
3
  • Spot of code golf: the last term could just be '${var:6}'
    – bu5hman
    Apr 2, 2018 at 17:10
  • Further golf : the first term could be just ${var::4}
    – steve
    Apr 2, 2018 at 18:33
  • Thanks didn't think about either. I did consider using ${var: -2} for the last one but didn't want to lead to confusion.
    – jesse_b
    Apr 2, 2018 at 19:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .