395

I have windows, using Cygwin, trying to set JAVA_HOME permanently through my .bashrc file.

.bashrc:

export PATH="$JAVA_HOME/bin:$PATH"  
export JAVA_HOME=$JAVA_HOME:"/cygdrive/c/Program Files (x86)/Java/jdk1.7.0_05"

.bash_profile:

if [ -f ~/.bashrc ]; then
   source ~/.bashrc
fi

running cygwin:

-bash: $'\377\376if': command not found
-bash: $'then\r': command not found
: No such file or directorysu//.bashrc
-bash: /cygdrive/c/Users/jhsu//.bash_profile: line 3: syntax error near unexpected token `fi'
-bash: /cygdrive/c/Users/jhsu//.bash_profile: line 3: `fi'

I am not sure if I took the commands from a tutorial that was meant for another system or if I am missing a step. Or whitespace is causing my commands not to run properly.

I've looked at multiple similar questions but I haven't found one where the question has my error exactly.


My home path:

$ echo $HOME
/cygdrive/c/Users/jhsu
$ echo ~
/cygdrive/c/Users/jhsu/

So I believe the files should be placed in the correct spot.

kenorb
  • 155,785
  • 88
  • 678
  • 743
Jasmine
  • 4,364
  • 3
  • 16
  • 16
  • What does `echo ~` give? – harpo Jul 23 '12 at 16:52
  • $ echo ~ /cygdrive/c/Users/jhsu/ – Jasmine Jul 23 '12 at 17:04
  • 8
    The issue is not specific to cygwin -- the same problem can happen if creating a script on a DOS machine and doing a binary transfer to a Linux system and running it there; I've removed the cygwin reference from the title so folks having this issue in other circumstances will be more likely to take the question (and its non-Cygwin-specific answers) seriously. – Charles Duffy May 12 '15 at 21:49
  • 2
    For background on this problem specifically in the context of Cygwin, it appears that around 2007 was when Cygwin started treating carriage returns in the Unix style - see https://www.neuron.yale.edu/phpBB/viewtopic.php?t=878. That forum post explains the igncr shell option discussed in several of the answers below. – Evan Donovan Jul 20 '18 at 05:19
  • on the command line, `export SHELLOPTS` and then `set -o igncr` got configure scripts running for me – DWR Apr 03 '21 at 22:56
  • I found that I was getting this issue after installing git for windows, and not selecting the last option regarding CGLF to not modify text files on checkout. when I reinstalled and selected this last option to not modify files on checkout, the problem disappeared for me in the bash shell. – openCivilisation Oct 13 '21 at 03:50
  • I have faced similar issue and was able to fix it by intellij -> open file -> at the bottom right, use Linux/Unix as line separator and then try again. – Anshul Singhal Mar 07 '22 at 10:38
  • @Jasmine : As you see from the error message, you have a _carriage return_ character in your file. If you google for _remove carriage returns bash_, you find several hits dealing with this problem. – user1934428 Oct 10 '22 at 07:47
  • @Jasmine : Of course there could be more wrong to it. The message complaining about `\377\376` suggests that you also have some weird UTF8 characters in your code. Therefore, after removing the carriage returns, I would have a close look at your file with a hex editor (or use `xxd` or `od` on the command line) to check whether the content is really what you believe it should be. – user1934428 Oct 10 '22 at 07:51

17 Answers17

449

When all else fails in Cygwin...

Try running the dos2unix command on the file in question.

It might help when you see error messages like this:

-bash: '\r': command not found

Windows style newline characters can cause issues in Cygwin.

The dos2unix command modifies newline characters so they are Unix / Cygwin compatible.

CAUTION: the dos2unix command modifies files in place, so take precaution if necessary.

If you need to keep the original file, you should back it up first.

Note for Mac users: The dos2unix command does not exist on Mac OS X.

Check out this answer for a variety of solutions using different tools.


There is also a unix2dos command that does the reverse:

It modifies Unix newline characters so they're compatible with Windows tools.

If you open a file with Notepad and all the lines run together, try unix2dos filename.

Community
  • 1
  • 1
jahroy
  • 22,322
  • 9
  • 59
  • 108
  • 1
    Beautiful, dos2unix fixed it, thanks. I also added `JAVA="$JAVA_HOME"/bin/java` as well and now it finds my Java! – Jasmine Jul 23 '12 at 17:26
  • 7
    Be careful with `dos2unix`; unlike most text filters, it overwrites its input file by default. – Keith Thompson Jul 23 '12 at 18:19
  • 6
    dos2unix made me kept my sanity. – Dreen Dec 07 '12 at 11:16
  • 1
    it was apparent to me that the windows newline is causing this trouble, but my question is, can I *make* cygwin *accept this newline* (windows) *too*? I'm confused because I git-cloned a repo in cygwin which contained .sh files but they came with \r\n endings... obviously either git translated the newlines even if I ran it from within cygwin, or the repo has no problem with bash shell script .sh files with windows newlines (or noone knows/cares that the repo contains bad files which is unlikely). I'm aware git is able to do newline conversion but I would not expect it on cygwin. – n611x007 Jun 10 '13 at 07:36
  • 1
    @naxa - Newlines were probably chosen based on the OS: Cygwin = Windows – jahroy Jun 20 '13 at 02:16
  • Bad developer for using Notepad.exe to edit your files. No donuts for you!. Windows, by default uses `\r\n` as line endings. If you use Windows tools like Notepad to create and edit files, you will keep running into this issue. Use professional development tools like Eclipse, Notepad++, or VIM, and set the default line ending of these tools to _Unix_. Not only will these tools solve the EOL problem, but provide other tools to help you write your Unix scripts/programs. – David W. Oct 22 '13 at 14:25
  • @DavidW - I definitely would **NEVER** use Notepad to write code (I hope nobody would). I've been using Vim for almost 20 years. I just included that tip to be thorough. – jahroy Oct 23 '13 at 02:33
  • @jahroy Didn't mean to diss you. Unfortunately, you're _(I hope nobody would)_ is far from true. I keep running into this issue over and over. I've even had developers _convert_ a shell script to `\r\n` line endings, just so they can edit it in Notepad. ("The entire file looked like one line, so I fixed it"). Grrrr... I've written a Subversion pre-commit hook to enforce Unix line encodings on these types of files. – David W. Oct 23 '13 at 16:26
  • This worked! I had an issue using a bash script created in notepad++ and i tried using the script gave me this error when i ran it on WSL. dos2unix made it work flawlessly – Divakar Rajesh Sep 27 '19 at 06:11
  • Helped flawlessly. – Nijat Mursali Oct 15 '19 at 22:16
  • To run on directory, this [post](https://stackoverflow.com/a/11929475/13642249) worked perfect too here – kyrlon Feb 24 '23 at 00:51
434

For those who don't have dos2unix installed (and don't want to install it):

Remove trailing \r character that causes this error:

sed -i 's/\r$//' filename


Explanation:

Option -i is for in-place editing, we delete the trailing \r directly in the input file. Thus be careful to type the pattern correctly.

Michel
  • 26,600
  • 6
  • 64
  • 69
  • 19
    this should be the accepted and top-most answer. hunting for some dos2unix commnd isn't the best option... – Lzh Aug 19 '16 at 13:02
  • I have been always using this solution on my cygwin. Works great! – Binita Bharati Nov 18 '16 at 06:16
  • 2
    How to do this on a whole directory with files? – Highmastdon Dec 12 '16 at 20:36
  • 9
    to do this for all files in current directory run
    `for i in *;do if [[ -f $i ]]; then sed -i 's/\r$//' "$i"; fi; done`
    – Sasha Bond Mar 02 '17 at 20:16
  • 2
    This helps in my case since I don't have sudo access to install 'dos2unix' – dex10 Feb 10 '20 at 13:35
  • Try the command without the `i` flag and you see what happens without modification of the file – Timo Nov 11 '20 at 14:18
  • It would be nice if sed could do it for all directories like: `sed -i 's/\r$//' dir_with_sub/*` – Timo Jan 25 '21 at 15:02
  • I have been using this since a long time when I move scripts from Windows to Linux. Works like a charm! Also this works if you put it in a jenkins pipeline before deploying scripts to different servers. – Prashanth Feb 01 '21 at 12:21
  • 1
    for macos - try sed -i "" 's/\r$//' filename. as described in:https://stackoverflow.com/a/28592391/7089048 – David Sep 08 '22 at 11:13
  • This works when I run it directly from the Cygwin command line, but not when I run it from a script. – AJM Oct 05 '22 at 17:32
  • This also works in my windows 11 cygwin installed – massquote Feb 09 '23 at 17:31
142

For WINDOWS (shell) users with Notepad++ (checked with v6.8.3) you can correct the specific file using the option

  • Edit -> EOL conversion -> Unix/OSX format

And save your file again.

Edit: still works in v7.5.1 (Aug 29 2017)

Edit: Jan 3, 2022. As VSCode is mentioned several times. Go to settings in VSCode and type files.eol in the search field and set to \n (Unix format). Note that this changes this setting for your user or workspace for all files and it may not be what you want. YMMV.

Ray Oei
  • 1,827
  • 1
  • 17
  • 21
39

I am using cygwin and Windows7, the trick was NOT to put the set -o igncr into your .bashrc but put the whole SHELLOPTS into you environment variables under Windows. (So nothing with unix / cygwin...) I think it does not work from .bashrc because "the drops is already sucked" as we would say in german. ;-) So my SHELLOPTS looks like this

braceexpand:emacs:hashall:histexpand:history:igncr:interactive-comments:monitor
Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
  • The reasons that SHELLOPTS needs to be set in an *Windows* environment variable instead of a *Cygwin* environment variable is because that variable is read only by bash (according to "man bash"). So it must be set before your shell starts - therefore it must go into a Windows environment variable. – svec Dec 29 '14 at 14:52
  • 4
    I would like to just clarify the answer for try-and-error users that could misunderstand it: only `igncr` is really needed in SHELLOPTS. – Martin D Jan 05 '16 at 12:16
  • This is the only solution that worked for me. I have a large base of scripts and running dos2unix on each one was not an option. – Bogatyr Apr 04 '19 at 01:33
38

SUBLIME TEXT

With sublime you just go to

View - > Line Endings -> (select)Unix

Then save the file. Will fix this issue.

Easy as that!

Alvaro Silvino
  • 9,441
  • 12
  • 52
  • 80
30

If you are using a recent Cygwin (e.g. 1.7), you can also start both your .bashrc and .bash_profile with the following line, on the first non commented line:

# ~/.bashrc: executed by bash(1) for non-login shells.
# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
# for examples
(set -o igncr) 2>/dev/null && set -o igncr; # this comment is needed

This will force bash to ignore carriage return (\r) characters used in Windows line separators.

See http://cygwin.com/ml/cygwin-announce/2010-08/msg00015.html.

  • this is what I was looking for but it didn't work out for me.. should this still work now, a year latr? – n611x007 Jun 10 '13 at 07:55
  • edit: `export SHELLOPTS` did the trick for me! but occasionally I still got "invalid option name" - why is that? – n611x007 Jun 10 '13 at 07:58
  • from here: http://stackoverflow.com/a/14607651/611007 – n611x007 Jun 10 '13 at 18:15
  • This is still working for me with the bash shipped with Cygwin, which implements this special option. –  Jun 11 '13 at 11:51
  • 2
    But to work, you need to put it at the top of all your scripts, not only the .bashrc and .bash_profile. –  Jun 11 '13 at 12:11
  • 2
    Using export SHELLOPTS will probably get you an "invalid option name" when you call /bin/sh (which some command like awk do implicitely) in your bash shell. sh understands SHELLOPTS, but does not understand igncr. –  Jun 11 '13 at 12:13
  • Put at the top of both `~/.bashrc` and `/etc/bash.bashrc` If I enter them manually like `(set -o igncr)` and `set -o igncr;` it still doesn't work. If I use the SHELLPOTS method it works, but I indeed get `invalid option name` here and there. I can't see what's wrong. – n611x007 Jun 11 '13 at 12:36
29

The error:

'\r': command not found

is caused by shell not able to recognise Windows-like CRLF line endings (0d 0a) as it expects only LF (0a).


Git

If you using Git on Windows, make sure you selected 'Checkout as-is' during setup. Then make sure that you run: git config --global core.autocrlf false, so Git will not perform any conversions when checking out or committing text files.


dos2unix

If you're not using Git, you simply need to convert these affected files/scripts back into Unix-like line endings (LF), either by:

dos2unix ~/.bashrc

Note: The dos2unix command is part of dos2unix package.

Ex/Vim editor + tr

If you've Vim installed, the following command should correct the files:

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

Useful alias: alias dos2unix="ex +'bufdo! %! tr -d \\\\r' -scxa".

tr

Here is the method by using tr:

cat ~/.bashrc | tr -d '\r' > ~/.bashrc.fixed && mv -v ~/.bashrc.fixed ~/.bashrc

or:

tr -d '\r' < filename > new_filename

Note: The \r is equivalent to \015.


sed

You can try the following command:

sed -i'.bak' s/\r//g ~/.bash*

recode

The following aliases can be useful (which replaces dos2unix command):

alias unix2dos='recode lat1:ibmpc'
alias dos2unix='recode ibmpc:lat1'

Source: Free Unix Tools (ssh, bash, etc) under Windows.


perl

The following perl command can convert the file from DOS into Unix format:

perl -p -i.bak -e 's/\015//g' ~/.bash*

Source: stripping the ^M.


tofrodos

On Linux, like Ubuntu which doesn’t come standard with either dos2unix or unix2dos, you can install tofrodos package (sudo apt-get install tofrodos), and define the following aliases:

alias dos2unix=’fromdos’
alias unix2dos=’todos’

Then use in the same syntax as above.


Vagrant

If you're using Vagrant VM and this happens for provisioning script, try setting binary option to true:

# 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"
end

See: Windows CRLF to Unix LF Issues in Vagrant.

kenorb
  • 155,785
  • 88
  • 678
  • 743
  • I have command not found ^M. How can I fix it? Or better: how can I search for this problem because ^ is cut by a search engine? Wait .. I searched for git command not found stripping m – Timo Jan 25 '21 at 14:25
  • Well done on providing such a comprehensive answer! – AJM Oct 06 '22 at 09:16
29

try execution the following command

vim .bashrc
:set ff=unix
:wq!
U2647
  • 450
  • 4
  • 10
20

You can also add the option -o igncr to the bash call, e.g.

bash -x -o igncr script.sh
user1010997
  • 523
  • 5
  • 13
  • What does this do, and Where can I get more documentation on this? Can I use this in linux? – KansaiRobot Jul 06 '21 at 02:09
  • 1
    This runs the bash script `script.sh` with the option `igncr` (= ignore carriage return (`\r`)). `-x` just enables tracing. In Linux, the `igncr` option does not seem to be available, it may be a Cygwin-only option. – user1010997 Oct 20 '21 at 11:05
  • bash: line 0: bash: igncr: invalid option name – Kappacake Sep 13 '22 at 09:58
  • As I already wrote, the option does not seem to be available in Linux versions of bash. In Cygwin bash (i.e. under Windows), it is available (and needed ...). – user1010997 Oct 20 '22 at 20:34
14

As per this gist, the solution is to create a ~/.bash_profile (in HOME directory) that contains:

export SHELLOPTS
set -o igncr
kenorb
  • 155,785
  • 88
  • 678
  • 743
Jesse Chisholm
  • 3,857
  • 1
  • 35
  • 29
  • 1
    This is the best answer here. Makes scripts less error prone cross platform and no conversions needed. – Glenn Nov 07 '18 at 17:24
  • That is the answer that actually works! **Windows 10 + CygWin64** I cloned a git and configure produces `'\r'-command not found` but the files don't contain any `'\r'`! This happens at the opening of a file and can not be prevented by deleting something that isn't there. – CarpeDiemKopi Aug 09 '19 at 11:55
9

May be you used notepad++ for creating/updating this file.

EOL(Edit->EOL Conversion) Conversion by default is Windows.

Change EOL Conversion in Notepad++

Edit -> EOL Conversion -> Unix (LF)
Faiz Ahmed
  • 396
  • 6
  • 13
3

I had the same problem. Solution: I edit the file with pspad editor, and give it a unix format (Menu - Format -> UNIX)

I believe you can set this format to your file with many other editors

israel
  • 31
  • 1
3

For the Emacs users out there:

  1. Open the file
  2. M-x set-buffer-file-coding-system
  3. Select "unix"

This will update the new characters in the file to be unix style. More info on "Newline Representation" in Emacs can be found here:

http://ergoemacs.org/emacs/emacs_line_ending_char.html

Note: The above steps could be made into an Emacs script if one preferred to execute this from the command line.

Community
  • 1
  • 1
user2548343
  • 731
  • 5
  • 12
2

Issue maybe occured because of the file/script created/downloaded from a windows machine. Please try converting into linux file format.

dos2unix ./script_name.sh

or

dos2unix ~/.bashrc
Walk
  • 1,531
  • 17
  • 21
2

If you have the vim package installed on your Cygwin install, you can use vim to fix this without find & replace. Start vim as follows: vim filename.sh (often it is aliased to vi also). Then, type :set fileformat=unix, then :wq (write & quit) to save your changes. (The : puts you in vim's edit mode.)

I recommend this over dos2unix since vim is probably more commonly installed.

However, it is probably a best practice to set your text editor to save files that you plan to use in a Unix/Linux environment to have a Unix text format. The answers given above for Notepad++ are a good example.

Additional note: If you are unsure what type a file is (DOS or Unix), you may use the file filename.sh. This can especially help in debugging more obscure issues (such as encoding issues when importing SQL dumps that come from Windows).

For other options on how to modify text file formatting, see this IU knowledge base article

More background information on Bash scripts and line endings is found on this StackOverflow question.

Evan Donovan
  • 748
  • 9
  • 18
1

In EditPlus you do this from the Document → File Format (CR/LF) → Change File Format... menu and then choose the Unix / Mac OS X radio button.

Danny Schoemann
  • 1,270
  • 26
  • 41
-2

1. Choice

EditorConfig EditorConfig — is my choice.


2. Relevance

This answer is relevant for March 2018. In the future, the data from this answer may be obsolete.

Author of this answer personally used EditorConfig at March 2018.


3. Limitations

You need to use one of supported IDE/editors.


4. Argumentation

  1. Simply usage. I need set my .editorconfig file 1 time, where I create my project, → I can forget some platform-, style- and IDE-specific problems.
  2. Cross-platform, cross-languages, cross-IDE, cross-editors.

5. Example

5.1. Simple

I install EditorConfig plugin for Sublime Text → my text editor. I edit files in Sublime Text.

For example, I have sashacrlf.sh file:

echo "Sasha" &
echo "Goddess!" &

I run this file in Cygwin:

$ bash sashacrlf.sh
Sasha
sashacrlf.sh: line 1: $'\r': command not found
Goddess!

I create a file .editorconfig in same project as sashacrlf.sh.

[*.sh]
end_of_line = lf

It means, that if you save any file with .sh extension in your project in your editor, EditorConfig set UNIX line endings for this file.

I save sashacrlf.sh in my text editor again. I run sashacrlf.sh again:

$ bash sashacrlf.sh
Sasha
Goddess!

I can't get unexpected output in console.

5.2. Multiple file extensions

For example, I want to have UNIX line endings in all files with extensions .sh, .bashrc and .bash_profile. I add these lines to my .editorconfig file:

[*.{sh,bashrc,bash_profile}]
end_of_line = lf

Now, if I save any file with .sh, .bashrc or .bash_profile extension, EditorConfig automatically set UNIX line ending for this file.


6. Additional links

Саша Черных
  • 2,561
  • 4
  • 25
  • 71