Posts Carriage return chars in WSL environment variables
Post
Cancel

Carriage return chars in WSL environment variables

I’ve been following through some tutorials on Azure Container Registry in Anthony Nocentino’s Pluralsight course “Microsoft Azure Developer: Implement IaaS Solutions”. There is quite a lot of Azure CLI and bash in the examples provided for managing Azure Container Registry. This is good for me, as I am no where near as comfortable with Azure CLI and bash as I am with PowerShell. So, it was a good opportunity to fire up bash in Windows Subsystem for Linux and get some experience.

It kept hitting a problem when using environment variables to store outputs of one Azure CLI command. For example, trying to use ACR_LOGINSERVER as part of a docker tag command would throw an “is not a valid repository/tag: invalid reference format”.

1
2
3
4
5
dave@DLSURFACE:/mnt/c/git/containers$ ACR_NAME='dlacr1234'
dave@DLSURFACE:/mnt/c/git/containers$ ACR_LOGINSERVER=$(az acr show --name $ACR_NAME --query loginServer --output tsv)
dave@DLSURFACE:/mnt/c/git/containers$ docker tag webappimage:v1 $ACR_LOGINSERVER/webappimage:v1
Error parsing reference: "dlacr1234.azurecr.io\r/webappimage:v1" is not a valid repository/tag: invalid reference format
dave@DLSURFACE:/mnt/c/git/containers$

In the error message the tag “dlacr1234.azurecr.io\r/webappimage:v1” had an \r in it at the end of the ACR_LOGINSERVER. I worked around it by just manually putting in the same of my container registry and running it again.

1
dave@DLSURFACE:/mnt/c/git/containers$ docker tag webappimage:v1 $ACR_LOGINSERVER/webappimage:v1

I had a similar problem later on when working with an example on using Service Principals:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
dave@DLSURFACE:/mnt/c/git/containers$ ACR_NAME='dlacr1234'
dave@DLSURFACE:/mnt/c/git/containers$ ACR_REGISTRY_ID=$(az acr show --name $ACR_NAME --query id --output tsv)
dave@DLSURFACE:/mnt/c/git/containers$ SP_NAME=acr-service-principal
dave@DLSURFACE:/mnt/c/git/containers$ SP_PASSWD=$(az ad sp create-for-rbac \
  --name http://$ACR_NAME-pull \
  --scopes $ACR_REGISTRY_ID \
  --role acrpull \
  --query password \
  --output tsv)

WARNING: Creating 'acrpull' role assignment under scope '/subscriptions/1c88c87f-ef5b-4d10-ac41-eb7c5fdb759c/resourceGroups/rg-demo/providers/Microso't.ContainerRegistry/registries/dlacr1234
WARNING:   Role assignment creation failed.

WARNING:   role assignment response headers: {'Content-Type': 'text/html; charset=us-ascii', 'Server': 'Microsoft-HTTPAPI/2.0', 'Date': 'Wed, 29 Dec 2021 17:00:23 GMT', 'Connection': 'close', 'Content-Length': '324'}

ERROR: Operation failed with status: 'Bad Request'. Details: 400 Client Error: Bad Request for url: https://management.azure.com/subscriptions/1c88c87f-ef5b-4d10-ac41-eb7c5fdb759c/resourceGroups/rg-demo/providers/Microsoft.ContainerRegistry/registries/dlacr1234%0D/providers/Microsoft.Authorization/roleDefinitions?$filter=roleName%20eq%20%27acrpull%27&api-version=2018-01-01-preview

Again, this was throwing up an add character at the end of a string that had been stored in an environment variable, in this case “dlacr1234%0D”.

Carriage return \r being appended to the output https://github.com/Azure/azure-cli/issues/13573

I had never actually installed Azure CLI on my Windows Subsystem for Linux installation, so it was using the Windows version that it found at /mnt/c which was returning Windows style end of line/carriage returns, not Unix style! Ooops!! :D

The which command reveals my mistake:

1
2
dave@DLSURFACE:/mnt/c/git/containers$ which az
/mnt/c/Program Files (x86)/Microsoft SDKs/Azure/CLI2/wbin/az

Installed Azure CLI in my WSL instance: https://docs.microsoft.com/en-us/cli/azure/install-azure-cli-linux?pivots=apt

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
dave@DLSURFACE:/mnt/c/git/containers$ curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash
[sudo] password for dave:
Hit:1 http://archive.ubuntu.com/ubuntu focal InRelease
Hit:2 http://archive.ubuntu.com/ubuntu focal-updates InRelease
Hit:3 http://archive.ubuntu.com/ubuntu focal-backports InRelease
Hit:4 http://security.ubuntu.com/ubuntu focal-security InRelease
Reading package lists... Done
Reading package lists... Done
Building dependency tree
Reading state information... Done
lsb-release is already the newest version (11.1.0ubuntu2).
lsb-release set to manually installed.
curl is already the newest version (7.68.0-1ubuntu2.7).
curl set to manually installed.
gnupg is already the newest version (2.2.19-3ubuntu2.1).
gnupg set to manually installed.
apt-transport-https is already the newest version (2.0.6).
0 upgraded, 0 newly installed, 0 to remove and 43 not upgraded.
Hit:1 http://archive.ubuntu.com/ubuntu focal InRelease
Hit:2 http://archive.ubuntu.com/ubuntu focal-updates InRelease
Hit:3 http://archive.ubuntu.com/ubuntu focal-backports InRelease
Get:4 https://packages.microsoft.com/repos/azure-cli focal InRelease [10.4 kB]
Hit:5 http://security.ubuntu.com/ubuntu focal-security InRelease
Get:6 https://packages.microsoft.com/repos/azure-cli focal/main amd64 Packages [7761 B]
Fetched 18.2 kB in 0s (39.1 kB/s)
Reading package lists... Done
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following NEW packages will be installed:
  azure-cli
0 upgraded, 1 newly installed, 0 to remove and 43 not upgraded.
Need to get 67.0 MB of archives.
After this operation, 1035 MB of additional disk space will be used.
Get:1 https://packages.microsoft.com/repos/azure-cli focal/main amd64 azure-cli all 2.31.0-1~focal [67.0 MB]
Fetched 67.0 MB in 8s (8141 kB/s)
Selecting previously unselected package azure-cli.
(Reading database ... 32263 files and directories currently installed.)
Preparing to unpack .../azure-cli_2.31.0-1~focal_all.deb ...
Unpacking azure-cli (2.31.0-1~focal) ...
Setting up azure-cli (2.31.0-1~focal) ...
dave@DLSURFACE:/mnt/c/git/containers$ which az
/usr/bin/az

Exited and restarted bash and it worked fine!

References:

This post is licensed under CC BY 4.0 by the author.