4 ms·
>For example, if we wanted to get the first three files from ls I’d do something like `ls | head -n 3`. In Powershell, it’s `$(dir)[0..2]` since the dir command
by Arnavion 7y ago
>For example, if we wanted to get the first three files from ls I’d do something like `ls | head -n 3`. In Powershell, it’s `$(dir)[0..2]` since the dir command is returning an array which I can index into.
You could do that, but the way to write it in the bash way would be `dir | select -first 3`
>a quick Google told me to use `echo | openssl s_client -showcerts -servername joejag.com -connect joejag.com:443 2>/dev/null | openssl x509 -inform pem -noout -text` which works fine in Bash or Zsh. Still, in Powershell, it throws an error for some reason.
It won't throw an error (unless you're running it without a tty), but it will ask you to enter the parameter of `echo`. That's because `echo` aka `Write-Object`'s parameter is required, unlike bash's `echo`. `echo |` in bash is the same as `< /dev/null`, but that also doesn't work in pwsh because it doesn't support `<`. The pwsh way is to pipe `$null`, thus
$null | openssl s_client -showcerts -servername joejag.com -connect joejag.com:443 2>/dev/null | openssl x509 -inform pem -noout -text
- awinder 7y agoA) this is all good stuff, I want to get better with powershell in 2020 and just learned some things here B) I think the authors point was that a lot of their googling for how to do things in a shell assume posix shell. So copy paste from web doesn’t work as well for powershell, which becomes frustrating for new onboarders.
- jstanley 7y ago> You could do that, but the way to write it in the bash way would be `dir | select -first 3` I would have said "ls | head -n3" as well. I've never heard of "select". I just tried your suggestion and it doesn't appear to work: $ dir | select -first 3 bash: syntax error near unexpected token `3'
- athrun 7y agoIt's because it's a PowerShell syntax. What the parent meant was "here's how to write it in PowerShell so that it still has a 'unix' feel to it (ie: composing commands with pipes and option switches)".
- vic20forever 7y ago>You could do that, but the way to write it in the bash way would be `dir | select -first 3` Fyi, `dir | select -first 3` also works in PowerShell.
- athrun 7y agoYes, this is what the parent meant: it is valid PowerShell. It wouldn't work by default in Bash unless you had `dir` and `select` defined there somehow.
- jstanley 7y ago"dir" is commonly available on Linux and "select" is a bash built-in, which is probably why so many of us were confused by the comment :)
- smacktoward 7y agoThe echo problem is reflective of a larger, longer-term issue with PowerShell, that being that it sometimes aliases PowerShell commands to the names of equivalent Unix commands. That’s done to ease the transition into PS-land for Unix folks, of course. But unless the PowerShell command works exactly the same as the Unix command it’s aliased to — takes all the same arguments, returns all the same data structures and error codes, etc. — you inevitably run into cases where things break because echo or curl or whatever aren’t behaving the way your code expects them to.
- Arnavion 7y agoTrue. I used PS only on Windows for many years and got into the habit of writing `cat`. When I started using PS Core on Linux I had to unlearn that and start using `gc` instead, because `cat` there is the native `cat`