在 bash 脚本中使用别名

在 bash 脚本中使用别名

我在代码中使用别名,它第一次工作并显示当前时间,但第二次,当我调用它时,它显示过去的时间(之前显示的时间而不是当前时间)

  GNU nano 6.2                            aliases.sh                                     
#!/bin/bash

shopt -s expand_aliases
alias TODAY="date"
echo "This shell represent differences between static substitution and dynamic substitut>
echo "==============================================================================="

DATE=`date`

echo "Static substitution"
echo "Start of bash script is: $DATE"
echo "Now I'm going to sleep for one minute"
sleep 5s

echo " end of the script: $DATE"

echo "------------------------------------------------------------------------------"
echo "Dynamic Substitution"

DDATE=`TODAY`
echo " Current Date is $DDATE"
echo "Now I'm going to sleep for one minute"
sleep 30s
echo "end of script date is $DDATE"`

答案1

您将日期设置为变量,因此仅调用一次,然后该变量是静态引用。

你可以这样做:

echo "------------------------------------------------------------------------------"
echo "Dynamic Substitution"

echo " Current Date is $(today)"
echo "Now I'm going to sleep for one minute"
sleep 30s
echo "end of script date is $(today)"

请注意,$(...)命令替换通常优于反引号。

相关内容