Assuming bash
:
You can make use of word splitting. Internal Field Separator (IFS) defaults to space, tab or newline. But you can override it with setting the IFS
environment variable, then you can use the normal ways to create a variable:
set -f # disable filename expansion
IFS='|' arr=($variable_1)
or declare
:
IFS='|' declare -a 'arr=($variable_1)'
or read
:
IFS='|' read -ra arr <<< "$variable_1"
If you change the IFS
variable like this, you may want to save it first in another variable and reset it afterwards:
OLD_IFS="$IFS"
# my commands
IFS="$OLD_IFS"