sh - split string by delimiter

clarkk picture clarkk · Nov 12, 2013 · Viewed 13.2k times · Source

code

s='id;some text here with possible ; inside'
IFS=';' read -r id string <<< "$s"
echo "$id"

error

restore.sh: 2: restore.sh: Syntax error: redirection unexpected

bash version GNU bash, version 4.2.37(1)-release (x86_64-pc-linux-gnu)

Answer

chepner picture chepner · Nov 12, 2013

A here string is just a shortcut for a small here document. This should work in any POSIX shell:

s='id;some text here with possible ; inside'
IFS=';' read -r id string <<EOF
$s
EOF
echo "$id"