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

  • irelephant [he/him]🍭@lemm.ee
    link
    fedilink
    arrow-up
    10
    arrow-down
    8
    ·
    15 days ago

    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
      link
      fedilink
      arrow-up
      18
      arrow-down
      1
      ·
      15 days ago

      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
      
      
      • FizzyOrange@programming.dev
        link
        fedilink
        arrow-up
        8
        arrow-down
        1
        ·
        15 days ago

        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.

        • Possibly linux@lemmy.zip
          link
          fedilink
          English
          arrow-up
          4
          arrow-down
          1
          ·
          15 days ago

          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
            link
            fedilink
            arrow-up
            5
            ·
            15 days 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 [he/him]🍭@lemm.ee
        link
        fedilink
        arrow-up
        3
        arrow-down
        4
        ·
        edit-2
        15 days 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.