To execute a command in a detached screen I can do this (after creating a screen, screen -dmS myscreen bash
):
screen -S myscreen -X stuff $'echo hello\n'
However this syntax messes with $
for environment variables:
MSG="hello"
screen -S myscreen -X stuff $'echo $MSG\n' #doesn't work
What can I do instead?
Use a double-quoted string for the part that needs to expand a variable, and a dollar-quoted string for the part that contains the escape sequence, and concatenate them.
screen -S myscreen -X stuff "echo $MSG"$'\n'
Another option is to assign the newline string to a variable:
NL=$'\n'
screen -S myscreen -X stuff "echo $MSG$NL"
BTW, MSG
is not an environment variable, it's just a shell variable. It doesn't become an environment variable unless you export
it.