From a80a3189458a86f29f61d980b4ad977594199864 Mon Sep 17 00:00:00 2001 From: Ayush Agarwal Date: Wed, 15 Sep 2021 22:47:27 +0530 Subject: Replace 'which' with POSIX equivalent 'command -v' The 'which' command is an external command that must be called each and every time pass is used. 'which' is also not mentioned in the README as one of the dependencies that might be needed to run pass. Instead of 'which', we can use the POSIX compatible and shell built-in 'command -v'. It saves pass from making an external call and is, arguably, more reliable than using 'which' as mentioned in the following link. --- src/completion/pass.bash-completion | 2 +- src/password-store.sh | 6 +++--- src/platform/darwin.sh | 2 +- tests/setup.sh | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/completion/pass.bash-completion b/src/completion/pass.bash-completion index 95d3e1e..2d23cbf 100644 --- a/src/completion/pass.bash-completion +++ b/src/completion/pass.bash-completion @@ -72,7 +72,7 @@ _pass_complete_folders () { _pass_complete_keys () { local GPG="gpg" - which gpg2 &>/dev/null && GPG="gpg2" + command -v gpg2 &>/dev/null && GPG="gpg2" local IFS=$'\n' # Extract names and email addresses from gpg --list-keys diff --git a/src/password-store.sh b/src/password-store.sh index 7c6ea35..f6c5328 100755 --- a/src/password-store.sh +++ b/src/password-store.sh @@ -9,7 +9,7 @@ set -o pipefail GPG_OPTS=( $PASSWORD_STORE_GPG_OPTS "--quiet" "--yes" "--compress-algo=none" "--no-encrypt-to" ) GPG="gpg" export GPG_TTY="${GPG_TTY:-$(tty 2>/dev/null)}" -which gpg2 &>/dev/null && GPG="gpg2" +command -v gpg2 &>/dev/null && GPG="gpg2" [[ -n $GPG_AGENT_INFO || $GPG == "gpg2" ]] && GPG_OPTS+=( "--batch" "--use-agent" ) PREFIX="${PASSWORD_STORE_DIR:-$HOME/.password-store}" @@ -155,7 +155,7 @@ check_sneaky_paths() { # clip() { - if [[ -n $WAYLAND_DISPLAY ]] && command -v wl-copy > /dev/null; then + if [[ -n $WAYLAND_DISPLAY ]] && command -v wl-copy &> /dev/null; then local copy_cmd=( wl-copy ) local paste_cmd=( wl-paste -n ) if [[ $X_SELECTION == primary ]]; then @@ -163,7 +163,7 @@ clip() { paste_cmd+=( --primary ) fi local display_name="$WAYLAND_DISPLAY" - elif [[ -n $DISPLAY ]] && command -v xclip > /dev/null; then + elif [[ -n $DISPLAY ]] && command -v xclip &> /dev/null; then local copy_cmd=( xclip -selection "$X_SELECTION" ) local paste_cmd=( xclip -o -selection "$X_SELECTION" ) local display_name="$DISPLAY" diff --git a/src/platform/darwin.sh b/src/platform/darwin.sh index f6cc471..9a1fda8 100644 --- a/src/platform/darwin.sh +++ b/src/platform/darwin.sh @@ -39,6 +39,6 @@ qrcode() { fi } -GETOPT="$({ test -x /usr/local/opt/gnu-getopt/bin/getopt && echo /usr/local/opt/gnu-getopt; } || brew --prefix gnu-getopt 2>/dev/null || { which port &>/dev/null && echo /opt/local; } || echo /usr/local)/bin/getopt" +GETOPT="$({ test -x /usr/local/opt/gnu-getopt/bin/getopt && echo /usr/local/opt/gnu-getopt; } || brew --prefix gnu-getopt 2>/dev/null || { command -v port &>/dev/null && echo /opt/local; } || echo /usr/local)/bin/getopt" SHRED="srm -f -z" BASE64="openssl base64" diff --git a/tests/setup.sh b/tests/setup.sh index 5d1e794..20dd7ea 100644 --- a/tests/setup.sh +++ b/tests/setup.sh @@ -52,7 +52,7 @@ fi export GNUPGHOME="$TEST_HOME/gnupg/" chmod 700 "$GNUPGHOME" GPG="gpg" -which gpg2 &>/dev/null && GPG="gpg2" +command -v gpg2 &>/dev/null && GPG="gpg2" # We don't want any currently running agent to conflict. unset GPG_AGENT_INFO -- cgit v1.2.3