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

Use type in fish like in Bourne-like shell:

if type -q figlet
  hostname | figlet

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

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

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

  • 1
    Thanks! I assume command -s figlet > /dev/nullcommand -s figlet > /dev/null is correct. – binaryanomaly Sep 15 '16 at 15:38
  • 2
    @binaryanomaly. Ah yes sorry, I assumed ^ would redirect both stdout and stderr, while it actually only redirects stderr. Here command doesn't seem to output anything on stderr, so > redirection should be enough indeed. You can add a ^&1 in case command might output something on stderr. – Stéphane Chazelas Sep 15 '16 at 16:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.