From 1569bb18ae9071dd56142f226a807783305309ab Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Wed, 29 Aug 2018 12:36:45 -0400 Subject: Rely on Bash 4.4 empty array behavior --- HACKING/code-quality.md | 11 +---------- src/chroot-tools/librechroot | 12 ++++++------ 2 files changed, 7 insertions(+), 16 deletions(-) diff --git a/HACKING/code-quality.md b/HACKING/code-quality.md index f296621..06f301d 100644 --- a/HACKING/code-quality.md +++ b/HACKING/code-quality.md @@ -43,15 +43,6 @@ checked because of it. Use `set -u` if you can; it makes using an unset variable an error. - If a variable not being set is valid (perhaps a configuration option), use `${var:-}` when accessing it to suppress the error. - - An empty array counts as unset, so if you have an array that may be - empty, use `${var+"${var[@]}"}` (don't put quotes around the outer - pair of braces) to only access it if it's non-empty. - - The reason for this is that a normal string variable is basically - an array with length=1; an unset variable looks like an array - with length=0. Weird stuff. - - Actually, that was only true in Bash <4.4; but since the change - wasn't mentioned in the NEWS, I suspect that a subsequent version - will revert to the pre-4.4 behavior. In the shebang, use `#!/usr/bin/env bash`. This allows us to not hardcode the location of bash (I'm not sure why this is useful for @@ -85,7 +76,7 @@ guidelines They're pretty good, and cover most of the "gotcha's" about Bash syntax. Though, it mentions but discourages the use of Bash 3 features... why? Who still uses Bash 2? For libretools, you should -feel free to use Bash 4 features! +feel free to use Bash 4.4 features! I wrote an article on Bash arrays . A lot of people think diff --git a/src/chroot-tools/librechroot b/src/chroot-tools/librechroot index 0b0ce0c..1896711 100755 --- a/src/chroot-tools/librechroot +++ b/src/chroot-tools/librechroot @@ -54,7 +54,7 @@ hack_arch_nspawn_flags() { local makepkg_conf="$copydir/etc/makepkg.conf" OPTIND=1 - set -- ${arch_nspawn_flags+"${arch_nspawn_flags[@]}"} + set -- "${arch_nspawn_flags[@]}" while getopts 'hC:M:c:f:s' arg; do case "$arg" in M) makepkg_conf="$OPTARG" ;; @@ -103,13 +103,13 @@ arch-nspawn() { local copydir=$1; shift local cmd=("$@") - local arch_nspawn_flags=(${arch_nspawn_flags+"${arch_nspawn_flags[@]}"}) + local arch_nspawn_flags=("${arch_nspawn_flags[@]}") hack_arch_nspawn_flags "$copydir" "$_arch_nspawn" \ - ${arch_nspawn_flags+"${arch_nspawn_flags[@]}"} \ + "${arch_nspawn_flags[@]}" \ "$copydir" \ - ${sysd_nspawn_flags+"${sysd_nspawn_flags[@]}"} \ + "${sysd_nspawn_flags[@]}" \ -- \ "${cmd[@]}" } @@ -119,11 +119,11 @@ mkarchroot() { local copydir=$1; shift local pkgs=("$@") - local arch_nspawn_flags=(${arch_nspawn_flags+"${arch_nspawn_flags[@]}"}) + local arch_nspawn_flags=("${arch_nspawn_flags[@]}") hack_arch_nspawn_flags "$copydir" unshare -m "$_mkarchroot" \ - ${arch_nspawn_flags+"${arch_nspawn_flags[@]}"} \ + "${arch_nspawn_flags[@]}" \ "$copydir" \ "${pkgs[@]}" } -- cgit v1.2.2