├── .gitignore ├── LICENSE ├── Microsoft.PowerShell_profile.ps1 ├── README.md ├── This-is-sample-powershell-profile └── Microsoft.PowerShell_profile.ps1 ├── You-can-test-with-this-bash-script └── .bash_profile └── bash2pwsh.js /.gitignore: -------------------------------------------------------------------------------- 1 | bash_profile.ps1 2 | *.ignore 3 | *.ignore.mjs 4 | launch.json -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 nyabkun 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Microsoft.PowerShell_profile.ps1: -------------------------------------------------------------------------------- 1 | $BashAliasPwsh = "$PSScriptRoot/bash_alias.ps1" 2 | $Bash2Pwsh = "$PSScriptRoot/bash2pwsh.js" 3 | $BashAlias = "$PSScriptRoot/.alias.zsh" 4 | 5 | function bash2pwsh_alias() { 6 | echo "node $Bash2Pwsh $BashAlias $BashAliasPwsh" 7 | node $Bash2Pwsh $BashAlias $BashAliasPwsh 8 | } 9 | 10 | $PWSHProfile = $PSCommandPath 11 | 12 | # if you want to use bash like command in powershell, 13 | # it is recommended to install gow. 14 | # "scoop install gow" 15 | 16 | function edit_pwsh_profile { 17 | echo "code $PWSHProfile" 18 | code $PWSH_Profile 19 | } 20 | 21 | # https://stackoverflow.com/a/69135912/5570400 22 | # 23 | # Pipe output to vscode 24 | # 25 | # Usage : 26 | # ls | oc 27 | Function Out-Code { 28 | do { 29 | $filename = New-Guid 30 | $filename = $filename.toString() + ".md" 31 | } while (Test-Path $filename) 32 | $input > $filename 33 | code $filename 34 | Start-Sleep 1 35 | Remove-Item $filename 36 | } 37 | Set-Alias oc Out-Code 38 | 39 | # reload profile script 40 | function re { 41 | echo ". $PWSHProfile" 42 | . $PWSHProfile 43 | } 44 | 45 | # https://stackoverflow.com/a/47075453/14820021 46 | .$BashAliasPwsh 47 | 48 | echo "PS Profile [Editable] :" 49 | echo " $PSCommandPath" 50 | echo "Bash Alias [Editable] :" 51 | echo " $BashAlias" 52 | echo "Bash2Pwsh / Conversion Script [Editable]" 53 | echo " $Bash2Pwsh" 54 | echo "PW Bash Alias (is Read from PS Profile) [Auto Generated] :" 55 | echo " $BashAliasPwsh" 56 | echo "" 57 | echo "If you want to edit both PowerShell and Bash aliases at the same time :" 58 | echo " 1. Edit Bash Alias ('$BashAlias')" 59 | echo " 2. Type 'bash2pwsh_alias' in PowerShell (convert Bash Alias to Powershell Profile)" 60 | echo " 3. Type 're' in PowerShell (reload profile)" 61 | 62 | # https://github.com/dahlbyk/posh-git 63 | Import-Module posh-git 64 | # https://github.com/joonro/Get-ChildItemColor 65 | Import-Module Get-ChildItemColor 66 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Bash to Powershell Converter 2 | 3 | A [node.js](https://en.wikipedia.org/wiki/Node.js) script that converts [bash](https://en.wikipedia.org/wiki/Bash_(Unix_shell)) to [powershell](https://en.wikipedia.org/wiki/PowerShell). 4 | 5 | Only simple things like imports and aliases are supported. 6 | 7 | It is a simple script using regular expressions, so feel free to fork it and modify it as you wish. 8 | 9 | ```powershell 10 | node bash2pwsh.js ./You-can-test-with-this-bash-script/.bash_profile bash_profile.ps1 11 | ``` 12 | 13 | ## .bash_profile [Before] 14 | ```bash 15 | ## Util 16 | 17 | function pipe() { 18 | local outfile=~/pipe.md 19 | $@ >& $outfile 20 | code $outfile 21 | } 22 | 23 | ## App 24 | 25 | alias play_mpc_be='C:/App/MediaPlayerClassic-BE/mpc-be64.exe' 26 | alias code='C:/Dev/VSCode/Code.exe' 27 | 28 | ## Dir 29 | 30 | export d_diary='C:/Ws/diary' 31 | export d_ws='C:/Ws' 32 | export d_App='C:/App' 33 | export d_AppInst='C:/AppInst' 34 | export d_Dev='C:/Dev' 35 | 36 | alias cd_diary="cd $d_diary" 37 | alias cd_ws="cd $d_ws" 38 | alias cd_App="cd $d_App" 39 | alias cd_AppInst="cd $d_AppInst" 40 | alias cd_Dev="cd $d_Dev" 41 | 42 | ## Util Alias 43 | 44 | alias la='ls -a' 45 | alias ll='ls -al' 46 | alias exp_open_here='explorer .' 47 | 48 | ## Git Alias 49 | 50 | alias git_hist='git log --oneline --graph --decorate --all' 51 | alias git_com='git commit --allow-empty-message --no-edit' 52 | alias git_conf_global='git config --global --edit' 53 | alias git_conf_local='git config --edit' 54 | function git_add_com_push() { 55 |  git add -A 56 | git commit --allow-empty-message --no-edit 57 | git push 58 | } 59 | 60 | ## Edit 61 | 62 | function edit_alias(){ 63 | code ~/.alias.zsh 64 | } 65 | function edit_alias_sh_only(){ 66 | code ~/.alias-sh.zsh 67 | } 68 | function edit_zshrc() { 69 | code ~/.zshrc 70 | } 71 | function edit_pwsh_profile(){ 72 | code C:/Users/owner/Documents/PowerShell/Microsoft.PowerShell_profile.ps1 73 | } 74 | 75 | ## Env 76 | export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfg 77 | export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/share/pkgconfig 78 | ``` 79 | 80 | `>>>` 81 | 82 | ## bash_profile.ps1 [After] 83 | 84 | ```powershell 85 | ## Util 86 | 87 | function pipe() { 88 | local outfile=C:/msys64/home/owner/pipe.md 89 | $args | Out-File -FilePath $outfile 90 | code $outfile 91 | } 92 | 93 | ## App 94 | 95 | Set-Alias -Name play_mpc_be -Value 'C:/App/MediaPlayerClassic-BE/mpc-be64.exe' 96 | Set-Alias -Name code -Value 'C:/Dev/VSCode/Code.exe' 97 | 98 | ## Dir 99 | 100 | $d_diary='C:/Ws/diary' 101 | $d_ws='C:/Ws' 102 | $d_App='C:/App' 103 | $d_AppInst='C:/AppInst' 104 | $d_Dev='C:/Dev' 105 | 106 | function cd_diary() { cd $d_diary } 107 | function cd_ws() { cd $d_ws } 108 | function cd_App() { cd $d_App } 109 | function cd_AppInst() { cd $d_AppInst } 110 | function cd_Dev() { cd $d_Dev } 111 | 112 | ## Util Alias 113 | 114 | function la() { ls -a } 115 | function ll() { ls -al } 116 | function exp_open_here() { explorer . } 117 | 118 | ## Git Alias 119 | 120 | function git_hist() { git log --oneline --graph --decorate --all } 121 | function git_com() { git commit --allow-empty-message --no-edit } 122 | function git_conf_global() { git config --global --edit } 123 | function git_conf_local() { git config --edit } 124 | function git_add_com_push() { 125 | git add -A 126 | git commit --allow-empty-message --no-edit 127 | git push 128 | } 129 | 130 | ## Edit 131 | 132 | function edit_alias(){ 133 | code C:/msys64/home/owner/.alias.zsh 134 | } 135 | function edit_alias_sh_only(){ 136 | code C:/msys64/home/owner/.alias-sh.zsh 137 | } 138 | function edit_zshrc() { 139 | code C:/msys64/home/owner/.zshrc 140 | } 141 | function edit_pwsh_profile(){ 142 | code C:/Users/owner/Documents/PowerShell/Microsoft.PowerShell_profile.ps1 143 | } 144 | 145 | ## Env 146 | $env:PKG_CONFIG_PATH += ";C:/msys64/usr/local/lib/pkgconfg" 147 | $env:PKG_CONFIG_PATH += ";C:/msys64/usr/local/share/pkgconfig" 148 | echo "Loaded : $PSCommandPath" 149 | ``` 150 | -------------------------------------------------------------------------------- /This-is-sample-powershell-profile/Microsoft.PowerShell_profile.ps1: -------------------------------------------------------------------------------- 1 | # !! This is sample 2 | # 3 | # C:/Users//Documents/PowerShell/Microsoft.PowerShell_profile.ps1 4 | 5 | node $PSScriptRoot/bash2pwsh.js C:/msys64/home/owner/.alias.zsh ./bash_alias.ps1 6 | 7 | # https://stackoverflow.com/a/47075453/14820021 8 | ."./bash_alias.ps1" 9 | 10 | echo "Loaded : $PSCommandPath" -------------------------------------------------------------------------------- /You-can-test-with-this-bash-script/.bash_profile: -------------------------------------------------------------------------------- 1 | ## !! This is Sample 2 | 3 | ## Util 4 | 5 | function pipe() { 6 | local outfile=~/pipe.md 7 | $@ >& $outfile 8 | code $outfile 9 | } 10 | 11 | ## App 12 | 13 | alias play_mpc_be='C:/App/MediaPlayerClassic-BE/mpc-be64.exe' 14 | alias code='C:/Dev/VSCode/Code.exe' 15 | 16 | ## Dir 17 | 18 | export d_diary='C:/Ws/diary' 19 | export d_ws='C:/Ws' 20 | export d_App='C:/App' 21 | export d_AppInst='C:/AppInst' 22 | export d_Dev='C:/Dev' 23 | 24 | alias cd_diary="cd $d_diary" 25 | alias cd_ws="cd $d_ws" 26 | alias cd_App="cd $d_App" 27 | alias cd_AppInst="cd $d_AppInst" 28 | alias cd_Dev="cd $d_Dev" 29 | 30 | ## Util Alias 31 | 32 | alias la='ls -a' 33 | alias ll='ls -al' 34 | alias exp_open_here='explorer .' 35 | 36 | ## Git Alias 37 | 38 | alias git_hist='git log --oneline --graph --decorate --all' 39 | alias git_com='git commit --allow-empty-message --no-edit' 40 | alias git_conf_global='git config --global --edit' 41 | alias git_conf_local='git config --edit' 42 | function git_add_com_push() { 43 | git add -A 44 | git commit --allow-empty-message --no-edit 45 | git push 46 | } 47 | 48 | ## Edit 49 | 50 | function edit_alias(){ 51 | code ~/.alias.zsh 52 | } 53 | function edit_alias_sh_only(){ 54 | code ~/.alias-sh.zsh 55 | } 56 | function edit_zshrc() { 57 | code ~/.zshrc 58 | } 59 | function edit_pwsh_profile(){ 60 | code C:/Users/owner/Documents/PowerShell/Microsoft.PowerShell_profile.ps1 61 | } 62 | 63 | ## Env 64 | export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfg 65 | export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/share/pkgconfig 66 | -------------------------------------------------------------------------------- /bash2pwsh.js: -------------------------------------------------------------------------------- 1 | // You can edit this file. 2 | // This is a simple conversion script which uses Regular Expression. 3 | // If additional conversions are required, please add it as you wish. 4 | 5 | const fs = require("fs"); 6 | 7 | const BASH_ROOT = "C:/msys64"; 8 | const BASH_HOME = "C:/msys64/home/owner"; 9 | 10 | const src = process.argv[2]; 11 | const dest = process.argv[3]; 12 | 13 | try { 14 | const bash = fs.readFileSync(src, "utf8"); 15 | let pwsh = convert(bash); 16 | 17 | pwsh = 18 | `\ 19 | # This file is auto generated by bash2pwsh.js 20 | 21 | ` + pwsh; 22 | 23 | fs.writeFileSync(dest, pwsh); 24 | 25 | console.log(`"${dest}" was created Successfully!`); 26 | } catch (err) { 27 | console.log("Error : " + err.message); 28 | } 29 | 30 | /** 31 | * Convert bash script to powershell script. 32 | * @param {String} bash bash script body 33 | * @returns powershell script body 34 | */ 35 | function convert(bash) { 36 | // bash & powershell: 37 | // "$a" > variables are expaneded 38 | // '$a' > variables are not expanded 39 | 40 | // bash : export vs alias 41 | // > One difference between the two is that aliases are only a shell feature. 42 | // > Environment variables are inherited by all subprocesses (unless deliberately cleared). 43 | // https://unix.stackexchange.com/a/129609 44 | // 45 | // export diray='C:/' 46 | // echo $diary 47 | // 48 | // alias diary='C:/' 49 | // echo diary 50 | 51 | // bash : variable vs alias 52 | // https://stackoverflow.com/a/7342780/14820021 53 | 54 | // export d_diary='C:/Ws/diary' 55 | // >>> 56 | // $d_diary='C:/Dev' 57 | bash = bash.replace(/export (\w+)=(['"].*?['"])/g, "$$$1=$2"); 58 | 59 | // alias which does not contains command or variables 60 | // alias code='C:/Dev/VSCode/Code.exe' 61 | // >>> 62 | // Set-Alias -Name code -Value 'cd $d_diary' 63 | bash = bash.replace( 64 | /alias (\w+)=(['"](?!\w{1,4} )[^$$]+?['"])/g, 65 | "Set-Alias -Name $1 -Value $2" 66 | ); 67 | 68 | // ## alias which contains command or variables 69 | // alias cd_diary="cd $d_diary" 70 | // >>> 71 | // function cd_diary() { cd $d_diary } 72 | bash = bash.replace(/alias (\w+)=['"](.+?)['"]/g, "function $1() { $2 }"); 73 | 74 | // EDITOR='code' 75 | // >>> 76 | // $EDITOR='code' 77 | bash = bash.replace(/(^(?:\s+)?)(\w+)=(['"].*?['"])$/gm, "$1$$$2=$3"); 78 | 79 | // export PATH=$PATH:/usr/bin 80 | // >>> 81 | // $env:Path += ";C:/msys64/usr/bin" 82 | bash = bash.replace( 83 | /(?<=[:"'])\/(bin|dev|etc|home|mingw|msys|opt|tmp|usr|var)/g, 84 | `${BASH_ROOT}/$1` 85 | ); 86 | bash = bash.replace(/export (\w+)=\$\w+:([\w:/\\]+)/g, '$env:$1 += ";$2"'); 87 | 88 | // ~/.bashrc 89 | // >>> 90 | // C:/msys64/.bashrc 91 | bash = bash.replace(/~\//g, `${BASH_HOME}/`); 92 | 93 | // $@ 94 | // >>> 95 | // $args 96 | bash = bash.replace(/\$@/g, "$$args"); 97 | 98 | // bash/zsh arg numbers are different from powershell 99 | // $1 100 | // >>> 101 | // $args[0] 102 | for (let i = 0; i < 10; i++) { 103 | bash = bash.replace(new RegExp("$" + (i + 1), "g"), "$$args[" + i + "]"); 104 | } 105 | 106 | // local some_variable 107 | // >>> 108 | // $some_variable 109 | bash = bash.replace(/^\s+local\s+(\w+)/gm, "$$$1"); 110 | 111 | // /c/Program Files/ 112 | // >>> 113 | // C:/Program Files 114 | bash = bash.replace(/(?:\s|^)\/([cdefghijk])\//im, "$1:/"); 115 | 116 | // $args >& $outfile 117 | // >>> 118 | // $args Out-File -FilePath $outFile 119 | bash = bash.replace(/(\S+)\s*>&\s*(\S+)/g, "$1 | Out-File -FilePath $2"); 120 | 121 | return bash; 122 | } 123 | --------------------------------------------------------------------------------