| Subject: | removing null elements from an array |
|---|---|
| From: | "Poor Yorick" <org.gnu.lists.bug-bash@xxxxxxxxxxxxxx> |
| Date: | Thu, 17 Jul 2008 23:12:47 +0000 |
| Newsgroups: | gnu.bash.bug |
To get rid of null elements in an array, I currently do something like this:
bash-3.2$ var1=("with spaces" "more spaces" '' "the end")
bash-3.2$ for v in "${var1[@]}"; do if test "$v"; then var2+=("$v"); fi;
done
bash-3.2$ echo ${#var2[@]}
3
bash-3.2$ printf '%s\n' "${var2[@]}"
with spaces
more spaces
the end
Is there any other more concise way? It would be nice to avoid the second
variable by doing something like this:
bash-3.2$ var1=("${var1[@]:-}")
Is it a feasible syntax for future releases?
--
Yorick
|
| <Prev in Thread] | Current Thread | [Next in Thread> |
|---|---|---|
| ||
| Previous by Date: | Re: inconsistent treatment of backslash-bang, Chet Ramey |
|---|---|
| Next by Date: | Re: inconsistent treatment of backslash-bang, Lawrence D'Oliveiro |
| Previous by Thread: | patch number 035, Roman Rakus |
| Next by Thread: | Re: removing null elements from an array, Stephane Chazelas |
| Indexes: | [Date] [Thread] [Top] [All Lists] |