The core routine of GNU `which` command made of pure shell builtins, sh

PHOTO EMBED

Sat Jan 08 2022 07:33:03 GMT+0000 (UTC)

Saved by @juliyvchirkov #shell #sh #which

#!/usr/bin/env sh

which() {
    local path

    if [ $# -gt 0 ]; then
        for path in $(IFS=: && echo ${PATH}); do
            [ -x "${path}/${1}" ] && echo -n "${path}/${1}" && return 0
        done

        return 1
    else
        echo >&2 "Usage: which COMMAND"
        echo >&2 "Writes the full path of COMMAND to standard output"

        return 255
    fi
}
content_copyCOPY

Completely made of pure shell builtins to act for shell scripts as the resolver of their external dependencies whether external `which` command available or not

https://gist.github.com/juliyvchirkov/d2c7ff01846157f58b1fc1f3a3b1e36c