Skip to content

Commit a885d3e

Browse files
committed
Fix error when reading command output to array
compgen output may contain whitespace and/or wildcards. Naive ($(...)) approach would parse such inputs incorrectly Resulting COMPREPLY array needs sanitization in case any of the values contain special characted, hence the extra loop with printf. Relevant blog post: https://www.baeldung.com/linux/reading-output-into-array Close #23
1 parent 42feefd commit a885d3e

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

bash_completion

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env bash
22
#
3-
# bash-complete-partial-path v1.0.0
3+
# bash-complete-partial-path v1.1.0-dev
44
#
55
# Zsh-like expansion of incomplete file paths for Bash.
66
# Source this file from your ~/.bashrc and use `_bcpp --defaults`
@@ -146,7 +146,12 @@ _bcpp_complete() {
146146
then
147147
COMPREPLY=("$INPUT/")
148148
else
149-
COMPREPLY=($(compgen -o "$OPTION" "$INPUT"))
149+
readarray -t COMPREPLY < <(compgen -o "$OPTION" "$INPUT")
150+
local i
151+
for i in "${!COMPREPLY[@]}"
152+
do
153+
COMPREPLY[i]="$(printf "%q" "${COMPREPLY[i]}")"
154+
done
150155
fi
151156
return
152157
fi

tests/test_issue23.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
Edge case for directory names containing whitespace
33
44
https://github.com/sio/bash-complete-partial-path/issues/23
5+
https://asciinema.org/a/549127
56
'''
67

78
import pytest

0 commit comments

Comments
 (0)