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

Time:01-23

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.

CodePudding user response:

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"

CodePudding user response:

There is no parameter expansion taking place inside $' .... ', but you can always catenate the pieces into a single string:

"VBoxManage controlvm ${VMN} resume --type headless"$'\n'
  • Related