Created
June 18, 2019 05:34
-
-
Save laggardkernel/18422f9f21063da1a7ebabb96c004e3d to your computer and use it in GitHub Desktop.
catch empty string into array #bash
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# catch empty strings into an array using `read` | |
# normal array assignment isn't able to catch emptry string. | |
unset k 2>/dev/null | |
while IFS=$'\n' read -r item; do k+=("$item"); done < <(printf "\n\n\n") | |
echo ${#k[@]} | |
[[ -z ${k[0]} ]] && echo "empty string is catched" | |
f() { | |
local o=('' '' '') | |
printf '%s\n' "${o[@]}" | |
} | |
unset k 2>/dev/null | |
while IFS=$'\n' read -r item; do k+=("$item"); done < <(f) | |
echo ${#k[@]} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment