Skip to content
Advertisement

Checking result of executed command in a Bash script

I want to run my VirtualBox after my I logged in. To do that, I have to check if it’s already running or not. If not, then start the VM.

My problem is that I can not put the result of VirtualBox command into a variable in Bash.

I’ve created a function to get the exit parameter, but I get a “Syntax error: Invalid parameter ‘|'” error message.

How can I make it work?

This is my Bash script:

JavaScript

Advertisement

Answer

In order to do what you want, you would have to use command substitution:

JavaScript

The caveat is that the instructions will be executed during the variable expansion (i.e. during the variable allocation).

If you want to execute your instructions only during your function, you could use a eval:

JavaScript

Note that eval brings a lot of issues.

Advertisement