0

I must be missing something here because no matter what bash compiler I use online and no matter what bash script I write I get the same Unexpected end of file error.

Here's my script

function hello { echo 'hello' } hello

I've also tried this

#!/bin/bash

hello_world () {
   echo 'hello, world'
}
jdoodle.sh: line 2: $'\r': command not found
jdoodle.sh: line 3: syntax error near unexpected token `$'{\r''
jdoodle.sh: line 3: `hello_world () {
'

And this

#!/bin/bash
f() { $branchName = "branch" echo $branchName}; f

hello_world

I'm using this tool:

https://www.jdoodle.com/test-bash-shell-script-online/

Why can't I write a simple bash script?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Jake12342134
  • 1,539
  • 1
  • 18
  • 45
  • `function hello { echo 'hello'; }; hello` – JUSHJUSH Aug 08 '19 at 14:41
  • 1
    The closing brace must be preceded by a newline or a semicolon, as `}` is only special in command position. (Otherwise, it's parsed as just another argument of the current command.) It's not clear why your second example would fail. – chepner Aug 08 '19 at 14:41
  • The second one fails because there are embedded `\r` carriage returns that you can't see. I've added jdoodle's error message to make the problem clear. – John Kugelman Aug 08 '19 at 14:47

1 Answers1

0

you are missing a ; after your echo command. i just ran this on git bash, recieved the same error then looked at some documentation. After adding the semicolon it functioned perfectly.

function hello { echo 'Hello World!'; }
hello
Xander Bielby
  • 161
  • 1
  • 12
  • Consider adding the reason for other two code segments as well, would answer the question completely. The second segment should work fine if ran as `dos2unix scriptname` and for 3rd one `f() { branchName="branch" ; echo $branchName; } ; f`. – Mihir Luthra Aug 08 '19 at 14:48