This question already has an answer here:

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!

marked as duplicate by David Foerster, edwinksl, Anwar, Arronical, Andrea Lazzarotto Sep 30 '16 at 11:40

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new 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

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
  • 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
  • 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 K Nov 9 '13 at 20:12

The error:

$'\r': command not found

means that your file (.bashrc) somehow consist Windows line endings (CRLF) and bash doesn't recognise them, so it's failing. Maybe you edited that file in Windows?

So you've to convert your affected file back to use Unix-style line endings from CRLF into LF.

This can be achieved by: dos2unix command, e.g.

dos2unix ~/.bashrc

If you don't have it, install via apt-get install tofrodos.

Otherwise if you've Vim installed, try:

ex +'%!tr -d "\r"' -scwq ~/.bashrc

for multiple files:

ex +'bufdo!%!tr -d \r' -scxa ~/.bash*

Note: The :bufdo command is not POSIX.

If you're using Vagrantfile by any chance and this happens as part of the provisioning script, you can set binary to true for your shell, e.g.

# Shell provisioner, see: https://www.vagrantup.com/docs/provisioning/shell.html
config.vm.provision "shell" do |s|
  s.binary = true # Replace Windows line endings with Unix line endings.
  s.path = "script.sh"

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.

  • 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

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