简体   繁体   中英

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.

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"

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'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM