Search code examples
shellcommand-linecsh

how to find out if a shell command exists in cshell


I'm looking for a function that would return 1 if a shell command exists, and 0 otherwise I know there is the which command that returns the path of a command if it exists. It also says in the manual that this command is supposed to have a return value, but trying set a = `which some_command.bin` doesn't put any value into a. I know that I can always use which then parse the results, I'm just looking for a cleaner solution


Solution

  • The return value for a shell command is not usually retrieved like that. You generally need to run the command, then the special environment variable $? will give you the return value.

    See the following transcript for tcsh:

    pax$ which qq ; echo $?
    qq: Command not found.
    1
    
    pax$ which ls ; echo $?
    /bin/ls
    0
    

    Just put whatever command you want to check for where I have ls above.