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
bash$ \ipconfig
Windows IP Configuration
Ethernet adapter ▒▒̫▒▒ 2:
Connection-specific DNS Suffix . :
IPv4 Address. . . . . . . . . . . : 10.20.98.201
Subnet Mask . . . . . . . . . . . : 255.255.0.0
Default Gateway . . . . . . . . . : 10.20.0.1
bash$ type ipconfig
ipconfig is aliased to `ipconfig|iiconv'
bash$ 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
#!/bin/bash
exec "C:\Program Files\Git\git-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'
}
复制
文章转载自生有可恋,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。
评论
相关阅读
[MYSQL] query_id和STATEMENT_ID在不同OS上的关系
大大刺猬
41次阅读
2025-03-26 19:08:13
在删除Hyper-V虚拟机时,是否需要选择保留虚拟硬盘文件?
手机用户0220
35次阅读
2025-03-06 10:44:09
Windows部署MinIO对象存储服务的方法
疯狂学习GIS
9次阅读
2025-03-05 22:01:11
Windows Server服务器操作系统漏洞修复报错问题处理
IT那活儿
9次阅读
2025-03-03 09:52:30
Ditto:Windows 的剪贴板历史管理工具
Linux技术宅
8次阅读
2025-03-05 06:25:22
Loaf:假装 Windows 更新的工具
Linux技术宅
6次阅读
2025-03-06 06:31:31
新特性:支持审核指定的 Git 仓库分支
爱可生开源社区
6次阅读
2025-03-10 10:29:40
告别es索引混乱!3分钟掌握Alias和Template的黄金搭档法则
Linux运维智行录
2次阅读
2025-03-17 18:48:53