From 7e4f7ffd1b4fce35dde5d4f3a45f5d56506bccff Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Tue, 5 Apr 2022 10:48:16 +0200 Subject: notes/bash: update --- _notes/bash.md | 20 +++++++++++++++----- 1 file 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 -- cgit v1.2.3