21 Mar 2023

Reason #1,287,529 WSL Still Vexes Me

Categories: Programming
Tags: Azure   Bash   WSL

WSL is sometimes a very useful tool for working in a Linux environment. It is super useful because it is light weight and integrated in Windows. The drawback to using WSL is from time to time you run across some inconsistencies that are endlessly frustrating before you can “bottom out” the problem. Once figuring it out, it is of course evident where the problem came from but just makes you shake your head and remind you that doing anything cross-platform in WSL is “mostly” cross platform but not always. So, what did I discover and how did we fix it.

My task was to put together a bash shell script that used the Azure CLI to work with Azure Managed Applications. To create some of the variables needed for the bash script we set up some variables that were az cli commands getting a single piece of data. Now please remember, I am by no means a Bash expert and know simply enough to be dangerous or at the very least get myself in trouble. Let’s take a look at WSL and the commands used to set up the variables

Image 1 - The problem

You can see that that I ran the data collection part of the script on the first line. It simply returns a string that is the subscription id and puts in the variable called appid (line 1). Next, I build a second variable called managedGroupId using that first variable essentially inserting it into the string represented there (line 3). You can see the results of echoing the variable managedGroupId. What in the Sam Hill is that output!?!?!? I kept thinking “well maybe because the command result is async”, “maybe it is over writing the buffer on timing”, etc….. As I said, I am no Bash expert so I was perplexed thinking there was some Bash assumptions that I was overlooking. It took me roughly 4 hours to figure out the problem. The problem was derived from one Stackoverflow thread (here for proper attribution). The problem is that since you are using WSL it appears that the windows command’s output (even though this is WSL) has a trailing (\r, 0x0D, ^M) character that makes the line fold back and continue at pos 1.

The way to fix the Bash script to overcome the WSL problem (and not torch your script when you run on Linux) is to pipe it into another command to remove the trailing like so:

tr -d $'\\r'

This produces the desired output like so:

Image 2 - The Solution

I hope this shows up on the internet and saves some time for everyone! 😊

Thank You For Reading
David White

Resident old man programmer. Get off my lawn!!!

comments powered by Disqus