How can I read line by line from a variable in bash?
You can use a while loop with process substitution:
while read -r line do echo "$line" done < <(jobs)
An optimal way to read a multiline variable is to set a blank
printfthe variable in with a trailing newline:
# Printf '%s\n' "$var" is necessary because printf '%s' "$var" on a # variable that doesn't end with a newline then the while loop will # completely miss the last line of the variable. while IFS= read -r line do echo "$line" done < <(printf '%s\n' "$var")
Note: As per shellcheck sc2031, the use of process substition is preferable to a pipe to avoid [subtly] creating an subshell.
Also, please realize that by naming the variable
jobsit may cause confusion since that is also the name of a common shell command.
If you want to keep all your whitespace, then use `while IFS= read` .... If you want to prevent \ interpretation, then use `read -r`
I've fixed the points fred.bear mentioned, as well as changed `echo` to `printf %s`, so that your script would work even with non-tame input.
To read from a multiline variable, a herestring is preferable to piping from printf (see l0b0's answer).
@ata Though I've heard this "preferable" often enough, it must be noted that a herestring always requires the `/tmp` directory to be writable, as it relies on being able to create a temporary work file. Should you ever find yourself on a restricted system with `/tmp` being read-only (and not changeable by you), you will be happy about the possibility of using an alternate solution, e. g. with the `printf` pipe.
You're welcome. TBH, I've been through this "hell" once before you, to know more see this question.