Bash string variable mangling
2010-Oct-08, Friday 11:58 am![[personal profile]](https://www.dreamwidth.org/img/silk/identity/user.png)
Problem:
In a very long script I have to construct several paths for use. Problem is the paths are getting mangled. Most easily seen with echo.
For example:
Output:
Solution:
The end of the first variable must contain a 'return line' character, and is resetting the cursor before it continues, overwriting what's already there. Chances are this file (or one it reads the paths from) was created on Windows, which uses a different character to signal the end of a line.
To fix, run:
Props, and endless thanks to the poster of the solution here: http://ubuntuforums.org/showthread.php?t=1239902
In a very long script I have to construct several paths for use. Problem is the paths are getting mangled. Most easily seen with echo.
For example:
var = /opt/big_program
new_var = ${var}/sub_folder
echo "Look at what happens: ${new_var}"
Output:
/sub_foldert happens: /opt/big_program
Solution:
The end of the first variable must contain a 'return line' character, and is resetting the cursor before it continues, overwriting what's already there. Chances are this file (or one it reads the paths from) was created on Windows, which uses a different character to signal the end of a line.
To fix, run:
sed -i 's/\r//' $file_name
Props, and endless thanks to the poster of the solution here: http://ubuntuforums.org/showthread.php?t=1239902