bashvariablesgnu-screen

How to substitute shell variable into screen's "stuff" command in Bash?


I have script that needs to resume Virtualbox machines, and would like to use machine name as a variable so I have:

VMN="VMtest"
screen -S MyScr -p 3 -X stuff $'VBoxManage controlvm "${VMN}" resume --type headless\n'

but variable is not visible in this command. So I only see following command in screen window

[me@srv ~]$ VBoxManage controlvm resume --type headless

So I'm not sure if variable needs to be defined in that screen first or how to carry it inside single quotes.


Solution

  • screen's stuff understands \n so you can use double quotes for the command:

    VMN="VMtest"
    screen -S MyScr -p 3 -X stuff "VBoxManage controlvm $VMN resume --type headless\n"
    

    The following also works:

    # CTRL-J
    ... stuff "VBoxManage controlvm $VMN resume --type headless^J"
    # \ooo (octal)
    ... stuff "VBoxManage controlvm $VMN resume --type headless\012"