Here is my bash script that is sent a variable from php:

mv $STRING1 "/opt/lampp/htdocs/dev/newlocation/"

It, for some reason, places "\r" at the end of $STRING1 and can't find the file because it will be something of the form "my_picture.jpg\r"

Any help is appreciated!


Bash expects that end-of-line in a script is always and only a newline (\n) character, Unix-style, not a carriage return-newline combination (\r\n) like you normally see on Windows. Bash thinks the \r character is just an ordinary character at the end of the string. (Characters that follow a double quoted string are just concatenated onto the end.)

As Ignacio suggests, the solution is to fix your script to eliminate the \r characters. dos2unix is one way. Another would be to use tr -d '\r' < infile > outfile as a filter.


Pass the script through dos2unix to fix the newlines.

  • 2
    And then switch to a text editor that doesn't use DOS-style line endings. – Gordon Davisson Oct 18 '12 at 15:14

This is obviously for the purpose of other googlers like me, not the OP, seeing that it's been 5+ years. But this solution existed back then too.

You can try a bash substitution to strip the largest pattern from the end of the string which looks like ${varname%%pattern}, more here:


It should be faster than spawning another process like tr to handle it.


Here is a reference for the severely-limited bash pattern matching (it is not really regular expression, more of a 'like' or 'glob' expression). As to why the carriage return is in the control class and not the whitespace class, I'm not sure. It is considered white-space in other languages. Without enabling extended pattern matching via a "shopt -s extglob", there's no way to specify a simple construct like "this char repeated 0 or more times" in bash. I also found

printf "%q" "${VARNAME}"

.. in addition to set -vx to show/test for the carriage return.

I came across this problem when using cygwin bash against output from the standard (non-cygwin) version of python; I'm confused as to why I'm not seeing more on how to solve it.

  • it works when I get a tool's version from a docker image, great – BMW Aug 27 at 4:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.