0

Wrong tokenize of string

For extraction of app version from command output

#!/bin/bash
# GNU bash, version 4.3.46

string='Version: 19.08.19-14:25'
IFS=" -:" set -- $string
echo $2

I expect that $2 will have value 19.08.19. But result is 19.08.19 14 25.

Cyrus
  • 84,225
  • 14
  • 89
  • 153
Roman Kazmin
  • 931
  • 6
  • 18

1 Answers1

1

The word splitting happens before the value of IFS has changed (see SIMPLE COMMAND EXPANSION in man bash). You need to set IFS in a separate statement.

IFS=" -:"
set -- $string

Don't forget to set it back if the script does anything else.

choroba
  • 231,213
  • 25
  • 204
  • 289