Introduction

In this section, we’ll learn more about the basic constructs provided by Hush. That’ll enable you to implement Hush scripts using functions, data structures, and control flow.

Hello World

With Hush installed, we’re able to execute scripts. Here’s the traditional Hello world program in Hush:

  1. #!/usr/bin/env hush
  2. std.print("Hello world!")

The first line is a Shebang). It tells the operating system which interpreter to use for the script. The second line is a function call of the print function from the standard library. When executed, this script will output Hello world! to the standard output.

Making the script executable

To execute this script, save it to a file named hello-world.hsh, give it execution permission, and then run it:

  1. $ chmod +x hello-world.hsh
  2. $ ./hello-world.hsh
  3. Hello world!

Calling Hush directly

You can also execute a given script calling Hush with the script path as argument. When executing a script using this method, the Shebang is unnecessary, and will be disregarded as an ordinary comment.

  1. $ hush hello-world.hsh
  2. Hello world!

Alternatively, when given no aguments, Hush will start a non-interactive shell. You can go ahead and write your Hush script into the terminal. When you’re done, press Ctrl-D and Hush will execute everything you’ve typed. Ctrl-D sends a special End of File (EOF) signal to the Hush interpreter so that it knows where the script ends.

Tooling

Hush provides some tools for static analyses of scripts, which can be invoked by passing flags to the shell. The most useful one is the --check flag, which will check the script for syntax and semantic errors, without executing it.

Consider the following script.hsh, which attempts to use the undeclared variable value:

  1. value = "Hello world!"
  2. std.print(value)

We can check verify that the script has semantic errors:

  1. $ hush --check script.hsh
  2. Error: script.hsh (line 1, column 0) - undeclared variable 'value'
  3. Error: script.hsh (line 2, column 10) - undeclared variable 'value'

To get a list of other useful flags, run hush --help.