Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

> There's no good way of telling if a builtin exists programatically (for, let's say, feature detection).

Doesn’t the `type` command (technically a builtin) meet your requirements?



It kinda does, and it is portable enough (modern bash zsh ksh dash busybox), thanks!

Of the mainstream packaged shells, it fails only on `posh`, but that's fine, posh is super austere, it also lacks `command`.

However, I will probably still use `command -v` for sporadic uses. That's because I don't need a subshell to capture the output:

    OLDPATH=$PATH;PATH=
    if command -v echo
    then echo is builtin
    fi
    PATH=$OLDPATH

    if test "$(type echo)" = "echo is a shell builtin"
    then echo is a builtin
    fi
`type` however seems to be able to pick up more stuff, so I might use it some cases like this:

    case $(type echo) in
        'echo is a shell builtin') do_something;;
        'echo is aliased'*) do_something_else;;
        'echo is /'*) do_something_else_entirely;;
    esac
It kinda fits an early feature detection that I could put in a more sensible place (my lib init) and pay the subshell cost only once.




Consider applying for YC's Winter 2027 batch! Applications are open till November 2.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: