diff options
Diffstat (limited to '_notes')
-rw-r--r-- | _notes/bash.md | 20 |
1 files changed, 15 insertions, 5 deletions
diff --git a/_notes/bash.md b/_notes/bash.md index 1feef07..89d98fd 100644 --- a/_notes/bash.md +++ b/_notes/bash.md @@ -103,22 +103,23 @@ foo "$( bar )" # Even with errexit, foo will still get executed. #### Do ```bash -output="$( command )" +shopt -s lastpipe -while IFS= read -r line; do +command | while IFS= read -r line; do process_line "$line" -done <<< "$output" +done ``` #### Don't ```bash -# This causes some bash insanity where you cannot change directories or set -# variables inside a loop: http://mywiki.wooledge.org/BashFAQ/024 +# Without lastpipe, you cannot pipe into read: command | while IFS= read -r line; do process_line "$line" done +``` +```bash # errexit doesn't work here no matter what: while IFS= read -r line; do process_line "$line" @@ -126,6 +127,15 @@ done < <( command ) echo 'should never see this' ``` +```bash +# This would break if $output contains the \0 byte: +output="$( command )" + +while IFS= read -r line; do + process_line "$line" +done <<< "$output" +``` + ### Functions #### Do |