wofr06 / lesspipe

lesspipe - display more with less
GNU General Public License v2.0
484 stars 51 forks source link

Fix command execution when there are environment overrides #125

Closed tabulon closed 1 year ago

tabulon commented 1 year ago

ISSUE DESCRIPTION

lesspipe.sh builds the filter command (with its options/args) in a bash array (cmd) which then gets executed like below:

"${cmd[@]}"

This approach appears to work as long as there are no environment overrides, like so :

ls -lA -G ~/

But bash refuses such an invocation when there are preceding environment overrides, like so :

CLICOLOR_FORCE=1 ls -lA -G ~/

On my machine (macOS), lesspipe.sh was trying to invoke that very command when doing less ~/. Instead of a directory listing, I was getting an error from bash, along the lines of CLICOLOR_FORCE=1 : Command not found

Note that, though on macOS, I am running a relatively recent version of bash (5.x).

PROPOSED FIX

Just prepend the env command in this case, like so :

if [[ "$cmd" =~ '=' ]]; then   # first element contains an equal sign ?
  cmd=(env "${cmd[@]}")
fi

NOTE

Both the issue and the proposed fix may independently be checked in an interactive bash shell, as below :

$ cmd=(echo "Hello World")     ; "${cmd[@]}"   # OK
# Hello World

$ cmd=(SOME_VAR=1 echo "Hello World")       ; "${cmd[@]}"   # NOK, bash complains
# -bash: SOME_VAR=1: command not found

$ cmd=(env SOME_VAR=1 echo "Hello World") ; "${cmd[@]}"   # OK, fixed
# Hello World
wofr06 commented 1 year ago

Works in zsh as well, thanks.