I want to split a string in a bash shell script with the following conditions.
1) The delimiter is a variable
2) The delimiter is multicharacater
example:
A quick brown fox
var=brown
I want to split the string into A quick and brown fox but using the variable var as delimiter and not brown
#!/bin/bash
#variables according to the example
example="A quick brown fox"
var="brown"
#the logic (using bash string replacement)
front=${example%${var}*}
rear=${example#*${var}}
#the output
echo "${front}"
echo "${var}"
echo "${rear}"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With