Skip to content
Latchkey

GitHub Actions run step "syntax error near unexpected token"

Bash parsed a multiline run block and hit malformed syntax - an unbalanced quote, a heredoc whose terminator is indented, or YAML folding that joined lines unexpectedly. The script never reaches its real work.

What this error means

A multiline run step aborts during parsing with "syntax error near unexpected token" referencing a line in your script.

github-actions
/usr/bin/bash: line 6: syntax error near unexpected token `fi'
##[error]Process completed with exit code 2.

Common causes

Block scalar style folded your newlines

A "run: >" folded scalar collapses newlines into spaces, merging shell statements and breaking if/fi or loop structure. Multiline scripts need "run: |".

Heredoc terminator is indented

A plain "<< EOF" requires the closing EOF at column 0. YAML indentation pushes it right, so the heredoc never closes and the parser fails.

Unbalanced quote inside the block

A stray single or double quote leaves the rest of the script inside a string, so a later token appears unexpected.

How to fix it

Use the literal block scalar and a dash-heredoc

  1. Write multiline scripts with "run: |" so newlines are preserved verbatim.
  2. Use "<<-EOF" with a tab-indented terminator, or keep the heredoc body and terminator at the script indentation.
  3. Re-run; bash now sees well-formed structure.
.github/workflows/ci.yml
- run: |
    if [ -f .env ]; then
      cat <<'EOF' >> .env
    FEATURE_FLAG=on
    EOF
    fi

How to prevent it

  • Default to "run: |" for anything longer than a single command.
  • Run "bash -n script.sh" locally to syntax-check before committing.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →