How can I safely read each line of a file into an array in Bash 3.2?

0
0
Asked By MellowPine47 On

I need to read a text file line by line and store the results in an array. My current function works in newer Bash versions by using a variable name passed as an argument, but it relies on array features that are unavailable on macOS's default Bash 3.2. I also ran into problems with backslashes and other escaped characters being altered, apparently because of eval. What is the safest way to preserve each line exactly while remaining compatible with Bash 3.2?

1 Answer

Answered By VelvetOrbit63 On

The simplest solution may be to install a current Bash through a package manager such as Homebrew or MacPorts. The system Bash on older macOS releases is 3.2, so newer array conveniences are unavailable. Once a recent Bash is used, the original function works as expected.

MellowPine47 -

That is what I ended up doing. After installing a newer Bash, the function started behaving correctly without changing the file contents.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.