Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

Over the last few days, I merged a home directory from a working install of 12.04 to an old home directory, I believe from an install of 10.04 (it was a bare driv)

I got the new drive to load and mount properly, but when I try and use the .bashrc entries from the 12.04 installation, things like the following alias:

alias banana='echo banana'

It produces the following errors:

-bash: $'\r': command not found
-bash: $'\r': command not found
-bash: $'\r': command not found
: invalid shell option name
-bash: $'\r': command not found
-bash: $'\r': command not found
: invalid shell option name
-bash: $'\r': command not found
-bash: $'\r': command not found
-bash: $'\r': command not found
-bash: $'\r': command not found
-bash: .bashrc: line 36: syntax error near unexpected token `$'in\r''
'bash: .bashrc: line 36: `case "$TERM" in

I can't find the text that is being referenced in that .bashrc file (it only works when I copy it from /etc/skel, and even then, after adding the alias and sourcing, then removing the alias and sourcing, it still produces the errors. The only way to get Bash working again is to re-copy the file from /etc/skel

It looks like I can't make any alterations to .bashrc without causing these errors. I suspect that it has something to do with readline or .inputrc, which were also having problems (though these seem to be resolved now). As of right now, there's no .inputrc in the /home/user directory.

Thanks for any help you can give!

share|improve this question
5  
What editor are you using to make your changes to the .bashrc file? it looks like the file is being re-saved with Windows-style line endings (CR-LF, or \r\n) instead of Unix-style (LF, i.e. plain \n). –  steeldriver Nov 8 '13 at 18:38
    
I am using Vim from within the terminal –  SkyCaptain Nov 8 '13 at 18:52

2 Answers 2

up vote 0 down vote accepted

Try this:

sed -i -e 's/\r$//' ~/.bashrc

This will remove Windows-style line endings (CR characters - \r). Now, if you source ~/.bashrc you shouldn't get any error.

Next, before to copy any content from other file, run the same command for that file:

sed -i -e 's/\r$//' /path/to/file_from_where_you_copy
share|improve this answer
    
Perfect! This solution works, thanks! Now I just have to find out where these line endings are coming from in the first place. –  SkyCaptain Nov 8 '13 at 19:24
    
If you have a ~/.vimrc file, open it and make sure that it does not include a set ff=dos assignment - if it does, you can explicitly set Unix endings with set ff=unix (although I think it should default to Unix-style on a *nix platform) –  steeldriver Nov 8 '13 at 19:29
    
Thanks, long time lurker, first time poster. –  SkyCaptain Nov 8 '13 at 20:03
1  
If you find yourself having to do this with any regularity, the dos2unix command is designed to perform the same translations as this sed expression with the cleaner syntax dos2unix <filename>. You may have to sudo apt-get install dos2unix first. –  Steven Kath Nov 9 '13 at 20:12

The file seems pretty messy. You may want to just remove the file and copying a new one from /etc/skel instead:

rm ~/.bashrc
cp /etc/skel/.bashrc ~/.bashrc

Then edit the file and make sure that is saved using the UNIX line ending.

share|improve this answer
    
OK, that seems to solve the problem if I: Copy the file from /etc/skel Edit the file with Vim Save, Source However, I am trying to copy the aliases over from another file opened on a local machine (in gVim), then pasted into the remote file (also in gVim). Adding, removing these lines causes the error to reoccur. I think I have the same problem when editing that file/cutting and pasting with Gedit. –  SkyCaptain Nov 8 '13 at 19:02

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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