Windows 下的 git-bash 可以模拟大部分 linux 命令,基本上可以将 git-bash 当成一个简版的 linux 子系统使用。配合 alias 和 function 可以进行许多系统改进。
比如显示当前文件绝对路径:
lsw 显示文件绝对路径
lsww 显示文件 windows 路径
sedw 将带盘符的 linux 路径替换成 windows 路径
bash$ type lsw
lsw is a function
lsw ()
{
for i in $@;
do
ls -F --color=auto --show-control-chars `pwd`/$i;
done
}
bash$ type lsww
lsww is a function
lsww ()
{
for i in $@;
do
ls -F --color=auto --show-control-chars `pwd`/$i | sedw;
done
}
bash$ type sedw
sedw is a function
sedw ()
{
sed -e 's#^.##' -e 's#^.#U&:#' -e 's#/#\#g' $*
}
第一个 bash 函数 lsw 比较好理解,使用了 `pwd` 来获取当前目录路径。函数 sedw 不好理解,它使用了 sed 的多重编辑功能。第一段是删除第一个字符,第二段是将盘符改为大写并加上冒号,第三段是将斜杠替换为反斜杠。
简单的命令包装使用 alias,复杂的命令特别是含单引号双引号的,使用函数会比较方便。
在 git-bash 下调用 windows 下的命令会出现乱码。可以使用 iconv 工具进行编码转换。当使用 ipconfig 时调用的是原生命令,不会调用 alias
ipconfig
Windows IP Configuration
Ethernet adapter ▒▒̫▒▒ 2:
DNS Suffix . :
IPv4 Address. . . . . . . . . . . : 10.20.98.201
Subnet Mask . . . . . . . . . . . : 255.255.0.0
Default Gateway . . . . . . . . . : 10.20.0.1
type ipconfig
ipconfig is aliased to `ipconfig|iiconv'
type iiconv
iiconv is aliased to `iconv -f GBK -t UTF-8'
在git-bash下打开 windows 的文件浏览器,可以使用 windows explorer 命令。反之 git-bash 下访问 windows 路径可以使用双引号将路径扩起来然后 cd 进去。这样就能快速在 windows 和 linu 之间切换了。
在 git-bash 下想启动一个新的 git-bash 窗口,可以这样包装一下:
$ alias gsh
alias gsh='/c/bin/git_shell.sh&'
$ cat /c/bin/git_shell.sh
exec "C:Program FilesGitgit-bash.exe"
执行 alias 可以输出所有的 git-bash 下的命令别名,使用 declear -f 可以输出 git-bash 下所有的函数。
declear -f
pwdw ()
{
pwd | sed -e 's#^.(.)#1#' -e 's#^.#U&:#' -e 's#/#\#g'
}
sedw ()
{
sed -e 's#^.##' -e 's#^.#U&:#' -e 's#/#\#g' $*
}
upenv ()
{
source ~/.bashrc
}
proxy ()
{
export http_proxy=http://127.0.0.1:7890;
export https_proxy=http://127.0.0.1:7890
}
unproxy ()
{
unset http_proxy;
unset https_proxy
}
ls_path ()
{
echo $PATH | sed 's/:/n/g'
}
原文始发于微信公众号(生有可恋):Windows 下 git-bash 常用 alias
免责声明:文章中涉及的程序(方法)可能带有攻击性,仅供安全研究与教学之用,读者将其信息做其他用途,由读者承担全部法律及连带责任,本站不承担任何法律及连带责任;如有问题可邮件联系(建议使用企业邮箱或有效邮箱,避免邮件被拦截,联系方式见首页),望知悉。
- 左青龙
- 微信扫一扫
- 右白虎
- 微信扫一扫
评论