如何编写 bash 别名来列出所有名称匹配的环境变量
你好,友好的朋友们。在使用 Bash 的 Linux 上,我经常需要列出名称以某个预定义单词开头的环境变量(env-var)。例如,要列出名称模式为 gmXXX 的所有环境变量,我会这样做
[chj @linux-ic37 ~]$ export|grep '^declare -x gm'
declare -x gmu_DO_SHOW_COMPILE_CMD="1"
declare -x gmu_DO_SHOW_LINK_CMD="1"
,但每次都写那么长的命令很无聊。那么,我可以写一个别名来简化它吗?我尝试过但没有运气。
[chj @linux-ic37 ~]$ alias fvgm="export|grep \'^declare -x gm\'"
[chj @linux-ic37 ~]$ fvgm
grep: gm': No such file or directory
有人可以帮我吗?
为了更进一步,我可以将 'gm' 参数化,比如
fv gm
列出以 'gm' 开头的所有 env-var 吗?
Hello friendly guys. On Linux with Bash, I often need to list environment variables(env-var) whose names start with some predefined word. For example, to list all env-vars with name pattern gmXXX, I will do
[chj @linux-ic37 ~]$ export|grep '^declare -x gm'
declare -x gmu_DO_SHOW_COMPILE_CMD="1"
declare -x gmu_DO_SHOW_LINK_CMD="1"
But writing that long command everytime is boring. So, can I write an alias to simplify it? I tried but no luck.
[chj @linux-ic37 ~]$ alias fvgm="export|grep \'^declare -x gm\'"
[chj @linux-ic37 ~]$ fvgm
grep: gm': No such file or directory
Can anyone help me out?
To make it further, can I go as far as having 'gm' parameterized, like
fv gm
to list all env-var starting with 'gm' ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您不需要转义别名中的单引号。将其更改为:
您需要编写一个函数以使其通用。例如:
然后您可以调用:
You don't need to escape the single-quote in your alias. Change it to:
You need to write a function to make it generic. For example:
You can then call:
怎么样:
fv() { env | grep "^$1"; }
How about:
fv() { env | grep "^$1"; }
您是否尝试过
compgen -v STR
这将生成所有以 STR 开头的变量名称的列表
这应该是比管道导出更好的选择,因为它不涉及另一个进程
因此相同的 fv 函数可以编写为[编辑以添加值]
Did you try
compgen -v STR
This will generate a list of all the variable names starting with STR
This should be a better option than piping
export
because it does not involve another processSo the same fv function can be written as [edited to add values]