5

In terminal, if i run echo $(date), it will give me the current date.

However, when i put the command in .bash_profile as an alias

alias dt="echo $(date)"

it will give me stale date. Namely, when i run dt, it will always give me the same date.

Any workaround? Thanks!

4

1 回答 1

4

您需要引用它以防止扩展:

alias dt="echo \$(date)"
alias dt="echo \"\$(date)\""  ## (internally quoted)

或者只使用单引号:

alias dt='echo $(date)'
alias dt='echo "$(date)"'  ## (internally quoted)

更好的是使用一个函数:

dt() { echo "$(date)"; }
于 2013-09-22T20:24:14.077 回答