0

Bash newbie here. I have a string in the variable IN and I want to split it into an array when the delimiter ; is found. Reading different stack exchange answers (this one specifically), I tried this:

#!/bin/bash
IN="12;25;365"
IFS=";" read -r -a ARR <<< "$IN"
echo $ARR

The output is 12. Interestingly, if I try the following commands in zsh:

IN="12;25;365"
IFS=";" read -r -A ARR <<< "$IN"
echo $ARR

The output will be 12 25 365.

What am I doing wrong in the bash script? I need that working in bash...

Davide_sd
  • 10,578
  • 3
  • 18
  • 30

1 Answers1

3

Bash requires using ${ARR[@]} or ${ARR[*]} to display all elements of array.

For further reading: https://linuxconfig.org/how-to-use-arrays-in-bash-script

Modified script:

#!/bin/bash
IN="12;25;365"
IFS=";" read -r -a ARR <<< "$IN"
echo ${ARR[@]}
Fazlin
  • 2,285
  • 17
  • 29