I run a command from the host machine on the guest machine (both Ubuntu) as

VBoxManage guestcontrol Ubuntu1 run --exe "script.sh" --username xx --password xx --wait-stdout

where the shell script on the guest machine is as

echo $1

How can I pass the argument 1 while executing the shell script?

I assumed it should work as

run --exe "script.sh arg1"

but it does not.



We can run an application on a virtual guest with an SSH session from the host to this machine. However this requires that networking was enabled, and that openssh-server was installed an runs on the guest machine.

VBoxManage guestcontrol

As an alternative we can use built-in features of Virtual Box to execute a program on a running guest VM. This can be done with VBoxManage guestcontrol.

The example line below will just run ls on the virtual machine's root:

VBoxManage --nologo guestcontrol "<vm_name>" run --exe "/bin/ls" --username <guestuser> --password <password> --wait-stdout

Running a graphical application on the guest requires us to define the DISPLAY environment variable to the guest with the option --putenv. Next example will run and open gedit on the guest:

VBoxManage --nologo guestcontrol "<vm_name" run --exe "/usr/bin/gedit" --username <guestuser> --password <password> --putenv "DISPLAY=:0" --wait-stdout

We can also pass options to open a program. Next example will open a file vmtest in the guest gedit:

VBoxManage --nologo guestcontrol "vm_name" run --exe "/usr/bin/gedit" --username <guestuser> --password <password> --putenv "DISPLAY=:0" --wait-stdout -- gedit/arg0 vmtest

Options and arguments are separated from the command with -- as can best be seen in below example of a script from the host.

Example host script

Below script will play an example.ogg file using paplay in a guest machine when run on the host. Replace the variables with appropriate values.



VBoxManage --nologo guestcontrol $VM_NAME run --exe $VM_EXEC_PATH \
--username $VM_USER --password $VM_PASSWD --wait-stdout \
-- {$VM_EXEC}/arg0 $VM_ARGS
  • Sorry, I got lost. Where do you argv1 in guestcontrol command? – Googlebot Apr 23 '17 at 20:10
  • @All it is named arg0. In the gedit example line it is -- gedit/arg0 then vmtest to open a file vmtest in the guest's gedit, in the example script it is the variable VM_ARGS that plays an ogg file in the guest and from the guest hard drive. Remember to always give full paths. – Takkat Apr 23 '17 at 20:23
  • Wonderful! Works like a charm. – Googlebot Apr 23 '17 at 20:36
  • above and beyond, wish I had more than one vote to give. – C.J. Steele Jul 30 at 10:03
  • This is great! Thanks for the wonderfully detailed reply you put together! – Draque Thompson Oct 1 at 19:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.