Sample Header Ad - 728x90

Semicolon in conditional structures after the closing double bracket in a bash/zsh script?

2 votes
1 answer
133 views
Continuing https://unix.stackexchange.com/questions/48805/semicolon-in-conditional-structures (which handles single brackets), what's the point of having a semicolon after the closing DOUBLE bracket ]]? In my tests, running
#!/bin/zsh --
if [[ "a" == "a" ]] then
	echo "true"
else
	echo "false"
fi

if [[ "a" == "a" ]]; then
	echo "true"
else
	echo "false"
fi

if [[ "a" == "a" ]]
then
	echo "true"
else
	echo "false"
fi

if [[ "a" == "a" ]];
then
	echo "true"
else
	echo "false"
fi
yields
true
true
true
true
, and running
#!/bin/zsh --
if [[ "a" == "b" ]] then
	echo "true"
else
	echo "false"
fi

if [[ "a" == "b" ]]; then
	echo "true"
else
	echo "false"
fi

if [[ "a" == "b" ]]
then
	echo "true"
else
	echo "false"
fi

if [[ "a" == "b" ]];
then
	echo "true"
else
	echo "false"
fi
yields
false
false
false
false
No error is reported. In zsh, what's the difference between the conditionals with a semicolon ; after the closing double bracket and the conditionals without a semicolon after the closing double bracket? The same question goes for bash.
Asked by user743115 (1 rep)
Jul 26, 2025, 12:42 AM
Last activity: Jul 26, 2025, 06:12 PM