Qvm-run - command failed with code: 2 (indention error)

I am experimenting with qvm-run. If am doing something like this:

if <some condition>; then
  qvm-run -u user disp4801 "
  echo hello
  echo hello2
  "
fi

, I’ll get

“command failed with code: 2”

Note the indention for the if statement and the " at its own line for code style aspects.

I need to change to

if <some condition>; then
  qvm-run -u user disp4801 "
  echo hello
  echo hello2"
fi

(" at end of existing line)

Whereas following works:

qvm-run -u user disp4801 "
echo hello
echo hello2
"

My reference comparison is bash, where indention with " in new line also works without problems:

  bash -c "
  echo hello
  echo hello2
  "

Background: I needed half an hour to find this tricky to debug error in my shell scripts.

What is the reason for qvm-run complaining?

You’ll have the same error with this command:

qvm-run -u user disp4801 "
echo hello
echo hello2
 "

Because it’ll execute space as a command at the end and it’ll return error code 2:

[user@dom0 ~]$  
[user@dom0 ~]$ echo $?
2
1 Like

Ah thanks!

Wouldn’t it then make sense to make qvm-run behave just like bash/sh and other shells, which don’t interpret trailing whitespace as new command?

Just somethink like a trim at the end.

You can open enhancement request here so that devs can consider it:

Crosslinked issue: https://github.com/QubesOS/qubes-issues/issues/7607

1 Like

qvm-run is meant to run a single command. If you want sh -c behavior, use sh -c as that single command, for example:
qvm-run -- <vmname> sh -c "whatever you want, possibly multiline".

3 Likes