this post was submitted on 29 May 2025
138 points (97.3% liked)

Linux

7654 readers
295 users here now

A community for everything relating to the GNU/Linux operating system

Also check out:

Original icon base courtesy of lewing@isc.tamu.edu and The GIMP

founded 2 years ago
MODERATORS
 

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

you are viewing a single comment's thread
view the rest of the comments
[–] Anomalocaris@lemm.ee 1 points 1 week ago (2 children)

mind telling me why?

i thought it's just another terminal.

[–] olafurp@lemmy.world 2 points 1 week ago

I can maybe chime in since I've used both. Basic operations like if statements, arithmetic and loops are a lot closer to what you'd expect on PS. The barrier to programming in bash vs PS is IMO a bit higher because bash heavily uses symbols for everything. This does make PS way more verbose but more easy to wrap your head around it when unfamiliar with the syntax.

I prefer bash but for anything bigger than 5 lines I prefer proper scripting language like python or js and making an alias for "node path/to/js/script.js" and using execSync("program param1 param2") to run shell commands.

Long story short, I prefer bash because it's built in and I know it better than PS, I expect PS guys to feel the same way.

[–] Irelephant@lemm.ee 1 points 1 week ago (3 children)

Its a completely different shell, not just another terminal emulator.

Its more readable, and its syntax is less arcane than bash.

For example, a script to get the first line of a file and its lenght in bash is:

#!/bin/bash


if [ "$#" -ne 1 ]; then
    echo "Usage: $0 filename"
    exit 1
fi

filename="$1"


if [ ! -r "$filename" ]; then
    echo "File '$filename' does not exist or is not readable."
    exit 1
fi


read -r first_line < "$filename"


echo "First line: $first_line"


length=${#first_line}

echo "Length of first line: $length"

There is so much I hate about this, like using a semicolon after the if condition, and ending it in fi.

Versus the powershell version:

param (
    [Parameter(Mandatory = $true)]
    [string]$FilePath
)


if (-not (Test-Path -Path $FilePath)) {
    Write-Error "File '$FilePath' does not exist."
    exit 1
}

try {
    
    $firstLine = Get-Content -Path $FilePath -TotalCount 1
}
catch {
    Write-Error "Could not read from file '$FilePath'."
    exit 1
}


Write-Output "First line: $firstLine"


$lineLength = $firstLine.Length
Write-Output "Length of first line: $lineLength"

It feels more modern.

[–] drspod@lemmy.ml 22 points 1 week ago (3 children)

bash scripting is not intended to perform all of your logic in the scripting language, it's intended to call out to other programs which perform specific tasks. The entire POSIX command set is your bash scripting language.

Your script is a simple one-liner if you know some simple commands:

$ head -n 1 /usr/share/dict/words | tee /dev/stderr | tr -d '\n' | wc -c
A
1

[–] Anomalocaris@lemm.ee 2 points 6 days ago

Bash one liners are one is the most fun things in programming.

[–] FizzyOrange@programming.dev 7 points 1 week ago (1 children)

bash scripting is not intended to perform all of your logic in the scripting language

Maybe not all, but it's definitely intended to do some, and it's really bad at it.

[–] possiblylinux127@lemmy.zip 3 points 1 week ago (1 children)

It works fine for what it is. Bash is just a shell while Powershell is more of a scripting language.

I think a better comparison would be Python vs Powershell.

[–] FizzyOrange@programming.dev 5 points 1 week ago

It doesn't work fine for what it is. People use Bash for scripting all the time and it's full of footguns and gotchas. Powershell is just an attempt at a sane shell. It's not meant to be a full general purpose language like Python; it doesn't make sense to equate them.

Personally I don't really like the style of Powershell. The structured data is very obviously a good thing but I don't really like the syntax. Nushell seems a lot nicer IMO.

[–] Irelephant@lemm.ee 0 points 1 week ago* (last edited 1 week ago)

I can do that as well:

$l = Get-Content "example.txt" -TotalCount 1; Write-Output $l; ($l.TrimEnd("`r", "`n")).Length

There's a condensed version using aliases then:

$l = gc 'example.txt' -TotalCount 1; $l; ($l.TrimEnd("`r", "`n")).Length

I still think it has a better syntax than bash.

[–] Anomalocaris@lemm.ee 4 points 1 week ago* (last edited 1 week ago) (2 children)

oh, so it's it's own language too.

I only use it to access WSL (don't judge me)

[–] Irelephant@lemm.ee 4 points 1 week ago (1 children)

Did you mean WSL? I mostly use it for that too because lua development on windows is ass.

[–] Anomalocaris@lemm.ee 2 points 1 week ago
[–] possiblylinux127@lemmy.zip 2 points 1 week ago

If you are on Windows test out either Git bash or Msys2

Much better experience

[–] Mjpasta710@midwest.social 4 points 1 week ago* (last edited 1 week ago) (1 children)

That semicolon is the same use in both languages, why the hangup? It's a way to put separate commands on the same line.

PowerShell tried to build everything around the verb-noun command naming structure, which improves readability.

What'd the semicolon ever do to you?

[–] Irelephant@lemm.ee -2 points 1 week ago (1 children)

After the if condition. No other language does that, so it feels unecessary.

[–] Mjpasta710@midwest.social 4 points 1 week ago* (last edited 1 week ago) (1 children)

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

[–] Irelephant@lemm.ee 1 points 1 week ago* (last edited 1 week ago) (1 children)

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
[–] Mjpasta710@midwest.social 2 points 1 week ago (1 children)

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.

[–] Irelephant@lemm.ee 1 points 1 week ago (1 children)

I wouldn't call it confusion, more just ick. I don't really like it.

[–] Mjpasta710@midwest.social 2 points 1 week ago (1 children)

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.

[–] Irelephant@lemm.ee 1 points 1 week ago

I started with (in order, give or take one or two): python, javascript, lua, c, bash, powershell