0

I am in the very basic of the basic with Shell. In general, I only use the WSL2 for ssh. Now, I write a loop, so I google for an example to see how it works. Here is my ref:https://ryanstutorials.net/bash-scripting-tutorial/bash-loops.php. The problem is that even if I just copy and paste their examples, I get the error:

loop_learning.sh: line 4: $'\r': command not found
loop_learning.sh: line 10: syntax error near unexpected token `done'
loop_learning.sh: line 10: `done'

This is the code that I just copy and paste:

#!/bin/bash
# Basic while loop
counter=1
while [ $counter -le 10 ]
do
echo $counter
((counter++))
done
echo All done
Piotr Godlewski
  • 378
  • 3
  • 10
Santi
  • 368
  • 2
  • 15

1 Answers1

1

The problem is probably that you copy-pasted the script from Windows and tried to execute it from Linux. Windows uses CR in addition to NL for new lines, where Linux uses only '\n' and finds the former ('\r') strange.

Try something like this from your WSL2 terminal:

sed 's/\r//g' your-copied-script.sh > your-clean-script.sh

And execute your-clean-script.sh

EDG956
  • 797
  • 8
  • 23
  • I tried writing on the terminal directly, but nothin: cnt=1 while [$cnt -lt 10] > do > echo $cnt > done [1: command not found – Santi Jan 19 '21 at 15:52
  • @Santi put some space between [ and $cnt. [ is a command (test command), but when the interpreter expands $cnt you end up with "[1", which is not recognized by the interpreter. – EDG956 Jan 19 '21 at 16:05