From 8947c4c99f4c1ad92f23e59e84b224cf35e4ff81 Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Fri, 15 Jul 2016 00:34:48 +1200 Subject: Quote expansions within parameter expansions I made the incorrect assumption that it was safe not to do this; expansions that include glob characters, for example, can cause problems. --- bash/bashrc.d/bd.bash | 2 +- bash/bashrc.d/cd.bash | 2 +- bash/bashrc.d/cf.bash | 2 +- bash/bashrc.d/keep.bash | 6 +++--- bash/bashrc.d/mysql.bash | 7 +++---- bash/bashrc.d/pass.bash | 4 ++-- bash/bashrc.d/prompt.bash | 2 +- bash/bashrc.d/pushd.bash | 2 +- bash/bashrc.d/td.bash | 6 +++--- bash/bashrc.d/ud.bash | 2 +- bash/bashrc.d/vared.bash | 2 +- bash/bashrc.d/vis.bash | 4 ++-- bash/bashrc.d/vr.bash | 2 +- 13 files changed, 21 insertions(+), 22 deletions(-) diff --git a/bash/bashrc.d/bd.bash b/bash/bashrc.d/bd.bash index 3850e973..b7fbe77e 100644 --- a/bash/bashrc.d/bd.bash +++ b/bash/bashrc.d/bd.bash @@ -89,7 +89,7 @@ _bd() { # Remove the last element in the array (the current directory) ((${#dirnames[@]})) || return 1 - dirnames=("${dirnames[@]:0:$((${#dirnames[@]}-1))}") + dirnames=("${dirnames[@]:0:"$((${#dirnames[@]}-1))"}") # Add the matching dirnames to the reply local dirname diff --git a/bash/bashrc.d/cd.bash b/bash/bashrc.d/cd.bash index 8557b8c9..04de96d5 100644 --- a/bash/bashrc.d/cd.bash +++ b/bash/bashrc.d/cd.bash @@ -20,7 +20,7 @@ cd() { done if (($# == 2)) ; then if [[ $PWD == *"$1"* ]] ; then - builtin cd "${opts[@]}" -- "${PWD/$1/$2}" + builtin cd "${opts[@]}" -- "${PWD/"$1"/"$2"}" else printf 'bash: %s: could not replace substring\n' \ "$FUNCNAME" >&2 diff --git a/bash/bashrc.d/cf.bash b/bash/bashrc.d/cf.bash index 587c44b3..9d6db7dd 100644 --- a/bash/bashrc.d/cf.bash +++ b/bash/bashrc.d/cf.bash @@ -3,7 +3,7 @@ cf() { local dirname # Specify directory to check - dirname=${1:-$PWD} + dirname=${1:-"$PWD"} # Error conditions if [[ ! -e $dirname ]] ; then diff --git a/bash/bashrc.d/keep.bash b/bash/bashrc.d/keep.bash index c7e42e04..585dd189 100644 --- a/bash/bashrc.d/keep.bash +++ b/bash/bashrc.d/keep.bash @@ -29,7 +29,7 @@ keep() { # Figure out the directory to which we're reading and writing these scripts local bashkeep - bashkeep=${BASHKEEP:-$HOME/.bashkeep.d} + bashkeep=${BASHKEEP:-"$HOME"/.bashkeep.d} mkdir -p -- "$bashkeep" || return # Parse options @@ -148,8 +148,8 @@ EOF complete -A function -A variable keep # Load any existing scripts in bashkeep -if [[ -d ${BASHKEEP:-$HOME/.bashkeep.d} ]] ; then - for bashkeep in "${BASHKEEP:-$HOME/.bashkeep.d}"/*.bash ; do +if [[ -d ${BASHKEEP:-"$HOME"/.bashkeep.d} ]] ; then + for bashkeep in "${BASHKEEP:-"$HOME"/.bashkeep.d}"/*.bash ; do [[ -e $bashkeep ]] || continue source "$bashkeep" done diff --git a/bash/bashrc.d/mysql.bash b/bash/bashrc.d/mysql.bash index 34debb1c..dd9ffe73 100644 --- a/bash/bashrc.d/mysql.bash +++ b/bash/bashrc.d/mysql.bash @@ -15,10 +15,9 @@ mysql() { config=$HOME/.mysql/$1.cnf if [[ -r $config ]] ; then shift - command mysql --defaults-extra-file="$config" "$@" - else - command mysql "$@" + set -- --defaults-extra-file="$config" "$@" fi + command mysql "$@" } # Completion setup for MySQL for configured databases @@ -44,7 +43,7 @@ _mysql() { # Collect all the config file names, strip off leading path and .cnf local -a cnfs cnfs=("$dirname"/"${COMP_WORDS[COMP_CWORD]}"*.cnf) - cnfs=("${cnfs[@]#$dirname/}") + cnfs=("${cnfs[@]#"$dirname"/}") cnfs=("${cnfs[@]%.cnf}") # Bail if no files to prevent empty output diff --git a/bash/bashrc.d/pass.bash b/bash/bashrc.d/pass.bash index 47f9effe..28941952 100644 --- a/bash/bashrc.d/pass.bash +++ b/bash/bashrc.d/pass.bash @@ -7,7 +7,7 @@ _pass() { # If we can't read the password directory, just bail local passdir - passdir=${PASSWORD_STORE_DIR:-$HOME/.password-store} + passdir=${PASSWORD_STORE_DIR:-"$HOME"/.password-store} [[ -r $passdir ]] || return 1 # Iterate through list of .gpg paths, extension stripped, null-delimited, @@ -26,7 +26,7 @@ _pass() declare -a entries entries=("$passdir"/"${COMP_WORDS[COMP_CWORD]}"*/**/*.gpg \ "$passdir"/"${COMP_WORDS[COMP_CWORD]}"*.gpg) - entries=("${entries[@]#$passdir/}") + entries=("${entries[@]#"$passdir"/}") entries=("${entries[@]%.gpg}") # Bail if no entries to prevent empty output diff --git a/bash/bashrc.d/prompt.bash b/bash/bashrc.d/prompt.bash index 75bb197c..b6847a92 100644 --- a/bash/bashrc.d/prompt.bash +++ b/bash/bashrc.d/prompt.bash @@ -214,7 +214,7 @@ prompt() { # name, removing leading slashes and the 'branches' prefix, and any # trailing content after a slash local branch - branch=${url/$root} + branch=${url/"$root"} branch=${branch#/} branch=${branch#branches/} branch=${branch%%/*} diff --git a/bash/bashrc.d/pushd.bash b/bash/bashrc.d/pushd.bash index 6c8a2174..ec621037 100644 --- a/bash/bashrc.d/pushd.bash +++ b/bash/bashrc.d/pushd.bash @@ -1,4 +1,4 @@ # Make pushd default to $HOME if no arguments given, much like cd pushd() { - builtin pushd "${@:-$HOME}" + builtin pushd "${@:-"$HOME"}" } diff --git a/bash/bashrc.d/td.bash b/bash/bashrc.d/td.bash index 9f47c973..ffb9c973 100644 --- a/bash/bashrc.d/td.bash +++ b/bash/bashrc.d/td.bash @@ -1,7 +1,7 @@ # Complete filenames for td(1) _td() { local dir - dir=${TODO_DIR:-$HOME/Todo} + dir=${TODO_DIR:-"$HOME"/Todo} while IFS= read -rd '' fn ; do COMPREPLY[${#COMPREPLY[@]}]=$fn done < <( @@ -9,9 +9,9 @@ _td() { shopt -u dotglob local -a fns fns=("$dir"/"${COMP_WORDS[COMP_CWORD]}"*) - fns=("${fns[@]#$dir/}") + fns=("${fns[@]#"$dir"/}") ((${#fns[@]})) || exit 1 - printf '%s\0' "${fns[@]##$dir/}" + printf '%s\0' "${fns[@]##"$dir"/}" ) return } diff --git a/bash/bashrc.d/ud.bash b/bash/bashrc.d/ud.bash index 2bed481b..a1792968 100644 --- a/bash/bashrc.d/ud.bash +++ b/bash/bashrc.d/ud.bash @@ -33,7 +33,7 @@ ud() { # Check and save optional second argument, target directory; default to # $PWD (typical usage case) local dirname - dirname=${2:-$PWD} + dirname=${2:-"$PWD"} if [[ ! -e $dirname ]] ; then printf 'bash: %s: Target directory %s does not exist\n' "$FUNCNAME" "$2" >&2 return 1 diff --git a/bash/bashrc.d/vared.bash b/bash/bashrc.d/vared.bash index 940b0900..510631ca 100644 --- a/bash/bashrc.d/vared.bash +++ b/bash/bashrc.d/vared.bash @@ -25,7 +25,7 @@ vared() { fi local name for name ; do - IFS= read -e -i "${!name}" -p "${prompt:-$name=}" -r -- "$name" + IFS= read -e -i "${!name}" -p "${prompt:-"$name"=}" -r -- "$name" done } complete -A variable vared diff --git a/bash/bashrc.d/vis.bash b/bash/bashrc.d/vis.bash index 17c89ca8..f84cb702 100644 --- a/bash/bashrc.d/vis.bash +++ b/bash/bashrc.d/vis.bash @@ -2,14 +2,14 @@ # ~/.local/bin _vis() { local vispath - vispath=${VISPATH:-$HOME/.local/bin} + vispath=${VISPATH:-"$HOME"/.local/bin} [[ -d $vispath ]] || return while IFS= read -rd '' executable ; do COMPREPLY[${#COMPREPLY[@]}]=$executable done < <( shopt -s dotglob nullglob declare -a files - files=("${VISPATH:-$HOME/.local/bin}"/"${COMP_WORDS[COMP_CWORD]}"*) + files=("${VISPATH:-"$HOME"/.local/bin}"/"${COMP_WORDS[COMP_CWORD]}"*) declare -a executables for file in "${files[@]}" ; do [[ -f $file && -x $file ]] || continue diff --git a/bash/bashrc.d/vr.bash b/bash/bashrc.d/vr.bash index bbb37758..04a86835 100644 --- a/bash/bashrc.d/vr.bash +++ b/bash/bashrc.d/vr.bash @@ -1,7 +1,7 @@ # Move to the root directory of a VCS working copy vr() { local path - path=${1:-$PWD} + path=${1:-"$PWD"} path=${path%/} # Raise some helpful errors -- cgit v1.2.3