Re: BASH reference for those who are "learning by doing"?
On Sun, Sep 08, 2024 at 06:48:30 +0200, Sirius wrote:
> Bash has some nifty uses when it comes to variables.
>
> If you just want to store a file in a variable,
> VAR="$(</path/to/file)
> will do it. If you want to do an array instead, use the 'while read line;
> do' construct. As others have pointed out, this is not an optimal usecase
> for bash.
Unless you're targeting very old versions of bash, you can read the lines
of a file/stream into an array with the "readarray" (or "mapfile") command.
You can specify a single character to act as the line terminator, or
let the default newline character $'\n' be used. In sufficiently new
versions of bash, you can specify '' to use NUL as the line terminator.
For example, in bash 4.4 or higher:
readarray -t -d '' myfiles < <(
find . -type f -print0
)
You almost always want the -t option, to strip the delimiter from the
array elements. In the case of NUL, it makes no difference, but it's
a good habit to have if you ever use this technique on text files.
Just to reiterate what others have said: do *not* use bash's text
processing tools or regular-expression-based techniques to parse HTML.
See <https://stackoverflow.com/q/1732348>.
Reply to: