diff options
author | Egor Tensin <Egor.Tensin@gmail.com> | 2022-04-05 10:48:16 +0200 |
---|---|---|
committer | Egor Tensin <Egor.Tensin@gmail.com> | 2022-04-05 10:48:16 +0200 |
commit | 7e4f7ffd1b4fce35dde5d4f3a45f5d56506bccff (patch) | |
tree | 58a00dadb96c77d7ccea65fc184a1cfa654727c6 | |
parent | notes/bash: add syntax highlighting (diff) | |
download | blog-7e4f7ffd1b4fce35dde5d4f3a45f5d56506bccff.tar.gz blog-7e4f7ffd1b4fce35dde5d4f3a45f5d56506bccff.zip |
notes/bash: update
-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 |