Splitting string by the first occurrence of a delimiter
cut
sounds like a suitable tool for this:
bash-4.2$ s='id;some text here with possible ; inside'
bash-4.2$ id="$( cut -d ';' -f 1 <<< "$s" )"; echo "$id"
id
bash-4.2$ string="$( cut -d ';' -f 2- <<< "$s" )"; echo "$string"
some text here with possible ; inside
But read
is even more suitable:
bash-4.2$ IFS=';' read -r id string <<< "$s"
bash-4.2$ echo "$id"
id
bash-4.2$ echo "$string"
some text here with possible ; inside
With any standard sh (including bash):
sep=';'
case $s in
(*"$sep"*)
before=${s%%"$sep"*}
after=${s#*"$sep"}
;;
(*)
before=$s
after=
;;
esac
read
based solutions would work for single character (and with some shells, single-byte) values of $sep
other than space, tab or newline and only if $s
doesn't contain newline characters.
cut
based solutions would only work if $s
doesn't contain newline characters.
sed
solutions could be devised that handle all the corner cases with any value of $sep
, but it's not worth going that far when there's builtin support in the shell for that.
As you have mentioned that you want to assign the values to id and string
first assign your pattern to a variable(say str)
str='id;some text here with possible ; inside'
id=${str%%;}
string=${str#;}
Now you have your values in respective variables