简体   繁体   English

定义引用其他别名的别名

[英]Define alias that references other aliases

I'd need to be able to define an alias in a Debian shell that includes other aliases, for a project I'm currently working on.对于我目前正在处理的项目,我需要能够在包含其他别名的 Debian shell 中定义别名。

Let's look at a code example to make things more clear.让我们看一个代码示例,让事情更清楚。

  1. alias foo=foo
  2. alias bar=bar

If I run type foo it returns foo is aliased to 'foo' , and type bar returns bar is aliased to 'bar' .如果我运行type foo它返回foo is aliased to 'foo'type bar返回bar is aliased to 'bar' Up to here all fine.到这里一切都很好。 Now, where I'm having the problem.现在,我遇到了问题。

  1. alias foobar=$foo$bar

Doesn't work.不起作用。 type foobar returns foobar is aliased to '' . type foobar返回foobar is aliased to ''

I've tried alias foobar=${foo}${bar} and it doesn't work either.我试过alias foobar=${foo}${bar}但它也不起作用。

Once I get this working, in the final version I actually need some text in between the two aliases, imagine something like: alias fooandbar=${foo}and${bar} .一旦我开始工作,在最终版本中,我实际上需要在两个别名之间添加一些文本,想象一下: alias fooandbar=${foo}and${bar}

Can anyone help please?有人可以帮忙吗?

To reuse alias in another alias use:要在另一个别名中重用别名,请使用:

foobar='foo;bar'

However I would suggest you to consider using shell function to get better control over this.但是我建议您考虑使用 shell 函数来更好地控制它。

Following @anubhava's sage advice:遵循@anubhava 的明智建议:

foo() { echo foo; }
bar() { echo bar; }
foobar() { echo "$(foo)$(bar)"; }
fooandbar() { echo "$(foo)and$(bar)"; }

Spaces and semicolons inside {} are required there. {}内需要空格和分号。

I used this in csh and it worked for me :我在 csh 中使用了它,它对我有用:

alias new_alias 'vi old_alias'别名 new_alias 'vi old_alias'

Here is an example of alias that i'm using这是我正在使用的别名示例

#clear
alias cls='clear; ls'
alias ccls='cd; cls' # used alias cls

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在csh中定义“别名” - How to define 'alias' in csh 在 fish shell 中定义一个别名 - Define an alias in fish shell 为每个文件夹定义 oh my zsh 别名 - Define oh my zsh aliases per folder 我为用户〜/ .bashrc添加了别名,但是“sudo -u user -i'alias_name'”仍然报告“找不到命令”? - I added aliases to user ~/.bashrc but “sudo -u user -i 'alias_name'” still reports “command not found”? Alias在cli上运行良好,但如果在aliases.sh文件中设置了该别名则不会 - Alias works from well from the cli but no if it is set on aliases.sh file zsh和普通shell如何共享环境变量和别名而不相互复制 - How can zsh and normal shell share environment variables and aliases without copying each other 使用-static进行编译会导致对其他库中函数的未定义引用 - Compiling with -static causes undefined references to functions in other libraries Grid1-> ClearGrid(); 及其他对wxGrid崩溃应用程序的引用 - Grid1->ClearGrid(); & other references to wxGrid crash app 如何定义 Linux 中所有文件的别名,包括隐藏文件、列表编号、总行数? - How to define the alias with all the files including hidden files, list numbering, total lines in Linux? 无法通过 LAN 从 windows ping avahi 别名,但可以从其他 linux 虚拟机 - can't ping avahi alias from windows over LAN but can from other linux VMs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM