0

Intro

I am trying to run the simplest of bash scripts on Ubuntu through WSL by calling WSL from PowerShell, passing the script as a parameter to the WSL command. My command for running WSL with the bash script is as follows

wsl ./helloworld.sh

The script helloworld.sh contains a very simple Hello World function, along with a call to that function to perform the functions actions

#!/bin/sh
Hello() { 
    echo "Hello World!" 
}
Hello

WSL is launched and the script is ran (at least I am pretty sure) however I get an error result

./<path_to_file>/helloworld.sh: 5: Syntax error: end of file unexpected (expecting "}")


Question

Is there some important knowledge that I am missing about Linux/Ubuntu and functions or how the Bash Shell executes the file line by line? I have been reading multiple Linux tutorials and guides (Bash Functions , Shell Script Functions , and More Bash Script Functions) as I am extremely new to Linux and would like to expand my knowledge, however from what I can tell all of these guides would lead me to believe that my script above should work. What is causing this script to not execute how I would like? (Echoing"Hello World!" to the console) I attached below an image of what a "working script" looks like from one of the guides I referenced if it helps.

Image from Guide that Shows a Working Function

Note: If this belongs in a different community, please let me know!

RankinJ
  • 99
  • 10
  • 1
    If you run `bash -x helloworld.sh`, exactly what is the output? (Importantly, does it have `\r`s in it? -- if so, this is a duplicate of [Are shell scripts sensitive to encodings and line endings?](https://stackoverflow.com/questions/39527571/are-shell-scripts-sensitive-to-encoding-and-line-endings)) – Charles Duffy Sep 01 '22 at 16:05
  • 1
    BTW, as an aside, putting `.sh` extensions on bash scripts is not considered good practice. (It's _common_ practice among people who don't know bash well, and deeply frowned on by people who do). In general, executables on UNIX don't have any extensions at all; and beyond that, a bash script and a sh script are two different things. See [Commandname extensions considered harmful](https://www.talisman.org/~erlkonig/documents/commandname-extensions-considered-harmful/), an essay which has long been linked by the #bash IRC channel [factoid on the topic](https://wooledge.org/~greybot/meta/.sh). – Charles Duffy Sep 01 '22 at 16:07
  • 1
    (The "Is there some important knowledge that I am missing...?" question is too broad to be on-topic here, but the "why doesn't my script work?" is narrow and specific enough to be allowed, as long as there's enough information for it to be answerable; those `bash -x` logs will help a lot on that count) – Charles Duffy Sep 01 '22 at 16:15
  • 1
    ...btw, if you're looking for reliable references on bash, I strongly recommend the [BashGuide](https://mywiki.wooledge.org/BashGuide), [BashFAQ](https://mywiki.wooledge.org/BashFAQ), [BashPitfalls page](https://mywiki.wooledge.org/BashPitfalls), and the [bash-hackers' wiki](https://wiki.bash-hackers.org/). [The tag wiki here](https://stackoverflow.com/tags/bash/info) is also a good starting point. Unfortunately, there are a lot of resources where people "teach" practices that aren't actually reliable, and some of those have a lot of Google juice – Charles Duffy Sep 01 '22 at 16:16
  • 1
    (`sh -x helloworld.sh` is actually closer to how the script is really run than `bash -x helloworld.sh`, but some versions of `sh` don't print xtrace logs in a fully unambiguous way given presence of nonprintable characters, so using bash instead is more reliable at detecting the specific problem that's likely to be at hand; that said, note in the future that the bash tag should be used only for scripts that start with `#!/bin/bash`, not for ones that start with `#!/bin/sh`; there's a separate sh tag for the latter) – Charles Duffy Sep 01 '22 at 16:19
  • @CharlesDuffy When running `bash -x hellowrold.sh` I still recieve a relatively similar error to the one mentioned in the question this time with different wording `syntax error: unexpected end of file` lol. While it does not exactly contain `\r` , the link to the duplicate question is a great resource for me, I did not find that question when searching. I can tell right away from your comments that I need to get a better understanding of bash and the workings of it. – RankinJ Sep 01 '22 at 17:03
  • @CharlesDuffy Thank you for providing those resources to me. I think that might have been what I had in mind when I had asked "Is there some important knowledge I am missing...?". In agreement with you there appears to be a lot of unskilled or inexperienced people making guides and tutorials on using Linux that don't lead to good practices. – RankinJ Sep 01 '22 at 17:06
  • @CharlesDuffy From the question you linked I was able to determine that I had the wrong "End of Line Sequence" that was causing my script to execute differently than what i expected. Once again I much appreciate the drop of knowledge you bestowed upon me, I look forward to developing my Linux skills and practices in the future with the assistance of the links you provided. Then I can stop mis-using bash/shell and stop using those dreaded extensions lol. Thanks Again! – RankinJ Sep 01 '22 at 17:12
  • 1
    @RankinJ Right - Did you create the script in a Windows application? In general, make sure you use Linux tools (or a Windows application that can output in the Linux/Unix-style line-endings) to create files/scripts that are intended for WSL/Linux. – NotTheDr01ds Sep 01 '22 at 17:15
  • 1
    I'm going to go ahead and mark this as a duplicate. @RankinJ Since you say that the linked question fixed your issue, would you click the option in the header to that effect so that the question gets closed out? Thanks! – NotTheDr01ds Sep 01 '22 at 17:16
  • @NotTheDr01ds Absolutely and yes I did create the script using a Windows Application. Should I be leaving my questions open and linked to duplicates or should I delete them? Maybe more of a meta question – RankinJ Sep 01 '22 at 17:25
  • @RankinJ Since there are no answers, it's okay to delete it. You shouldn't delete it if there are answers, though, as the system doesn't "like" that. But regardless, you can leave it as-is and the system will delete it automatically after a while (I think 30 days for questions with no upvotes, but also no downvotes). – NotTheDr01ds Sep 01 '22 at 17:39
  • 1
    Actually, it looks like [9 days](https://stackoverflow.com/help/roomba). – NotTheDr01ds Sep 01 '22 at 17:40
  • 2
    BTW, just to draw emphasis to something NotTheDr01ds said -- unlike questions closed other ways, questions closed as duplicates _can still get upvotes_. That's because asking a question in a different way than how it's been asked previously, so we can link those new terms/keywords to the existing question with its base of already-vetted answers helps the preexisting/canonical answer reach more people; so a "duplicate" question that uses different enough terms to let people search it is a valuable contribution to our knowledge base. – Charles Duffy Sep 01 '22 at 18:11
  • 1
    So don't feel like a close-as-duplicate vote means we don't value your question -- as long as you did the research to _try_ to find prior instances but failed because you're describing the problem in different terms, a duplicate question can still be something we appreciate and value. – Charles Duffy Sep 01 '22 at 18:12
  • 1
    @CharlesDuffy That, and heck, I've even bookmarked this one so I can refer back to that second comment (and the link) on extensions in command filenames. I think I'm going to have to read through it several times to digest it fully myself ;-). – NotTheDr01ds Sep 01 '22 at 18:47
  • Thank you fellas for your insight. Just want to make sure I am doing right by the community and not cluttering up the site with unwanted questions or responses. – RankinJ Sep 02 '22 at 16:30

0 Answers0