0

So I've activated and installed Bash for ubuntu for windows, great!

Now I'm trying to write a simple shell script, but I get stuck. This simple example script from here below runs fine on my linux server, but throws an error in bash on windows:

#!/bin/sh

# Define your function here
Hello () {
   echo "Hello World $1 $2"
   return 10
}

# Invoke your function
Hello Zara Ali

# Capture value returnd by last command
ret=$?

echo "Return value is $ret"

output:

rene@laptop:/mnt/f/xampp/htdocs$ sh test.sh
: not foundite.sh: 2: test.sh:
Hello World

So a warning and no correct output. Why? Any settings to configure in bash for windows to make this work? I havnt changed anything yet after installing bash in windows.

  • Cannot reproduce, for me it only shows Hello World Zara Ali\nReturn value is 10 without any problems – Sami Kuhmonen Aug 9 '16 at 16:54
1

From testing it seems this is because your script was created using windows line endings (CRLF) rather than Unix style line endings (LF), probably because you used a Windows based editor. I encountered the same issue since I was using SublimeText which defaults to 'system' line endings (CRLF) on Windows.

To verify just copy your script and create the file using an included editor such as vi in the Ubuntu on Windows bash shell, configure your editor to use Unix line endings, or convert your existing file to Unix line endings.

  1. vi test.sh
  2. Paste your code
  3. Save the file wq
  4. ./test.sh produces the expected results
  • thanks! yes this was indeed the problem. As a beginner I used nano but that worked just fine aswell. I tried this before by copy pasting directly from a windows editor and that didnt work. Now that I copied from the browser it did. Either way, I now the solution now, tnx – Flion Aug 12 '16 at 8:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.