Bash find version of an installed application but if none is found set variable to App Not Installed

Hello Forum,

I'm issuing a one line bash command to look for the version of an installed application and saving the result to a variable like so:

APP=application --version

But if the application is not installed I want to return to my variable that the Application is not installed. So I'm looking for a command something like the following:

APP=application --version &&  echo "Application is not installed";

Or perhaps a better way to find this would be to look at when the application was installed using:

rpm -aq --last | grep <application>

Where I would again want the result to show either the app installed version and date and if not found return the text "Application is not installed".

What is the correct syntax of this one line command to gather the information I need into my $APP variable?

Thank you.

Your commands won't work; you need at least e.g. "command substitution".
Try

APP=$(application --version 2>/dev/null || echo "not installed")

Be aware that not all applications / tools offer a --version option - their result may be misleading.

1 Like

Thanks Again RudiC for your help. I'll now be able to use the

2>/dev/null || echo "not installed"

in future one line command scripts.

Your solution worked perfectly! :b: