How can I cut the date and leave it in 3 variable?
date="20181219"
year=substr(date,1,4)
monthsubstr(date,4,2)
daysubstr(date,6,2)
result
year=2018
month=12
day=19
this does not work for me
How can I cut the date and leave it in 3 variable?
date="20181219"
year=substr(date,1,4)
monthsubstr(date,4,2)
daysubstr(date,6,2)
result
year=2018
month=12
day=19
this does not work for me
date=20181219
year=${date:0:4}
month=${date:4:2}
day=${date:6:2}
echo $year $month $day
Notice that the first character of the string is position zero.
Or, in case you run a recent shell, try
$ { read -n4 year; read -n2 month; read -n2 day; } <<< $date
$ echo $day $month $year
19 12 2018
Longhand using OSX 10.14.1, default bash terminal calling dash, POSIX compliant.
This assumes that the format is consistent as per the OP.
Last login: Wed Dec 19 16:12:52 on ttys000
AMIGA:amiga~> dash
AMIGA:\u\w> # POSIX compliant using dash, OSX 10.14.1, default bash terminal.
AMIGA:\u\w> STRING="20181219"
AMIGA:\u\w> year="${STRING%????}"
AMIGA:\u\w> date="${STRING#??????}"
AMIGA:\u\w> STRING="${STRING#????}"
AMIGA:\u\w> month="${STRING%??}"
AMIGA:\u\w> echo $year $month $date
2018 12 19
AMIGA:\u\w> exit
AMIGA:amiga~> _