Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save laggardkernel/18422f9f21063da1a7ebabb96c004e3d to your computer and use it in GitHub Desktop.
Save laggardkernel/18422f9f21063da1a7ebabb96c004e3d to your computer and use it in GitHub Desktop.
catch empty string into array #bash
# 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