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?
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With