77

I am writing a Bash script to store the version number of Virtualbox, but cannot succeed.

My Script is:

#!/bin/bash
installed_virtualbox=$(virtualbox --version) # --version this is what I want to change

I tried

virtualbox --version
virtualbox -v
virtualbox -V
virtualbox --Version

all seems to open the virtualbox instead of output.
virtualbox --help gives the version number, but the output is too long to store in variable and use later.

4 Answers 4

130

To print or view the current VirtualBox version you should use below command:

vboxmanage --version

which will then print the current version as seen below;

enter image description here

Refer 8.2. Command overview at the official VirtualBox site for more.


To print the version using the script, see below:

#!/bin/bash
echo $(vboxmanage --version)

Hope it helps.

6
  • 1
    echo $(vboxmanage --version | head -n 1 | awk '{print $NF}') and echo $(vboxmanage --version) what is the difference? are not they same?
    – sourav c.
    Feb 13, 2014 at 14:41
  • @souravc -- In your comment here, you use --version, which outputs only one line and one field; so using head and awk to trim is redundant, and yes they output the same result. In your answer below, you use --help instead of --version, in which case head and awk trimming is indeed necessary... but you still get less-precise information, as the version found via --help is only #.#.# vs --version which yields #.#.#r# (version with revision info). Feb 6, 2015 at 17:57
  • @DreadPirateShawn you are correct now it is unnecessary. At that time vboxmanage used to come with some other info that is why I proposed that. You can see the edit, OP accepted my suggestion. Why would I make things complex:)
    – sourav c.
    Feb 6, 2015 at 18:40
  • @souravc -- Ahhh... funny how context makes things clearer. :-P So... excellent tip. :-) Feb 6, 2015 at 20:54
  • Since VBox 5.0, vbox-img --version can print version as well.
    – kenorb
    Aug 7, 2015 at 9:49
7

You can try this,

virtualbox --help | head -n 1 | awk '{print $NF}'

or

$ echo $(virtualbox --help | head -n 1 | awk '{print $NF}')
4.3.6

How it works

Step -1

virtualbox --help

gives you a long output saying many options etc. But its very first lines are like,

Oracle VM VirtualBox Manager 4.3.6
(C) 2005-2013 Oracle Corporation
All rights reserved.
...

Step -2

| head -n 1

| is called pipe. It has great application in command line. It passes the whole expression to the next command head. head -n 1 prints the first line only. see man head for more. At this stage output is only

Oracle VM VirtualBox Manager 4.3.6

Step -3

| awk '{print $NF}'

Again the remaining things are passed to awk. At this stage awk sees the whole line as combinations of few fields separated by space and prints only the last field of the above expression. So you get the version only. See man awk for more.

2
  • Would be great if you could breakdown the command and explain what each part does.
    – jobin
    Feb 13, 2014 at 14:11
  • @Jobin plz see my edit.
    – sourav c.
    Feb 13, 2014 at 14:34
2

you could know the version of package installed with dpkg and grep with piping

dpkg -l | grep virtualbox | awk '{print $3}'
1
  • What does each of these piped commands do, please be verbose.
    – jobin
    Feb 13, 2014 at 14:12
1
avi@avi-IdeaPad-Z500:~$ virtualbox --help
Oracle VM VirtualBox Manager 4.3.6

So run the below command,

virtualbox --help | awk '/Oracle/{ print $5 }'

Output:

4.3.6

awk '/Oracle/{ print $5 }'

Searches for the line which consists of the word Oracle .If yes then the command picks up the fifth column on that line and redirects it to standard output.If no such word was present on any lines,then it displays nothing.

You must log in to answer this question.