Shell – Fish shell testing for existence of file in $PATH

fishshell

I'm trying to test in a fish shell script for the existence of the figlet binary. Since I use Linux and OS X I cannot rely on the file being in the same location and need to resolve it dynamically. I'm used to doing this with $(which) in bash, which works.

With fish though this does not work properly. Why?

function print_hostname --description 'print hostname'
  if test -x (which figlet)
    hostname | figlet
  end
end

Best Answer

Use type in fish like in Bourne-like shell:

if type -q figlet
  hostname | figlet
end

Or to limit to executables in $PATH (ignoring functions, builtins):

if command -s figlet > /dev/null
  hostname | figlet
end

See also Why not use “which”? What to use then?

Related Question