Variables
Defining Variables
Section titled “Defining Variables”name = "Jake"version = "1.0.0"build_dir = "dist"Using Variables
Section titled “Using Variables”Use {{variable}} syntax in commands:
greeting = "Hello"target = "World"
task greet: echo "{{greeting}}, {{target}}!"Output: Hello, World!
Variable Scope
Section titled “Variable Scope”Variables are global and available to all recipes:
project = "myapp"
task build: echo "Building {{project}}"
task test: echo "Testing {{project}}"Two Variable Worlds: {{var}} vs $VAR
Section titled “Two Variable Worlds: {{var}} vs $VAR”Jake has two separate variable namespaces and they don’t override each other. This trips people up; understand it once and the rest is obvious.
| Syntax | Reads from |
|---|---|
{{name}} | Jakefile variables (name = "...") and recipe parameters |
$NAME | Shell environment (loaded by @dotenv, set by @export, inherited from the parent shell) |
{{...}} substitutions are resolved by Jake before the shell sees the
command. $NAME substitutions are resolved by the shell at command time.
Setting PORT = "3000" in the Jakefile does not put PORT in the
environment, and conversely @dotenv does not put loaded values into the
{{...}} namespace.
Precedence within each namespace
Section titled “Precedence within each namespace”For {{name}}:
- Recipe parameters passed at the command line (
jake build env=prod→{{env}}isprodfor that run) - Jakefile variables (
name = "..."assignments)
For $NAME:
- Variables set on the same shell command line (
PORT=9999 jake serve) @export VAR=valuedirectives.envvalues loaded by@dotenv- Variables inherited from the shell that started Jake
Worked example
Section titled “Worked example”@dotenv
PORT = "3000"
task serve: echo "via Jake namespace: {{PORT}}" # always prints "3000" echo "via shell env: $PORT" # prints whatever the env saysWith .env containing PORT=8080:
$ jake servevia Jake namespace: 3000via shell env: 8080If you want a single value to drive both, set it once and pick the right syntax
on the consuming side. To pass a Jakefile variable to a subprocess as an
environment variable, set it inline on the command — Jake expands the {{...}}
before the shell sees it:
APP_PORT = "3000"
task serve: PORT={{APP_PORT}} my-server@export VAR=value passes value through to subprocesses verbatim — {{...}}
inside an @export value is not expanded.
Environment Variables
Section titled “Environment Variables”Loading .env Files
Section titled “Loading .env Files”@dotenv # Load .env@dotenv ".env.local" # Load specific fileFiles are loaded in order; later files override earlier ones.
Exporting Variables
Section titled “Exporting Variables”@export NODE_ENV=production@export DEBUG=falseExported variables are passed to all subprocess commands.
Using in Commands
Section titled “Using in Commands”Use $VAR or ${VAR} syntax:
task show: echo "Node: $NODE_ENV" echo "Debug: ${DEBUG}".env File Format
Section titled “.env File Format”# Database settingsDATABASE_URL=postgres://localhost/myappDB_POOL_SIZE=10
# API Keys (use quotes for special chars)API_KEY="abc123!@#"
# Empty valuesEMPTY_VAR=
# Escape sequencesMULTILINE=line1\nline2WINDOWS_PATH=C:\\Users\\NameSupported escapes: \n, \t, \r, \\, \", \', \$
Requiring Variables
Section titled “Requiring Variables”Validate required environment variables:
@require API_KEY DATABASE_URL
task deploy: echo "Deploying with $API_KEY"Jake exits with an error if any required variable is missing.