2

Problem

I may or may not have the executable thing in my $PATH.

How can I neatly check this in a ZSH script?

Existing Attempt

I just run the command, sending the output and errors or noman's land, then check the result code.

thing > /dev/null 2>&1
thing_installed=$?
if [ $thing_installed -eq 0 ]; then
    echo 'Thing Installed!'
fi

I feel like this could be done more neatly (one liner?).

5
  • from the man page on test ... -x file True if file exists and is executable. True indicates only that the execute flag is on. If file is a directory, true indicates that file can be searched. Commented Feb 15, 2014 at 19:17
  • Amusing that you accepted an answer that has a score of -4! Commented Feb 15, 2014 at 19:18
  • @devnull I accepted the answer which worked for me. It didn't have a -4 when I accepted it... Glad you got some amusement out of it though ;) Commented Feb 17, 2014 at 11:54
  • @PeterHamilton It's not a matter of being amused or not. It's sad to see unhelpful or wrong answers being accepted as those do not help any future visitors. Commented Feb 17, 2014 at 11:56
  • Which is why I changed it. Why do I feel like you're accusing me of something? :( Commented Feb 17, 2014 at 13:10

4 Answers 4

5

In zsh, which behaves reasonably, so you can simply do

if which thing > /dev/null 2>&1; then
    echo installed
fi

or

which thing > dev/null 2>&1 && echo installed

Note that which is a shell builtin, and its behavior is not reasonable in all shells, so this behavior cannot be relied upon.

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks, I tried this first, but the [ -e thing ] method with longform if was nicer IMO.
This more accurately answers your question, which specifically asked if thing was in your path.
"nicer" is an interesting definition for a solution that completely fails to work! Try test -e ls to answer the question 'is ls installed on my machine?'
@WilliamPursell That is the beauty of SO. You can have accepted answers that are completely wrong.
(It's nice to see an accepted answer with a score of -4 and counting!)
|
1

For zsh whence is the tool you are looking for. With parameter -p it can be forced to look only in $PATH and ignore functions, builtins and aliases with the same name.

This will either return the path to thing or an error message:

whence -cp thing

Comments

0

use which thing and check result code

Comments

-4

You can check whether the file exists by this expresion:

[ -e thing ] && echo 'Thing Installed!'

The -e operator checks whether the file exists or not. If you want to do other file tests take a look to this page: http://tldp.org/LDP/abs/html/fto.html.

Hope this was helpful ;-)

2 Comments

I didn't think this would work with executables, awesome, cheers.
[ -e thing] does neither check executabilty nor does it look in $PATH. For this to find anyhing thing has to be in the current directory and even then it may be a directory instead of an executable file.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.