source ~/.bashrc
在运行之前或source ~/.bash_aliases
仅在第一次运行别名之后,别名才能正常工作。有没有一种神奇的方法可以$todaydir
在不这样做的情况下正确替换字符串?
[myname@linux_server ~]$ cat .bash_aliases
alias col="todaydir=$(date | awk '{print $2$3}') ; cd /home/myname/collect/$todaydir"
[myname@linux_server ~]$ alias | grep today
alias col='todaydir=Feb24 ; cd /home/myname/collect/'
[myname@linux_server ~]$ source .bashrc
[myname@linux_server ~]$ alias | grep today
alias col='todaydir=Feb24 ; cd /home/myname/collect/'
[myname@linux_server ~]$ col
[myname@linux_server collect]$ alias | grep today
alias col='todaydir=Feb24 ; cd /home/myname/collect/'
[myname@linux_server collect]$ source ~/.bashrc
[myname@linux_server collect]$ alias | grep today
alias col='todaydir=Feb24 ; cd /home/myname/collect/Feb24'
[myname@linux_server collect]$ col
[myname@linux_server Feb24]$
[myname@linux_server ~]$ bash --version
bash --version
GNU bash, version 4.1.2(1)-release (x86_64-redhat-linux-gnu)
Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
[myname@linux_server collect]$ cat ~/.bashrc
# .bashrc
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
# User specific aliases and functions
export PATH=$PATH:~/bin
source ~/.bash_aliases
是的,是的,bash 已经很古老了;我不是根。
答案1
命令替换在您定义 时进行评估alias
,而不是在执行时进行评估。对于$todaydir
此时为空的变量的求值也是如此。这是因为您在双引号而不是单引号中定义了别名。
这就是你的意思(我使用不同的日期格式):
$ alias col='todaydir=$(date | awk '\''{print $2$3}'\''); cd /home/myname/collect/$todaydir'
$ alias col
alias col='todaydir=$(date | awk '\''{print $2$3}'\''); cd /home/myname/collect/$todaydir'
$ col
-bash: cd: /home/myname/collect/25Feb: No such file or directory
但你不需要awk
and 变量,使用date
and 格式:
alias col='cd /home/myname/collect/"$(date "+%b%d")"'