Skip to content

Instantly share code, notes, and snippets.

@polikeiji
Last active May 9, 2021 02:53
Show Gist options
  • Save polikeiji/4c7759428f59f824abd5265ed0857257 to your computer and use it in GitHub Desktop.
Save polikeiji/4c7759428f59f824abd5265ed0857257 to your computer and use it in GitHub Desktop.
How to make my PowerShell compatible with Bash to join open source projects.

Background

Recently, I've started trying to join open source projects using my Windows PC and I like to keep to use PowerShell as my shell environment.

But, unfortunately, some open source projects don't care of PowerShell users maybe because the number of PowerShell users is quite fewer than Bash (or other Unix shells).

For example, sometime NodeJS project includes these kind of package.json, and we can't execute npm run clean. PowerShell gives an alias to Remove-Item for rm, but its command options aren't compatible with rm's one.

"scripts": {
  "clean": "rm -rf ./build ./types"
}

Of course, we can use WSL2 or Cygwin, but I think PowerShell is a good shell and I want to learn about it. Why am I need to change my shell.

I'll write down here what I did for me who is forgatable.

What I did

Long-term solution

I've decided to use shx. We can easily make the above package.json snippet cross-platform compatible.

"scripts": {
  "clean": "shx rm -rf ./build ./types"
}

I updated the package.json and created a PR.

Temporal solution

Wrap Unix-specific commands using shx

I wanted to keep to create another PR to implement a proposal of a new feature before the PR is merged. To do that, I wrrapped rm command using shx in my PowerShell profile file.

del Alias:rm -Force -ErrorAction Ignore
del Function:SHX-RM -ErrorAction Ignore
function global:SHX-RM {
    shx rm @args
}
New-Alias rm SHX-RM -Force -Scope Global

I think we can take the same approach for other Unix specific commands.

Update PowerShell version

PowerShell 5, which is installed as a default one for Windows 10 at this moment, doesn't support && and ||. Recent version PowerShell, which is 7, supports it. So I installed new one in addition to the default one.

Links that I referred

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment