0

I'm installing Poetry in a dockerfile, but I want to do it under a different user (to play nicely with VSCode). I don't understand the behavior of the su command though.

When I run su vscode -c "echo $HOME" I get /root. However, when I run su vscode, and subsequently run echo $HOME, I get /home/vscode`.

Even stranger, when I run su vscode -c "echo $HOME && curl -sSL https://install.python-poetry.org | python3", I get /root as output of the first command, but poetry is installed to /home/vscode/.local/bin. I'm at a loss here... can someone shine some light on this?

  • We have a bunch of similar questions in the knowledge base from people trying to run commands over ssh, but using double quotes and so getting values expanded locally instead of remotely; it's all the same underlying issue (of content being expanded by your current shell when you want it to instead be expanded by a different shell that's started later). – Charles Duffy May 08 '22 at 18:21

1 Answers1

2

"echo $HOME" is evaluated by your current shell before su is executed. So su will only be passed as argument "echo /root" (already-evaluated). If you want the variable to be evaluated by the shell spawned by su, you need to escape it: 'echo $HOME'

See 2.2 Quoting in the POSIX specification

knittl
  • 246,190
  • 53
  • 318
  • 364