I have recently installed a fresh version of Debian. I created this simple script:
#!/bin/bash
print_something () {
echo Hello I am a function
}
print_something
print_something
However this displays this error upon me issuing bash test.sh
:
test.sh: line 3: $'\r': command not found
test.sh: line 4: syntax error near unexpected token `$'{\r''
'est.sh: line 4: `print_something () {
What am I doing wrong? Many thanks!
Diagnosing:
\n
only, and usually break or show unexpected behavior and output with files that have Windows-style \r\n
line endings - as bash
does here.\r
are an indicator that such Windows-style line endings are present.cat -v
and examining the output for ^M
at the end of output lines (which is how \r
chars. are represented) is a way to check a file for Windows-style line endings on demand.Fixing:
dos2unix
utility, if already installed (typically, it is not), or installing it is an option; how you install it depends on your platform; e.g.:
sudo apt-get install dos2unix
brew install dos2unix
sed $'s/\r$//' win.txt > unix.txt
awk 'sub("\r$", "")+1' win.txt > unix.txt