Windows 自带的是 PowerShell 5.1,很多 Bash 的写法在这里不能用。
连着执行几条命令#
5.1 不支持 && 和 ||,用分号隔开,再看 $LASTEXITCODE 判断上一条成功没有:
npm run build; if ($LASTEXITCODE -ne 0) { Write-Host "构建失败" }curl 不是真的 curl#
在 PowerShell 里 curl 是 Invoke-WebRequest 的别名,参数完全不同。要用真正的 curl 写 curl.exe:
curl.exe -I https://aidao.cc中文乱码#
重定向到文件时,5.1 默认写成 UTF-16。要 UTF-8 就这样写:
Get-Content .\a.txt | Out-File .\b.txt -Encoding utf8常用对照#
| 想做的事 | Bash | PowerShell |
|---|---|---|
| 列文件 | ls -la | Get-ChildItem -Force |
| 看文件 | cat a.txt | Get-Content a.txt |
| 找文字 | grep foo a.txt | Select-String foo a.txt |
| 环境变量 | $HOME | $env:USERPROFILE |
| 删文件夹 | rm -rf dir | Remove-Item dir -Recurse -Force |