Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

Stackoverflow.com-r Command Not Found - Bashrc Bash_profile

The document discusses issues encountered when setting the JAVA_HOME environment variable in Cygwin on Windows, particularly related to Windows-style newline characters causing command errors in .bashrc and .bash_profile files. It suggests using the dos2unix command or other methods to convert files to Unix-compatible formats to resolve these errors. Various solutions and tools are provided for users to correct their scripts and avoid similar issues in the future.

Uploaded by

syb
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Stackoverflow.com-r Command Not Found - Bashrc Bash_profile

The document discusses issues encountered when setting the JAVA_HOME environment variable in Cygwin on Windows, particularly related to Windows-style newline characters causing command errors in .bashrc and .bash_profile files. It suggests using the dos2unix command or other methods to convert files to Unix-compatible formats to resolve these errors. Various solutions and tools are provided for users to correct their scripts and avoid similar issues in the future.

Uploaded by

syb
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

'\r': command not found - .bashrc / .

bash_profile
stackoverflow.com /questions/11616835/r-command-not-found-bashrc-bash-profile

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.

15 Answers

1/8
up vote 214 down vote When all else fails in Cygwin...
accepted
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 .

up vote 79 For those who don't have dos2unix installed (and don't want to install it):
down vote
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.

answered Oct 2 '15 at 17:42

Michael P. Bazos

11k22839

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

braceexpand:emacs:hashall:histexpand:history:igncr:interactive-comments:monitor

up vote 22 If you are using a recent Cygwin (e.g. 1.7), you can also start both your .bashrc and
down vote .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.

answered Jul 28 '12 at 11:02

Ael Ombreglace

84449

up vote 15 down vote -o


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

bash -x -o igncr
script.sh

answered Mar 18 '14 at 13:39

user1010997

34037

3/8
up vote 15 down vote SUBLIME TEXT

With sublime you just go to

View - > Line Endings -> (select)Unix

Then save the file. Will fix this issue.

Easy as that!

answered Sep 29 '15 at 13:36

Alvaro Joao

3,82741638

up vote 14 For WINDOWS users with Notepad++ (checked with v6.8.3) you can correct the specific file using
down vote the option - Edit -> EOL conversion -> Unix/OSX format

And save your file again.

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

up vote 3 down vote folks who use notepad++(6.8.1) to ship shell scripts from windows to linux.

set the following in notepad ++ Edit -> EOL Conversion -> Unix/OSX format

answered Nov 30 '15 at 2:59

viru

694

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

export
SHELLOPTS
set -o igncr

up The error:
vote 3
down
vote '\r': command not found

4/8
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
git config --global core.autocrlf
that you run: 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*

alias dos2unix="ex +'bufdo! %! tr -d \\\\r' -


Useful alias: 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
5/8
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
sudo apt-get install
tofrodos package ( 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.

6/8
up vote 2 down I had the same problem. Solution: I edit the file with pspad editor, and give it a unix format
vote (Menu - Format -> UNIX)

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

answered Feb 24 '15 at 9:08

israel

211

up vote 2
down vote
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.

answered Sep 21 '15 at 17:37

user2548343

22118

up vote 0 In EditPlus you do this from the


down vote Document → File Format (CR/LF) → Change File
Format... menu and then choose
Unix / Mac OS
the X radio button.

answered Nov 16 '16 at 12:57

Danny Schoemann

8691430

7/8
up vote 0 Issue maybe occured because of the file/script created/downloaded from a windows machine.
down vote Please try converting into linux file format.

dos2unix
./script_name.sh

or

dos2unix
~/.bashrc

answered May 25 at 7:17

Sonu

16310

Your Answer

Sign up or log in

Sign up using Google

Sign up using Facebook

Sign up using Email and Password

Post as a guest

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 bash shell
cygwin newline or ask your own question.

8/8

You might also like