1

I'm parsing a HTML page with a bash script and extracting (with grep and sed)

    string number one
    string number two
    string number three
    etc

as $str in a parsing loop followed by

    array+=($str)

When the array is printed with

    for each in "${array[@]}"
    do
        echo "$each"
    done

I end up with

    string
    number
    one
    string
    number
    ....

Clearly spaces are being used as a delimiter when adding a new element in the array, but I can't seem to work out how to pass the entire string as an element!

2
  • 2
    Unquoted parameter expansions are wrong until proven otherwise. Commented Oct 22, 2016 at 14:09
  • I found your comment hilarious and very important to commit to memory @chepner, thank you for commenting. Commented Oct 23, 2016 at 4:55

1 Answer 1

2

To avoid splitting each element of the array on whitespace, you should double quote your variable:

array+=("$str")
Sign up to request clarification or add additional context in comments.

1 Comment

I was certain I had tested quotation marks in testing, apparently not. Thanks for the help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.