Skip to main content
edited body
Source Link
Aaron
  • 6.7k
  • 7
  • 41
  • 78

I've browsed questions for splitting input based on a delimitercharacter but can't quite figure out multiple delimiterscharacters based on a condition:

Say I had a simply bash script that split input separated by spaces into an array:

echo "Terms:"
read terms            // foo bar hello world
array=(${terms// / }) // ["foo", "bar", "hello", "world"]

I want an extra condition where if terms are encapsulated by another delimitercharacter, the whole phrase should be split as one.

e.g. encapsulated with a back tick:

echo "Terms:"
read terms            // foo bar `hello world`
{conditional here}    // ["foo", "bar", "hello world"]

I've browsed questions for splitting input based on a delimiter but can't quite figure out multiple delimiters based on a condition:

Say I had a simply bash script that split input separated by spaces into an array:

echo "Terms:"
read terms            // foo bar hello world
array=(${terms// / }) // ["foo", "bar", "hello", "world"]

I want an extra condition where if terms are encapsulated by another delimiter, the whole phrase should be split as one.

e.g. encapsulated with a back tick:

echo "Terms:"
read terms            // foo bar `hello world`
{conditional here}    // ["foo", "bar", "hello world"]

I've browsed questions for splitting input based on a character but can't quite figure out multiple characters based on a condition:

Say I had a simply bash script that split input separated by spaces into an array:

echo "Terms:"
read terms            // foo bar hello world
array=(${terms// / }) // ["foo", "bar", "hello", "world"]

I want an extra condition where if terms are encapsulated by another character, the whole phrase should be split as one.

e.g. encapsulated with a back tick:

echo "Terms:"
read terms            // foo bar `hello world`
{conditional here}    // ["foo", "bar", "hello world"]
Source Link
Aaron
  • 6.7k
  • 7
  • 41
  • 78

Bash conditionally split string into array

I've browsed questions for splitting input based on a delimiter but can't quite figure out multiple delimiters based on a condition:

Say I had a simply bash script that split input separated by spaces into an array:

echo "Terms:"
read terms            // foo bar hello world
array=(${terms// / }) // ["foo", "bar", "hello", "world"]

I want an extra condition where if terms are encapsulated by another delimiter, the whole phrase should be split as one.

e.g. encapsulated with a back tick:

echo "Terms:"
read terms            // foo bar `hello world`
{conditional here}    // ["foo", "bar", "hello world"]