v0.3.2

Documentation version

v0.2.4v0.3.2
en

Documentation language

en

Search documentation

ESC

Documentation search

What are you working on?

Find a guide, configuration option, action, or ESH topic.

Browse documentation
DocsYour first ESH script

Your first ESH script

An ESH script is a normal project file interpreted by the ESH runtime. Start by making the inputs, output location, and failure boundary visible. ESH defines its own script syntax and built-ins; external commands and operating-system behavior remain host-dependent.

Create and run a script

#!/usr/bin/env esh

set -e
TARGET="${1:-./dist}"

mkdir -p "$TARGET"
cargo build --release
cp ./target/release/app "$TARGET/app"
echo "Wrote $TARGET/app"

Save the script as scripts/package.esh, make it executable if your platform requires it, and run it with an explicit destination:

esh scripts/package.esh ./artifacts

esh -c 'echo hello' evaluates one command string. To parse a file and print its execution plan without running commands, use esh --dry-run scripts/package.esh. esh help json prints the runtime documentation for a built-in in the terminal.

Pass values deliberately

ESH preserves a scalar expansion as one argument. It does not split words or expand pathnames implicitly, so a value containing spaces stays intact. Quote values when you want to make that boundary clear.

DEST="release build files"
mkdir -p "$DEST"

if test -f ./config.yaml; then
  echo "Using $(yaml --get app.name config.yaml)"
else
  echo "Missing config.yaml" >&2
  exit 1
fi

Use $NAME or ${NAME} to expand a value. Parameter modifiers handle common defaults without separate branching:

FormMeaning
${PORT:-8080}Use PORT, or 8080 when it is unset or empty.
${TARGET:=./build}Set and use a default target.
${#NAME}Length of a scalar value.
$(command)Capture command output.
$(( expression ))Evaluate signed 64-bit arithmetic.

Work with script arguments

The script name is available as $0; the arguments that follow it are positional parameters. Functions receive their own positional parameters, so they can package a small operation without relying on global state.

announce() {
  local NAME="${1:-world}"
  echo "Hello, $NAME"
}

for NAME in "$@"; do
  announce "$NAME"
done

Use return to leave a function and exit to end the script. See the Language Reference for the exact status rules.

Next: work with project data using ESH's in-process tools.

Page source: book/esh/10.first-script.md