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

99封情书 提交于 2019-12-13 05:57:46

问题


In ~/.bash_profile aliases are defined, eg. alias vaguppro='vagrant up --provision

What I want is an echo of vagrant up --provision after typing vaguppro in the terminal.

Similar to when one types ls -hal in the terminal, then again type !ls. There's an echo of ls -hal in the terminal before execution.

SOME SOLUTIONS

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

or andlrc's function solution below.


回答1:


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. You can then press enter to run as usual.




回答2:


!ls is a history expansion that searches your command history for ls and expands to the found command. 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, ...

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


来源:https://stackoverflow.com/questions/37823239/how-to-echo-os-x-bash-alias-command-to-terminal-after-calling-it-like-cmd

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!