jozxyqk
jozxyqk

Reputation: 17266

Using environment variables in commands passed to screen

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?

Upvotes: 2

Views: 2067

Answers (2)

Barmar
Barmar

Reputation: 781310

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.

Upvotes: 3

jozxyqk
jozxyqk

Reputation: 17266

One alternative, though it doesn't seem like a clean example...

MSG="hello"
screen -S myscreen -X stuff "echo $MSG
"

Upvotes: 0

Related Questions