0

I'm using Bash on OS X. There are several commands that I want to determine if exist, but don't want to run them in case they do exist and have unwanted effects. For example, if I wanted to test if destroyPC were a command, but didn't want to test it by running it. Is there a simple way to do this, like isCommand destroyPC?

EDIT: This is already answered here. I was looking for "does this COMMAND exist" without thinking to look for "does this PROGRAM exist". Remember that commands are just programs.

gkeenley
  • 6,088
  • 8
  • 54
  • 129

2 Answers2

0

Try using command -v <your-command>. It will return error code 1 if the command isn't found and 0 if it is, so you can use it like this:

if ! command -v foo &> /dev/null
    #command does not exist
else
    #command exists
fi

Here, I'm using the &> /dev/null to silence the output.

PowerBall253
  • 11
  • 1
  • 2
0

Interesting Question Lets say we want to test the existence of 'ls' commands, this is what I would type on the command line

command -v ls

If the commands doesnt exist, there will be no output If it does(assuming bash), the output will be /bin/ls

Onke
  • 103
  • 1
  • 8