Você deve fazer isso usando apenas bash
, usando a expansão do parâmetro bash
:
$ time=1315
$ hr="${time%??}" ## getting first two characters
$ min="${time#??}" ## getting last two characters
$ echo "Your time is "$hr" hours and "$min" minutes"
Your time is 13 hours and 15 minutes
Ou corte de string (obrigado @Serg por mencionar), observe que o índice começa em 0:
O formato é:
${parameter:offset:length}
$ time=1315
$ hr="${time:0:2}" ## getting chars at index 0 and index 1
$ min="${time:2:2}" ## getting chars at index 2 and 3
$ echo "Your time is "$hr" hours and "$min" minutes"
Your time is 13 hours and 15 minutes
Se você insistir em grep
:
$ time=1315
$ hr="$(grep -o '^..' <<<"$time")" ## getting first two characters
$ min="$(grep -o '..$' <<<"$time")" ## getting last two characters
$ echo "Your time is "$hr" hours and "$min" minutes"
Your time is 13 hours and 15 minutes