In the comments section of a recent post I found out that Windows PowerShell had been ported to Linux. Had no clue it was a thing.
Went looking and found this old article attempting to explain why they did it. Not remotely interested in giving up Bash for PowerShell, but I thought it was interesting enough to share. The article seems to be from 2016.
I have never been more tempted to check the NSFW box, but I’ll leave it open for now unless a mod complains. :-D
It is unnecessary. It’s only needed when you keep them on the same line. E.g.:
if [ "$variable" == "value" ]
then
echo "Condition is true"
fi
That ; can be used anywhere in bash or powershell for the same effect
I know, but not all languages require it.
For example, lua does the following:
if true then print("hello") end hello
but this also works:
if true then print("hello") hello
It sounds like the main point of confusion for you with semicolons, especially in bash and its if/then statements, isn’t about their general readability but more about their role in defining what counts as a complete statement or command, and when they are required versus optional.
You’re right that bash requires a semicolon (or a newline) after the if condition before the then keyword if they are on the same line. This is because then is considered a separate ‘command’ or keyword that follows the if condition and its associated
[ ]
or(( ))
test.A newline serves the same purpose as a semicolon.
In contrast, languages like Lua, Python, or PowerShell often have syntax where then (or its equivalent) is intrinsically linked to the if and doesn’t require a separator between the condition and the block opening keyword, even on the same line. They typically use newlines or specific block delimiters (like
end
in Lua, indentation in Python, or curly braces{}
in PowerShell) to define the scope of the if statement.While the semicolon’s general use is to put multiple commands on one line, its mandatory placement after the if condition before then in bash when on the same line is a specific syntactic requirement of bash to separate those two distinct logical parts of the if construct. Many other languages simply define if condition then block as a single syntactic unit, hence no semicolon is needed there.
I wouldn’t call it confusion, more just ick. I don’t really like it.
I can understand that. There are several expression ‘rules’ that don’t feel right to me.
As a wonder, what’s your ‘first’ language? Did you like it?
I’d guess that might influence your preferences.
I started with (iirc, in order) batch, bash, python, powershell, go, typescript, rust.
I’m not putting all the ‘markup’ languages in there.
I started with (in order, give or take one or two): python, javascript, lua, c, bash, powershell