Skip to content

Commit 9ba5831

Browse files
committed
fix: variable quoting fixes
As prompted by shellcheck SC2086.
1 parent cd5e98e commit 9ba5831

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+252
-235
lines changed

bash_completion

Lines changed: 39 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ _comp_have_command()
120120
{
121121
# Completions for system administrator commands are installed as well in
122122
# case completion is attempted via `sudo command ...'.
123-
PATH=$PATH:/usr/sbin:/sbin:/usr/local/sbin type $1 &>/dev/null
123+
PATH=$PATH:/usr/sbin:/sbin:/usr/local/sbin type "$1" &>/dev/null
124124
}
125125

126126
_comp_deprecate_func _have _comp_have_command
@@ -132,7 +132,7 @@ _comp_deprecate_func _have _comp_have_command
132132
have()
133133
{
134134
unset -v have
135-
_comp_have_command $1 && have=yes
135+
_comp_have_command "$1" && have=yes
136136
}
137137

138138
# This function checks whether a given readline variable
@@ -274,10 +274,11 @@ _upvar()
274274
echo "bash_completion: $FUNCNAME: deprecated function," \
275275
"use _upvars instead" >&2
276276
if unset -v "$1"; then # Unset & validate varname
277+
# shellcheck disable=SC2140 # TODO
277278
if (($# == 2)); then
278-
eval $1=\"\$2\" # Return single value
279+
eval "$1"=\"\$2\" # Return single value
279280
else
280-
eval $1=\(\"\$"{@:2}"\"\) # Return array
281+
eval "$1"=\(\"\$"{@:2}"\"\) # Return array
281282
fi
282283
fi
283284
}
@@ -312,8 +313,8 @@ _upvars()
312313
return 1
313314
}
314315
# Assign array of -aN elements
315-
# shellcheck disable=SC2015 # TODO
316-
[[ $2 ]] && unset -v "$2" && eval $2=\(\"\$"{@:3:${1#-a}}"\"\) &&
316+
# shellcheck disable=SC2015,SC2140 # TODO
317+
[[ $2 ]] && unset -v "$2" && eval "$2"=\(\"\$"{@:3:${1#-a}}"\"\) &&
317318
shift $((${1#-a} + 2)) || {
318319
echo bash_completion: \
319320
"$FUNCNAME: \`$1${2+ }$2': missing argument(s)" \
@@ -324,7 +325,7 @@ _upvars()
324325
-v)
325326
# Assign single value
326327
# shellcheck disable=SC2015 # TODO
327-
[[ $2 ]] && unset -v "$2" && eval $2=\"\$3\" &&
328+
[[ $2 ]] && unset -v "$2" && eval "$2"=\"\$3\" &&
328329
shift 3 || {
329330
echo "bash_completion: $FUNCNAME: $1:" \
330331
"missing argument(s)" >&2
@@ -559,8 +560,8 @@ __get_cword_at_cursor_by_ref()
559560
((index < 0)) && index=0
560561
fi
561562

562-
local "$2" "$3" "$4" && _upvars -a${#words[@]} $2 ${words+"${words[@]}"} \
563-
-v $3 "$cword" -v $4 "${cur:0:index}"
563+
local "$2" "$3" "$4" && _upvars -a${#words[@]} "$2" ${words+"${words[@]}"} \
564+
-v "$3" "$cword" -v "$4" "${cur:0:index}"
564565
}
565566

566567
# Get the word to complete and optional previous words.
@@ -771,9 +772,9 @@ _quote_readline_by_ref()
771772
{
772773
if [[ $1 == \'* ]]; then
773774
# Leave out first character
774-
printf -v $2 %s "${1:1}"
775+
printf -v "$2" %s "${1:1}"
775776
else
776-
printf -v $2 %q "$1"
777+
printf -v "$2" %q "$1"
777778

778779
# If result becomes quoted like this: $'string', re-evaluate in order
779780
# to drop the additional quoting. See also:
@@ -831,7 +832,7 @@ _filedir()
831832

832833
reset=$(shopt -po noglob)
833834
set -o noglob
834-
toks+=($(compgen "${opts[@]}" -- $quoted))
835+
toks+=($(compgen "${opts[@]}" -- "$quoted"))
835836
IFS=' '
836837
$reset
837838
IFS=$'\n'
@@ -841,7 +842,7 @@ _filedir()
841842
$arg && ${#toks[@]} -lt 1 ]] && {
842843
reset=$(shopt -po noglob)
843844
set -o noglob
844-
toks+=($(compgen -f ${plusdirs+"${plusdirs[@]}"} -- $quoted))
845+
toks+=($(compgen -f ${plusdirs+"${plusdirs[@]}"} -- "$quoted"))
845846
IFS=' '
846847
$reset
847848
IFS=$'\n'
@@ -883,8 +884,8 @@ _variables()
883884
# Completing $var / ${var / ${!var / ${#var
884885
if [[ $cur == '${'* ]]; then
885886
local arrs vars
886-
vars=($(compgen -A variable -P ${BASH_REMATCH[1]} -S '}' -- ${BASH_REMATCH[3]}))
887-
arrs=($(compgen -A arrayvar -P ${BASH_REMATCH[1]} -S '[' -- ${BASH_REMATCH[3]}))
887+
vars=($(compgen -A variable -P "${BASH_REMATCH[1]}" -S '}' -- "${BASH_REMATCH[3]}"))
888+
arrs=($(compgen -A arrayvar -P "${BASH_REMATCH[1]}" -S '[' -- "${BASH_REMATCH[3]}"))
888889
if ((${#vars[@]} == 1 && ${#arrs[@]} != 0)); then
889890
# Complete ${arr with ${array[ if there is only one match, and that match is an array variable
890891
compopt -o nospace
@@ -902,7 +903,7 @@ _variables()
902903
# Complete ${array[i with ${array[idx]}
903904
local reset=$(shopt -po noglob) IFS=$'\n'
904905
set -o noglob
905-
COMPREPLY+=($(compgen -W '$(printf %s\\n "${!'${BASH_REMATCH[2]}'[@]}")' \
906+
COMPREPLY+=($(compgen -W '$(printf %s\\n "${!'"${BASH_REMATCH[2]}"'[@]}")' \
906907
-P "${BASH_REMATCH[1]}${BASH_REMATCH[2]}[" -S ']}' -- "${BASH_REMATCH[3]}"))
907908
IFS=$' \t\n'
908909
$reset
@@ -1101,7 +1102,7 @@ _comp_initialize()
11011102
;;
11021103
esac
11031104
cur=${cur##"$redir"}
1104-
_filedir $xspec
1105+
_filedir "$xspec"
11051106
return 1
11061107
fi
11071108

@@ -1187,7 +1188,10 @@ _parse_help()
11871188
(
11881189
case $cmd in
11891190
-) exec cat ;;
1190-
*) _comp_dequote "$cmd" && LC_ALL=C "$ret" ${2:---help} 2>&1 ;;
1191+
*)
1192+
# shellcheck disable=SC2086
1193+
_comp_dequote "$cmd" && LC_ALL=C "$ret" ${2:---help} 2>&1
1194+
;;
11911195
esac
11921196
) |
11931197
while read -r line; do
@@ -1224,7 +1228,10 @@ _parse_usage()
12241228
(
12251229
case $cmd in
12261230
-) exec cat ;;
1227-
*) _comp_dequote "$cmd" && LC_ALL=C "$ret" ${2:---usage} 2>&1 ;;
1231+
*)
1232+
# shellcheck disable=SC2086
1233+
_comp_dequote "$cmd" && LC_ALL=C "$ret" ${2:---usage} 2>&1
1234+
;;
12281235
esac
12291236
) |
12301237
while read -r line; do
@@ -1237,7 +1244,7 @@ _parse_usage()
12371244
# Treat as bundled short options
12381245
for ((i = 1; i < ${#option}; i++)); do
12391246
char=${option:i:1}
1240-
[[ $char != '[' ]] && printf '%s\n' -$char && rc=0
1247+
[[ $char != '[' ]] && printf '%s\n' -"$char" && rc=0
12411248
done
12421249
;;
12431250
*)
@@ -1392,7 +1399,7 @@ _ncpus()
13921399
local var=NPROCESSORS_ONLN
13931400
[[ $OSTYPE == *@(linux|msys|cygwin)* ]] && var=_$var
13941401
local n=$(getconf $var 2>/dev/null)
1395-
printf %s ${n:-1}
1402+
printf %s "${n:-1}"
13961403
}
13971404

13981405
# Perform tilde (~) completion
@@ -1409,7 +1416,7 @@ _tilde()
14091416
# 2>/dev/null for direct invocation, e.g. in the _tilde unit test
14101417
((result > 0)) && compopt -o filenames 2>/dev/null
14111418
fi
1412-
return $result
1419+
return "$result"
14131420
}
14141421

14151422
# Expand variable starting with tilde (~)
@@ -1437,7 +1444,7 @@ _tilde()
14371444
__expand_tilde_by_ref()
14381445
{
14391446
if [[ ${!1-} == \~* ]]; then
1440-
eval $1="$(printf ~%q "${!1#\~}")"
1447+
eval "$1"="$(printf ~%q "${!1#\~}")"
14411448
fi
14421449
} # __expand_tilde_by_ref()
14431450

@@ -1662,7 +1669,7 @@ _modules()
16621669
{
16631670
local modpath
16641671
modpath=/lib/modules/$1
1665-
COMPREPLY=($(compgen -W "$(command ls -RL $modpath 2>/dev/null |
1672+
COMPREPLY=($(compgen -W "$(command ls -RL "$modpath" 2>/dev/null |
16661673
command sed -ne 's/^\(.*\)\.k\{0,1\}o\(\.[gx]z\)\{0,1\}$/\1/p' \
16671674
-e 's/^\(.*\)\.ko\.zst$/\1/p')" -- "$cur"))
16681675
}
@@ -1938,6 +1945,7 @@ _known_hosts()
19381945
local options
19391946
[[ ${1-} == -a || ${2-} == -a ]] && options=-a
19401947
[[ ${1-} == -c || ${2-} == -c ]] && options+=" -c"
1948+
# shellcheck disable=SC2086
19411949
_known_hosts_real ${options-} -- "$cur"
19421950
} # _known_hosts()
19431951

@@ -1977,7 +1985,7 @@ _included_ssh_config_files()
19771985
if [[ -r $f && ! -d $f ]]; then
19781986
config+=("$f")
19791987
# The Included file is processed to look for Included files in itself
1980-
_included_ssh_config_files $f
1988+
_included_ssh_config_files "$f"
19811989
fi
19821990
done
19831991
fi
@@ -2240,7 +2248,7 @@ _cd()
22402248
for i in ${CDPATH//:/$'\n'}; do
22412249
# create an array of matched subdirs
22422250
k=${#COMPREPLY[@]}
2243-
for j in $(compgen -d -- $i/$cur); do
2251+
for j in $(compgen -d -- "$i/$cur"); do
22442252
if [[ ($mark_symdirs && -L $j || $mark_dirs && ! -L $j) && ! -d ${j#"$i/"} ]]; then
22452253
j+="/"
22462254
fi
@@ -2365,7 +2373,7 @@ _comp_command_offset()
23652373
# FIXME: should we take "+o opt" into account?
23662374
cspec=${cspec#*-o }
23672375
opt=${cspec%% *}
2368-
compopt -o $opt
2376+
compopt -o "$opt"
23692377
cspec=${cspec#"$opt"}
23702378
done
23712379
else
@@ -2457,7 +2465,7 @@ _longopt()
24572465
COMPREPLY=($(compgen -W "$(LC_ALL=C $1 --help 2>&1 |
24582466
while read -r line; do
24592467
[[ $line =~ --[A-Za-z0-9]+([-_][A-Za-z0-9]+)*=? ]] &&
2460-
printf '%s\n' ${BASH_REMATCH[0]}
2468+
printf '%s\n' "${BASH_REMATCH[0]}"
24612469
done)" -- "$cur"))
24622470
[[ ${COMPREPLY-} == *= ]] && compopt -o nospace
24632471
elif [[ $1 == *@(rmdir|chroot) ]]; then
@@ -2490,7 +2498,7 @@ _filedir_xspec()
24902498
toks=($(
24912499
compgen -d -- "$(quote_readline "$cur")" | {
24922500
while read -r tmp; do
2493-
printf '%s\n' $tmp
2501+
printf '%s\n' "$tmp"
24942502
done
24952503
}
24962504
))
@@ -2509,7 +2517,7 @@ _filedir_xspec()
25092517
toks+=($(
25102518
eval compgen -f -X "'!$xspec'" -- '$(quote_readline "$cur")' | {
25112519
while read -r tmp; do
2512-
[[ $tmp ]] && printf '%s\n' $tmp
2520+
[[ $tmp ]] && printf '%s\n' "$tmp"
25132521
done
25142522
}
25152523
))
@@ -2750,7 +2758,7 @@ unset -v _comp__init_compat_dir _comp__init_file
27502758
# garbages of suppressed command outputs.
27512759
_comp__init_user_file=${BASH_COMPLETION_USER_FILE:-~/.bash_completion}
27522760
[[ $_comp__init_user_file != "${BASH_SOURCE[0]}" && $_comp__init_user_file != /dev/null && -r $_comp__init_user_file && -f $_comp__init_user_file ]] &&
2753-
. $_comp__init_user_file
2761+
. "$_comp__init_user_file"
27542762
unset -v _comp__init_user_file
27552763
27562764
unset -f have

completions/7z

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ _7z()
1515

1616
case $cur in
1717
-ao*)
18-
COMPREPLY=($(compgen -P${cur:0:3} -W 'a s t u' -- "${cur:3}"))
18+
COMPREPLY=($(compgen -P"${cur:0:3}" -W 'a s t u' -- "${cur:3}"))
1919
return
2020
;;
2121
-?(a)[ix]*)
@@ -26,7 +26,7 @@ _7z()
2626
opt=${cur:0:2} cur=${cur:2}
2727
fi
2828
if [[ $cur != *[@\!]* ]]; then
29-
COMPREPLY=($(compgen -P$opt -W '@ ! r@ r-@ r0@ r! r-! r0!' \
29+
COMPREPLY=($(compgen -P"$opt" -W '@ ! r@ r-@ r0@ r! r-! r0!' \
3030
-- "$cur"))
3131
elif [[ $cur == ?(r@(-|0|))@* ]]; then
3232
local IFS=$' \t\n' reset=$(shopt -po noglob)
@@ -50,31 +50,31 @@ _7z()
5050
set -o noglob
5151
compopt -o filenames
5252
local IFS=$'\n'
53-
COMPREPLY=($(compgen -d -P${cur:0:2} -S/ -- "${cur:2}"))
53+
COMPREPLY=($(compgen -d -P"${cur:0:2}" -S/ -- "${cur:2}"))
5454
_comp_unlocal IFS
5555
$reset
5656
compopt -o nospace
5757
return
5858
;;
5959
-r?*)
60-
COMPREPLY=($(compgen -P${cur:0:2} -W '- 0' -- "${cur:2}"))
60+
COMPREPLY=($(compgen -P"${cur:0:2}" -W '- 0' -- "${cur:2}"))
6161
return
6262
;;
6363
-scs*)
64-
COMPREPLY=($(compgen -P${cur:0:4} -W 'UTF-8 WIN DOS' \
64+
COMPREPLY=($(compgen -P"${cur:0:4}" -W 'UTF-8 WIN DOS' \
6565
-- "${cur:4}"))
6666
return
6767
;;
6868
-ssc?*)
69-
COMPREPLY=($(compgen -P${cur:0:4} -W '-' -- "${cur:4}"))
69+
COMPREPLY=($(compgen -P"${cur:0:4}" -W '-' -- "${cur:4}"))
7070
return
7171
;;
7272
-t*)
7373
if [[ $mode == w ]]; then
74-
COMPREPLY=($(compgen -P${cur:0:2} -W '7z bzip2 gzip swfc
74+
COMPREPLY=($(compgen -P"${cur:0:2}" -W '7z bzip2 gzip swfc
7575
tar wim xz zip' -- "${cur:2}"))
7676
else
77-
COMPREPLY=($(compgen -P${cur:0:2} -W '7z apm arj bzip2 cab
77+
COMPREPLY=($(compgen -P"${cur:0:2}" -W '7z apm arj bzip2 cab
7878
chm cpio cramfs deb dmg elf fat flv gzip hfs iso lzh lzma
7979
lzma86 macho mbr mslz mub nsis ntfs pe ppmd rar rpm
8080
squashfs swf swfc tar udf vhd wim xar xz z zip' \
@@ -114,7 +114,7 @@ _7z()
114114
else
115115
if [[ ${words[1]} == d ]]; then
116116
local IFS=$'\n'
117-
COMPREPLY=($(compgen -W "$(printf '%s\n' "$($1 l ${words[2]} \
117+
COMPREPLY=($(compgen -W "$(printf '%s\n' "$($1 l "${words[2]}" \
118118
-slt 2>/dev/null | command sed -n '/^Path =/s/^Path = \(.*\)$/\1/p' \
119119
2>/dev/null | tail -n+2)")" -- "$cur"))
120120
compopt -o filenames

completions/_adb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ _adb()
4949

5050
# TODO: more and better command completions
5151

52-
_adb_command_usage "$1" $cmd
52+
_adb_command_usage "$1" "$cmd"
5353

5454
case $cmd in
5555
push | restore | sideload)

completions/_mock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ _mock()
2727
return
2828
;;
2929
-r | --root)
30-
COMPREPLY=($(compgen -W "$(command ls $cfgdir)" -- "$cur"))
30+
COMPREPLY=($(compgen -W "$(command ls "$cfgdir")" -- "$cur"))
3131
COMPREPLY=(${COMPREPLY[@]/%.cfg/})
3232
return
3333
;;

completions/_modules

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@
2323

2424
_module_list()
2525
{
26-
local modules="$(command sed 's/:/ /g' <<<$LOADEDMODULES | sort)"
27-
compgen -W "$modules" -- $1
26+
local modules="$(command sed 's/:/ /g' <<<"$LOADEDMODULES" | sort)"
27+
compgen -W "$modules" -- "$1"
2828
}
2929

3030
_module_path()
3131
{
32-
local modules="$(command sed 's/:/ /g' <<<$MODULEPATH | sort)"
33-
compgen -W "$modules" -- $1
32+
local modules="$(command sed 's/:/ /g' <<<"$MODULEPATH" | sort)"
33+
compgen -W "$modules" -- "$1"
3434
}
3535

3636
_module_avail()
@@ -41,7 +41,7 @@ _module_avail()
4141
xargs printf '%s\n' | command sed -e 's/(default)//g' | sort
4242
)"
4343

44-
compgen -W "$modules" -- $1
44+
compgen -W "$modules" -- "$1"
4545
}
4646

4747
# A completion function for the module alias

completions/_mount

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ _mount()
4444
host=${host%%/*}
4545
if [[ $host ]]; then
4646
COMPREPLY=($(compgen -P "//$host" -W \
47-
"$(smbclient -d 0 -NL $host 2>/dev/null |
47+
"$(smbclient -d 0 -NL "$host" 2>/dev/null |
4848
command sed -ne '/^[[:blank:]]*Sharename/,/^$/p' |
4949
command sed -ne '3,$s|^[^A-Za-z]*\([^[:blank:]]*\).*$|/\1|p')" \
5050
-- "${cur#//"$host"}"))

completions/_mount.linux

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ _mount()
238238
host=${host%%/*}
239239
if [[ $host ]]; then
240240
COMPREPLY=($(compgen -P "//$host" -W \
241-
"$(smbclient -d 0 -NL $host 2>/dev/null |
241+
"$(smbclient -d 0 -NL "$host" 2>/dev/null |
242242
command sed -ne '/^[[:blank:]]*Sharename/,/^$/p' |
243243
command sed -ne '3,$s|^[^A-Za-z]*\([^[:blank:]]*\).*$|/\1|p')" \
244244
-- "${cur#//"$host"}"))

0 commit comments

Comments
 (0)