3

I need to execute the following command in WSL:

sudo curl -L "https://github.com/docker/compose/releases/download/1.23.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

In order to execute it from powershell, i tried to run:

Ubuntu1804 run "sudo curl -L 'https://github.com/docker/compose/releases/download/1.23.2/docker-compose-$(uname -s)-$(uname -m)' -o /usr/local/bin/docker-compose"

But errors occur as it cannot find the value of uname -s and uname -m

uname : The term 'uname' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling
of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:107
+ ... ocker/compose/releases/download/1.23.2/docker-compose-$(uname -s)-$(u ...
+                                                             ~~~~~
    + CategoryInfo          : ObjectNotFound: (uname:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

But the following command works as i manually entered the value of uname -s and uname -m in that command.

 Ubuntu1804 run "sudo curl -L 'https://github.com/docker/compose/releases/download/1.23.2/docker-compose-Linux-x86_64' -o /usr/local/bin/docker-compose"

Can anyone please help me to find when using powershell, how to incorporate the results of some commands to another command and execute it in WSL?

Also, how can i incorporate the value of environment variables like $USER in WSL commands and execute from powershell?

AnjK
  • 2,887
  • 7
  • 37
  • 64

2 Answers2

2

The problem is your subexpressions $( ) in the argument to the command. PowerShell interprets these in expression mode (because of the double-quotes which allow expansion) and is looking for an executable named uname before passing the argument to Ubuntu1804.

Solutions:

  • Use the stop-parser operator after run: --%

  • Flip your quotes so expansion doesn't happen: ... 'sudo curl -L " ...

  • Escape the subexpressions: `$`(`)

To answer how you include environment variables in the WSL command:

& Ubuntu1804.exe ... $Env:USER ...

about_Parsing

about_Environment_Variables

Maximilian Burszley
  • 18,243
  • 4
  • 34
  • 63
  • @Thelncorrigible1 Thanks for the answer! The first option to use stop-parser operator worked for me! – AnjK Mar 21 '19 at 05:22
  • But using the stop-parser operator is not working for a command that contain a pipeline character. As the official documentation says: **The stop-parsing symbol is effective only until the next newline or pipeline character. You cannot use a continuation character to extend its effect or use a command delimiter to terminate its effect.** In a command like `Ubuntu1804 run curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -` , `--%` can't affect the portion after `|`. But enclosing the entire command within double quotes works well! @TheIncorrigible1 – AnjK Mar 21 '19 at 05:39
  • Including environment variables in the WSL command by using '$Env:USER' not worked for me. I am adding what worked for me in this case as an answer here. – AnjK Mar 21 '19 at 06:07
  • @AnjanaDyna If you want to use `--%` AND reference environment variables, use the `%USER%` batch syntax (since that's what it's falling back to in this case) – Maximilian Burszley Mar 21 '19 at 15:04
0

For normal commands like sudo apt-get update -y just use stop-parsing operator as:

Ubuntu1804 run --% sudo apt-get update -y

For commands like curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - which includes pipeline characters (|) , using only --% will not work. In such cases enclosing the command within double quotes works as:

 Ubuntu1804 run --% "curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -"

Also, avoid using usage of backslash or new line in commands when using --% operator. For example, instead of using like this:

Ubuntu1804 run --% sudo apt-get install \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg-agent \
    software-properties-common

Execute like:

Ubuntu1804 run --% sudo apt-get install apt-transport-https ca-certificates curl gnupg-agent software-properties-common

Including environment variables in the WSL command by using like $Env:USER doesn't worked for the command sudo usermod -aG docker $USER.

It is an error if i use it like this:

 Ubuntu1804 run sudo usermod -aG docker $Env:USER

or like this:

  Ubuntu1804 run --% sudo usermod -aG docker $Env:USER

What worked for me to include the environment variables of WSL in powershell commands is using the stop-parsing operator --% like this:

 Ubuntu1804 run --% sudo usermod -aG docker $USER
AnjK
  • 2,887
  • 7
  • 37
  • 64