简体   繁体   English

调用后如何将OS X bash别名命令回显到终端(如!cmd)?

[英]How to echo OS X bash alias' command to terminal after calling it (like !cmd)?

In ~/.bash_profile aliases are defined, eg. 在〜/ .bash_profile中定义了别名,例如。 alias vaguppro='vagrant up --provision

What I want is an echo of vagrant up --provision after typing vaguppro in the terminal. 我想要的是在终端中键入vaguppro后对vagrant up --provision的回声。

Similar to when one types ls -hal in the terminal, then again type !ls . 类似于在终端中键入ls -hal时,然后再次键入!ls There's an echo of ls -hal in the terminal before execution. 执行前在终端中有一个ls -hal的回ls -hal

SOME SOLUTIONS 一些解决方案

vaguppro='vagrant up --provision'
alias vaguppro='echo $vaguppro && $vaguppro'

or andlrc's function solution below. 或下面的andlrc函数解决方案。

You may find this shortcut useful: 您可能会发现此快捷方式很有用:

   shell-expand-line (M-C-e)
          Expand the line as the shell does.  This performs alias and his‐
          tory expansion as well as all of the shell word expansions.  See
          HISTORY EXPANSION below for a description of history expansion.

In other words, if you write vaguppro and press Ctrl + Alt + E in bash' default Emacs mode, it will expand aliases in the current line and turn it into vagrant up --provision . 换句话说,如果您编写vaguppro并在bash的默认Emacs模式下按Ctrl + Alt + E ,它将在当前行中扩展别名并将其转换为vagrant up --provision You can then press enter to run as usual. 然后,您可以按Enter键照常运行。

!ls is a history expansion that searches your command history for ls and expands to the found command. !ls是一个历史扩展,它在命令历史中搜索ls并扩展为找到的命令。 As a bonus the expansion is also printed to the terminal. 作为奖励,扩展也会打印到终端上。

To get the same behavior with your aliases, I think you would need to convert it to a function and print it manually: 为了获得与别名相同的行为,我认为您需要将其转换为函数并手动打印:

vaguppro() {
  echo "vagrant up --provision"
  vagrant up --provision "$@"
}

I almost always recommend people using functions over aliases, unless it for adding colors for grep , ls , ... 我几乎总是建议人们使用函数而不是别名,除非它为grepls ,...添加颜色。

alias grep='grep --color=auto'
alias ls='ls --color=auto'
alias cd..='cd ..'

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM