Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sh - split string by delimiter

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)

like image 681
clarkk Avatar asked Jun 09 '26 19:06

clarkk


2 Answers

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

string='id;some text here with possible ; inside'
IFS=';' read -r id string <<EOF
$string
EOF
echo "$id"
like image 151
chepner Avatar answered Jun 12 '26 12:06

chepner


You seem to be using sh to execute the script. Herestrings aren't supported in sh; hence the error.

Ensure that you're using bash to execute the script.

like image 34
devnull Avatar answered Jun 12 '26 10:06

devnull



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!