Simple solution to Unix variable processing ‘\r’ issue

Writing code across operating systems can cause annoyance at times. Take for example an issue I faced last week. I had a tight deadline and wanted to finish a task a bit early so that I had time to test things thoroughly before my demo. My code seemed to not work at a critical point where I was doing a ‘pull’ over Android Debug Bridge. The issue was that I was storing a path that I needed to ‘pull from’ into a variable. The variable was storing a ‘\r’ as that’s Unix’s line ending character. This was screwing up my script.

The solution? I added:
#!/bin/bash -x
at the beginning of my script and saw the issue in the verbose output and then I simply added this:
tr -d '\r'
to the ending of my variable to process out the ‘\r’

Problem solved!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.