From 98b6dfd075c92a624ffad542a2bebb3d8ac98892 Mon Sep 17 00:00:00 2001 From: Christoph Anton Mitterer Date: Thu, 5 Oct 2023 15:43:05 +0200 Subject: [PATCH] =?UTF-8?q?[bash]=20use=20`command`=20to=20=E2=80=9Cprotec?= =?UTF-8?q?t=E2=80=9D=20further=20commands?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit causes all simple commands that are not built-ins or functions to be invoked via `command` in order to protect them from alias substitution or from accidentally taking functions of the same name. Signed-off-by: Christoph Anton Mitterer --- shell/completion.bash | 28 ++++++++++++++-------------- shell/key-bindings.bash | 16 ++++++++-------- 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/shell/completion.bash b/shell/completion.bash index a51713c6292..0ec93ea8c2e 100644 --- a/shell/completion.bash +++ b/shell/completion.bash @@ -17,7 +17,7 @@ if ! declare -F _fzf_compgen_path > /dev/null; then echo "$1" command find -L "$1" \ -name .git -prune -o -name .hg -prune -o -name .svn -prune -o \( -type d -o -type f -o -type l \) \ - -a -not -path "$1" -print 2> /dev/null | sed 's@^\./@@' + -a -not -path "$1" -print 2> /dev/null | command sed 's@^\./@@' } fi @@ -25,7 +25,7 @@ if ! declare -F _fzf_compgen_dir > /dev/null; then _fzf_compgen_dir() { command find -L "$1" \ -name .git -prune -o -name .hg -prune -o -name .svn -prune -o -type d \ - -a -not -path "$1" -print 2> /dev/null | sed 's@^\./@@' + -a -not -path "$1" -print 2> /dev/null | command sed 's@^\./@@' } fi @@ -39,10 +39,10 @@ __fzf_comprun() { _fzf_comprun "$@" elif [[ -n "${TMUX_PANE-}" ]] && { [[ "${FZF_TMUX:-0}" != 0 ]] || [[ -n "${FZF_TMUX_OPTS-}" ]]; }; then shift - fzf-tmux ${FZF_TMUX_OPTS:--d${FZF_TMUX_HEIGHT:-40%}} -- "$@" + command fzf-tmux ${FZF_TMUX_OPTS:--d${FZF_TMUX_HEIGHT:-40%}} -- "$@" else shift - fzf "$@" + command fzf "$@" fi } @@ -195,7 +195,7 @@ __fzf_generic_path_completion() { printf '\e[5n' return 0 fi - dir=$(dirname "$dir") + dir=$(command dirname "$dir") [[ "$dir" =~ /$ ]] || dir="$dir"/ done else @@ -229,8 +229,8 @@ _fzf_complete() { fi local cur selected trigger cmd post - post="$(caller 0 | awk '{print $2}')_post" - type -t "$post" > /dev/null 2>&1 || post=cat + post="$(caller 0 | command awk '{print $2}')_post" + type -t "$post" > /dev/null 2>&1 || post='command cat' cmd="${COMP_WORDS[0]//[^A-Za-z0-9_=]/_}" trigger=${FZF_COMPLETION_TRIGGER-'**'} @@ -238,7 +238,7 @@ _fzf_complete() { if [[ "$cur" == *"$trigger" ]] && [[ $cur != *'$('* ]] && [[ $cur != *'<('* ]] && [[ $cur != *'`'* ]]; then cur=${cur:0:${#cur}-${#trigger}} - selected=$(FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} --reverse --bind=ctrl-z:ignore ${FZF_DEFAULT_OPTS-} ${FZF_COMPLETION_OPTS-} $str_arg" __fzf_comprun "${rest[0]}" "${args[@]}" -q "$cur" | $post | tr '\n' ' ') + selected=$(FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} --reverse --bind=ctrl-z:ignore ${FZF_DEFAULT_OPTS-} ${FZF_COMPLETION_OPTS-} $str_arg" __fzf_comprun "${rest[0]}" "${args[@]}" -q "$cur" | $post | command tr '\n' ' ') selected=${selected% } # Strip trailing space not to repeat "-o nospace" if [[ -n "$selected" ]]; then COMPREPLY=("$selected") @@ -277,14 +277,14 @@ _fzf_proc_completion() { } _fzf_proc_completion_post() { - awk '{print $2}' + command awk '{print $2}' } __fzf_list_hosts() { - command cat <(command tail -n +1 ~/.ssh/config ~/.ssh/config.d/* /etc/ssh/ssh_config 2> /dev/null | command grep -i '^\s*host\(name\)\? ' | awk '{for (i = 2; i <= NF; i++) print $1 " " $i}' | command grep -v '[*?%]') \ - <(command grep -oE '^[[a-z0-9.,:-]+' ~/.ssh/known_hosts | tr ',' '\n' | tr -d '[' | awk '{ print $1 " " $1 }') \ + command cat <(command tail -n +1 ~/.ssh/config ~/.ssh/config.d/* /etc/ssh/ssh_config 2> /dev/null | command grep -i '^\s*host\(name\)\? ' | command awk '{for (i = 2; i <= NF; i++) print $1 " " $i}' | command grep -v '[*?%]') \ + <(command grep -oE '^[[a-z0-9.,:-]+' ~/.ssh/known_hosts | command tr ',' '\n' | command tr -d '[' | command awk '{ print $1 " " $1 }') \ <(command grep -v '^\s*\(#\|$\)' /etc/hosts | command grep -Fv '0.0.0.0') | - awk -v "user=$1" '{if (length($2) > 0) {print user $2}}' | sort -u + command awk -v "user=$1" '{if (length($2) > 0) {print user $2}}' | command sort -u } _fzf_host_completion() { @@ -311,13 +311,13 @@ _fzf_complete_ssh() { _fzf_var_completion() { _fzf_complete -m -- "$@" < <( - declare -xp | sed -En 's|^declare [^ ]+ ([^=]+).*|\1|p' + declare -xp | command sed -En 's|^declare [^ ]+ ([^=]+).*|\1|p' ) } _fzf_alias_completion() { _fzf_complete -m -- "$@" < <( - alias | sed -En 's|^alias ([^=]+).*|\1|p' + alias | command sed -En 's|^alias ([^=]+).*|\1|p' ) } diff --git a/shell/key-bindings.bash b/shell/key-bindings.bash index 8654a6f2c05..1f0d0b576a3 100644 --- a/shell/key-bindings.bash +++ b/shell/key-bindings.bash @@ -18,7 +18,7 @@ __fzf_select__() { cmd="${FZF_CTRL_T_COMMAND:-"command find -L . -mindepth 1 \\( -path '*/.*' -o -fstype 'sysfs' -o -fstype 'devfs' -o -fstype 'devtmpfs' -o -fstype 'proc' \\) -prune \ -o -type f -print \ -o -type d -print \ - -o -type l -print 2> /dev/null | cut -b3-"}" + -o -type l -print 2> /dev/null | command cut -b3-"}" opts="--height ${FZF_TMUX_HEIGHT:-40%} --bind=ctrl-z:ignore --reverse --scheme=path ${FZF_DEFAULT_OPTS-} ${FZF_CTRL_T_OPTS-} -m" eval "$cmd" | FZF_DEFAULT_OPTS="$opts" $(__fzfcmd) "$@" | @@ -31,7 +31,7 @@ if [[ $- =~ i ]]; then __fzfcmd() { [[ -n "${TMUX_PANE-}" ]] && { [[ "${FZF_TMUX:-0}" != 0 ]] || [[ -n "${FZF_TMUX_OPTS-}" ]]; } && - echo "fzf-tmux ${FZF_TMUX_OPTS:--d${FZF_TMUX_HEIGHT:-40%}} -- " || echo "fzf" + echo "command fzf-tmux ${FZF_TMUX_OPTS:--d${FZF_TMUX_HEIGHT:-40%}} -- " || echo "command fzf" } fzf-file-widget() { @@ -43,7 +43,7 @@ fzf-file-widget() { __fzf_cd__() { local cmd opts dir cmd="${FZF_ALT_C_COMMAND:-"command find -L . -mindepth 1 \\( -path '*/.*' -o -fstype 'sysfs' -o -fstype 'devfs' -o -fstype 'devtmpfs' -o -fstype 'proc' \\) -prune \ - -o -type d -print 2> /dev/null | cut -b3-"}" + -o -type d -print 2> /dev/null | command cut -b3-"}" opts="--height ${FZF_TMUX_HEIGHT:-40%} --bind=ctrl-z:ignore --reverse --scheme=path ${FZF_DEFAULT_OPTS-} ${FZF_ALT_C_OPTS-} +m" dir=$(set +o pipefail; eval "$cmd" | FZF_DEFAULT_OPTS="$opts" $(__fzfcmd)) && printf 'builtin cd -- %q' "$dir" } @@ -56,7 +56,7 @@ if command -v perl > /dev/null; then output=$( set +o pipefail builtin fc -lnr -2147483648 | - last_hist=$(HISTTIMEFORMAT='' builtin history 1) perl -n -l0 -e "$script" | + last_hist=$(HISTTIMEFORMAT='' builtin history 1) command perl -n -l0 -e "$script" | FZF_DEFAULT_OPTS="$opts" $(__fzfcmd) --query "$READLINE_LINE" ) || return READLINE_LINE=${output#*$'\t'} @@ -70,12 +70,12 @@ else # awk __fzf_history__() { local output opts script if [[ -z $__fzf_awk ]]; then - __fzf_awk=awk + __fzf_awk='command awk' # if installed, mawk is faster command -v mawk > /dev/null && - mawk --version | # at least 1.3.4 - awk 'NR == 1 { split($2, a, "."); v=(a[1]*1000000+ a[2]*1000+ a[3]*1); exit !(v >= 1003004) }' && - __fzf_awk=mawk + command mawk --version | # at least 1.3.4 + command awk 'NR == 1 { split($2, a, "."); v=(a[1]*1000000+ a[2]*1000+ a[3]*1); exit !(v >= 1003004) }' && + __fzf_awk='command mawk' fi opts="--height ${FZF_TMUX_HEIGHT:-40%} --bind=ctrl-z:ignore ${FZF_DEFAULT_OPTS-} -n2..,.. --scheme=history --bind=ctrl-r:toggle-sort ${FZF_CTRL_R_OPTS-} +m --read0" [[ $(HISTTIMEFORMAT='' builtin history 1) =~ [[:digit:]]+ ]] # how many history entries