为什么我在Bash中的字符串比较总是被评估为真?

Mar*_*rek 8 string bash

我有一个脚本检查文件是否是最新的.

updatedate=`ls -l file | sed -e 's/  */ /g' | cut -d' ' -f7` #cut the modification time
nowdate=`date +"%H:%M"`
echo "$updatedate $nowdate"
if [ "$updatedate"="$nowdate" ]
then
  echo 'OK'
else
  echo 'NOT OK'
fi
Run Code Online (Sandbox Code Playgroud)

但是当我运行它时,比较总是如此:

$ ./checkfile
10:04 10:07
OK

$ ./checkfile
10:07 10:07
OK
Run Code Online (Sandbox Code Playgroud)

为什么?

jco*_*ctx 18

你需要在等号的每一边有一个空格.


if [ "$updatedate" = "$nowdate" ]
Run Code Online (Sandbox Code Playgroud)