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.
/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
- Write multiline scripts with "run: |" so newlines are preserved verbatim.
- Use "<<-EOF" with a tab-indented terminator, or keep the heredoc body and terminator at the script indentation.
- Re-run; bash now sees well-formed structure.
- run: |
if [ -f .env ]; then
cat <<'EOF' >> .env
FEATURE_FLAG=on
EOF
fiHow to prevent it
- Default to "run: |" for anything longer than a single command.
- Run "bash -n script.sh" locally to syntax-check before committing.