0

I am attempting to stuff a line s.a. echo $$ | command into an existing screen window.

e.g. screen -S session -X stuff "echo $$ | command\n"

However $$ appears to already have been evaluated once it is entered into the window.

e.g. echo 7589 | command

Where 7589 is $$ of the shell executing screen -S.

Is there a way to stop the shell from evaluating $$ prior to stuffing the string?

1 Answers1

1

Put single quotes around the whole thing, instead of double quotes. Double quotes still do expansion inside, single quotes don't.

BTW if you need to escape things in the middle of a string, you can break it like "foo "'something'" bar"

Adam D. Ruppe
  • 25,382
  • 4
  • 41
  • 60
  • See also http://stackoverflow.com/questions/10067266/when-to-wrap-quotes-around-a-variable – tripleee Aug 01 '15 at 17:33
  • 1
    ... Though with single quotes `\n` will also be literal. In Bash, C-style strings would be ideal, i.e. `$'echo $$ | command\n'` (notice the leading dollar sign). – tripleee Aug 01 '15 at 17:36
  • 1
    The `\n` is literal in double quotes too I believe (I'm actually on my Windows box right now...)... but if you need to, my second little comment there about breaking the string comes to the rescue - you can break out of the single when done and switch to double. By the way, I never knew about the `$'xxx'` syntax in bash! that's cool to know. – Adam D. Ruppe Aug 01 '15 at 17:42
  • Yeah, I guess that's true. I believe `screen` will want a literal newline but I can't check from here, either! – tripleee Aug 01 '15 at 17:46
  • c-style strings work, thanks! will this not work with other shells? I was hoping to be as compatible as possible with different shells. –  Aug 01 '15 at 18:08
  • `$'...'` is not POSIX, but supported in `bash`, `ksh`, and `zsh` at least (though not in `dash`). – chepner Aug 01 '15 at 18:10
  • Thanks guys! Much appreciated, simply single quoting appears to work for this particular purpose as well so I will stick to that for POSIX compliance. –  Aug 01 '15 at 19:36