Peculiar variations in command results [duplicate] - bash

This question already has answers here:
running bash script in cygwin on windows 7 [duplicate]
(2 answers)
Are shell scripts sensitive to encoding and line endings?
(14 answers)
Closed 3 years ago.
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.

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.

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.

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.

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

SUBLIME TEXT
With sublime you just go to
View - > Line Endings -> (select)Unix
Then save the file. Will fix this issue.
Easy as that!

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.

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.

try execution the following command
vim .bashrc
:set ff=unix
:wq!

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

As per this gist, the solution is to create a ~/.bash_profile (in HOME directory) that contains:
export SHELLOPTS
set -o igncr

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)

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

For the Emacs users out there:
Open the file
M-x set-buffer-file-coding-system
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.

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

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.

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.

1. Choice
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
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.
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
EditorConfig official site.

Related

g++ reutrns error, while shell script looks fine [duplicate]

This question already has answers here:
running bash script in cygwin on windows 7 [duplicate]
(2 answers)
Are shell scripts sensitive to encoding and line endings?
(14 answers)
Closed 3 years ago.
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.
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.
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.
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.
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
SUBLIME TEXT
With sublime you just go to
View - > Line Endings -> (select)Unix
Then save the file. Will fix this issue.
Easy as that!
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.
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.
try execution the following command
vim .bashrc
:set ff=unix
:wq!
You can also add the option -o igncr to the bash call, e.g.
bash -x -o igncr script.sh
As per this gist, the solution is to create a ~/.bash_profile (in HOME directory) that contains:
export SHELLOPTS
set -o igncr
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)
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
For the Emacs users out there:
Open the file
M-x set-buffer-file-coding-system
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.
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
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.
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.
1. Choice
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
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.
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
EditorConfig official site.

No such file or directory errors [duplicate]

This question already has answers here:
running bash script in cygwin on windows 7 [duplicate]
(2 answers)
Are shell scripts sensitive to encoding and line endings?
(14 answers)
Closed 3 years ago.
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.
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.
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.
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.
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
SUBLIME TEXT
With sublime you just go to
View - > Line Endings -> (select)Unix
Then save the file. Will fix this issue.
Easy as that!
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.
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.
try execution the following command
vim .bashrc
:set ff=unix
:wq!
You can also add the option -o igncr to the bash call, e.g.
bash -x -o igncr script.sh
As per this gist, the solution is to create a ~/.bash_profile (in HOME directory) that contains:
export SHELLOPTS
set -o igncr
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)
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
For the Emacs users out there:
Open the file
M-x set-buffer-file-coding-system
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.
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
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.
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.
1. Choice
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
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.
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
EditorConfig official site.

Shell script: using variables adds carriage return [duplicate]

This question already has answers here:
Are shell scripts sensitive to encoding and line endings?
(14 answers)
Closed 5 years ago.
I'm using this tutorial to learn bash scripts to automate a few
tasks for me.
I'm connecting to a server using putty.
The script, located in .../Documents/LOG, is:
#!/bin/bash
# My first script
echo "Hello World!"
And I executed the following for read/write/execute permissions
chmod 755 my_script
Then, when I enter ./my_script, I'm getting the error given in the
title.
Some similar questions wanted to see these, so I think they might
help :
$ which bash
/bin/bash
and
$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/bin/mh
I tried adding the current directory to PATH, but that doesn't
work …
Run following command in terminal
sed -i -e 's/\r$//' scriptname.sh
Then try
./scriptname.sh
It should work.
I have seen this issue when creating scripts in Windows env and then porting over to run on a Unix environment.
Try running dos2unix on the script:
http://dos2unix.sourceforge.net/
Or just rewrite the script in your Unix env using vi and test.
Unix uses different line endings so can't read the file you created on Windows. Hence it is seeing ^M as an illegal character.
If you want to write a file on Windows and then port over, make sure your editor is set to create files in UNIX format.
In notepad++ in the bottom right of the screen, it tells you the document format. By default, it will say Dos\Windows. To change it go to
settings->preferences
new document / default directory tab
select the format as unix and close
create a new document
If you use Sublime Text on Windows or Mac to edit your scripts:
Click on View > Line Endings > Unix and save the file again.
In notepad++ you can set it for the file specifically by pressing
Edit --> EOL Conversion --> UNIX/OSX Format
This is caused by editing file in windows and importing and executing in unix.
dos2unix -k -o filename should do the trick.
problem is with dos line ending. Following will convert it for unix
dos2unix file_name
NB: you may need to install dos2unix first with yum install dos2unix
another way to do it is using sed command to search and replace the dos line ending characters to unix format:
$sed -i -e 's/\r$//' your_script.sh
Your file has Windows line endings, which is confusing Linux.
Remove the spurious CR characters. You can do it with the following command:
$ sed -i -e 's/\r$//' setup.sh
For Eclipse users, you can either change the file encoding directly from the menu File > Convert Line Delimiters To > Unix (LF, \n, 0Α, ¶):
Or change the New text file line delimiter to Other: Unix on Window > Preferences > General > Workspace panel:
I was able to resolve the issue by opening the script in gedit and saving it with the proper Line Ending option:
File > Save As...
In the bottom left of the Save As prompt, there are drop-down menus for Character Encoding and Line Ending. Change the Line Ending from Windows to Unix/Linux then Save.
Atom has a built-in line ending selector package
More details here: https://github.com/atom/line-ending-selector
I develop on Windows and Mac/Linux at the same time and I avoid this ^M-error by simply running my scripts as I do in Windows:
$ php ./my_script
No need to change line endings.

Shell Script -- Unexpected End of File Error [duplicate]

This question already has answers here:
running bash script in cygwin on windows 7 [duplicate]
(2 answers)
Are shell scripts sensitive to encoding and line endings?
(14 answers)
Closed 3 years ago.
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.
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.
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.
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.
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
SUBLIME TEXT
With sublime you just go to
View - > Line Endings -> (select)Unix
Then save the file. Will fix this issue.
Easy as that!
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.
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.
try execution the following command
vim .bashrc
:set ff=unix
:wq!
You can also add the option -o igncr to the bash call, e.g.
bash -x -o igncr script.sh
As per this gist, the solution is to create a ~/.bash_profile (in HOME directory) that contains:
export SHELLOPTS
set -o igncr
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)
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
For the Emacs users out there:
Open the file
M-x set-buffer-file-coding-system
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.
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
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.
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.
1. Choice
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
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.
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
EditorConfig official site.

Bash script – "/bin/bash^M: bad interpreter: No such file or directory" [duplicate]

This question already has answers here:
Are shell scripts sensitive to encoding and line endings?
(14 answers)
Closed 5 years ago.
I'm using this tutorial to learn bash scripts to automate a few
tasks for me.
I'm connecting to a server using putty.
The script, located in .../Documents/LOG, is:
#!/bin/bash
# My first script
echo "Hello World!"
And I executed the following for read/write/execute permissions
chmod 755 my_script
Then, when I enter ./my_script, I'm getting the error given in the
title.
Some similar questions wanted to see these, so I think they might
help :
$ which bash
/bin/bash
and
$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/bin/mh
I tried adding the current directory to PATH, but that doesn't
work …
Run following command in terminal
sed -i -e 's/\r$//' scriptname.sh
Then try
./scriptname.sh
It should work.
I have seen this issue when creating scripts in Windows env and then porting over to run on a Unix environment.
Try running dos2unix on the script:
http://dos2unix.sourceforge.net/
Or just rewrite the script in your Unix env using vi and test.
Unix uses different line endings so can't read the file you created on Windows. Hence it is seeing ^M as an illegal character.
If you want to write a file on Windows and then port over, make sure your editor is set to create files in UNIX format.
In notepad++ in the bottom right of the screen, it tells you the document format. By default, it will say Dos\Windows. To change it go to
settings->preferences
new document / default directory tab
select the format as unix and close
create a new document
If you use Sublime Text on Windows or Mac to edit your scripts:
Click on View > Line Endings > Unix and save the file again.
In notepad++ you can set it for the file specifically by pressing
Edit --> EOL Conversion --> UNIX/OSX Format
This is caused by editing file in windows and importing and executing in unix.
dos2unix -k -o filename should do the trick.
problem is with dos line ending. Following will convert it for unix
dos2unix file_name
NB: you may need to install dos2unix first with yum install dos2unix
another way to do it is using sed command to search and replace the dos line ending characters to unix format:
$sed -i -e 's/\r$//' your_script.sh
Your file has Windows line endings, which is confusing Linux.
Remove the spurious CR characters. You can do it with the following command:
$ sed -i -e 's/\r$//' setup.sh
For Eclipse users, you can either change the file encoding directly from the menu File > Convert Line Delimiters To > Unix (LF, \n, 0Α, ¶):
Or change the New text file line delimiter to Other: Unix on Window > Preferences > General > Workspace panel:
I was able to resolve the issue by opening the script in gedit and saving it with the proper Line Ending option:
File > Save As...
In the bottom left of the Save As prompt, there are drop-down menus for Character Encoding and Line Ending. Change the Line Ending from Windows to Unix/Linux then Save.
Atom has a built-in line ending selector package
More details here: https://github.com/atom/line-ending-selector
I develop on Windows and Mac/Linux at the same time and I avoid this ^M-error by simply running my scripts as I do in Windows:
$ php ./my_script
No need to change line endings.

Resources