Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash, ... check if a program is installed or not, using bash script [duplicate]

Tags:

bash

I am trying to create a little script that check if a program is installed or not. I am trying with tmux, ...

`tmux --help` | grep "tmux: command not found" &> /dev/null
if [ $? == 1 ]; then
    echo "tmux is not installed"
    exit
fi

After installation of tmux, I get:

usage: tmux [-2lquvV] [-c shell-command] [-f file] [-L socket-name]
            [-S socket-path] [command [flags]]
tmux is not installed

If a program is not installed, appair the string "tmux: command not found". This could explain why I grep output of tmux --help command. Is a correct way to check if tmux is installed or not?

The script alwais echoes "tmux is not installed". Even if I install tmux. What's wrong with it?

like image 819
sensorario Avatar asked Mar 17 '23 09:03

sensorario


1 Answers

You can use the command, type and hash built-in functions to test whether a given command is usable in the current shell session.

This will not tell you if it is available in some location not in the current PATH however.

You should avoid using which for this purpose (even though that is the default suggestion you will get from many people) because it isn't a standardized tool (not completely) and it is an external tool as compared to the above which are all built-in to the shell (and is thus more expensive of a check).

like image 61
Etan Reisner Avatar answered Apr 29 '23 23:04

Etan Reisner