Can I use EK9 for scripting?

← Getting Started · Ref: Q19

EK9 supports script-like usage with a compile-and-run model.

Every EK9 file starts with #!ek9 as the first line. This shebang line follows Unix conventions for script identification.

Running EK9 programs:

  ek9 script.ek9                    Run the program
  ek9 script.ek9 arg1 arg2          Pass arguments
  ek9 -r SpecificProgram script.ek9 Run a named program

Program arguments are typed. Declare parameters and EK9 automatically converts command-line strings:

  defines program
    ProcessFile()
      -> filename as String
      ...
  Run with: ek9 script.ek9 mydata.txt

Supported argument types include String, Integer, Float, Boolean, Date, Time, DateTime, Duration, and more. EK9 handles the string-to-type conversion automatically.

EK9 compiles and runs in a single step by default. There is no separate interpreter mode. The compiler caches compiled output so subsequent runs are fast.

Direct execution as a script:

  chmod u+x script.ek9
  ./script.ek9 arg1 arg2

Because every .ek9 file starts with the #!ek9 shebang, making it executable with chmod lets you run it directly like a Bash or Python script. EK9 will compile and run it automatically.

Multiple related programs can live in one file, making it easy to group CLI utilities together.

See Q1 for a minimal program. See Q2 for compile and run commands. See Q17 for entry points and named programs.

Example

defines module qa.getting.started.scripting

  defines program

    Greeting()
      -> name as String
      stdout <- Stdout()
      stdout.println(`Hello, ${name}!`)

    AddNumbers()
      ->
        a as Integer
        b as Integer
      stdout <- Stdout()
      result <- a + b
      stdout.println(`Sum: ${result}`)

Common mistakes

E01072 — EK9 does not have a return statement. It was designed out of existence. Use return value declarations (<- rtn as Type) instead. Triggers E01072 — excluded keyword used. See ek9 -h E01072 for details.

Incorrect:

return `Hello, ${name}!`

Correct:

stdout.println(`Hello, ${name}!`)
Other ways to ask this
  • Can I run EK9 files as scripts?
  • How do I pass command-line arguments to an EK9 program?
  • What does the shebang line do in EK9?
  • How do I pass parameters to a program in EK9?
  • How do I get input from the command line?
  • How do I accept user input arguments?

Coming from another language?

Python: direct script execution with shebang, Bash: shell scripts, Ruby: ruby script.rb, Go: go run main.go. EK9: ek9 script.ek9 with typed arguments and automatic conversion. Unlike Python and Bash, EK9 compiles before running. Unlike Go, multiple programs per file.

Keywords: input, scripting, command-line, run, start, migrate, argc, beginner, pass, intro, first, program, parameters, shebang, arguments, script, execute, argv, cli