How to use Linux shell echo date to a log file All In One
如何使用 Linux shell 将日期打印输出到日志文件
solutions
# 命令 ✅
$ date >> /etc/rc.local.md# echo 反斜号 ✅
$ echo `date` >> /etc/rc.local.md# echo 双引号 包裹 反斜号 ✅
$ echo "`date` \n" >> /etc/rc.local.md
eric@rpi3b:~ $ sudo /etc/rc.local
My IP address is 192.168.101.17
eric@rpi3b:~ $ cat /etc/rc.local.md
# rc.local logs md file# timestamp 时间戳Tue Mar 11 01:16:53 CST 2025 /etc/rc.local testing begin ✅
/etc/rc.local testing end ✅
eric@rpi3b:~ $ cat /etc/rc.local
#!/bin/sh -e# 添加日期时间
echo "`date` \n" >> /etc/rc.local.md 2>&1echo "/etc/rc.local testing begin ✅" >> /etc/rc.local.md 2>&1# add below line before exit 0 ✅
# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; thenprintf "My IP address is %s\n" "$_IP"
fi# 禁用电源关闭
iwconfig wlan0 power offecho "/etc/rc.local testing end ✅" >> /etc/rc.local.md 2>&1# sudo /sbin/iw dev wlan0 set power_save off
# su eric -c "exec /sbin/iw dev wlan0 set power_save off"# 可执行脚本
# su eric -c "exec /home/eric/Desktop/echo-test.sh"exit 0
demos
# 命令 ✅
➜ cyclic-express-server git:(main) ✗ $ date
Tue Mar 11 01:01:15 CST 2025
# echo 反斜号 ✅
➜ cyclic-express-server git:(main) ✗ $ echo `date`
Tue Mar 11 01:05:07 CST 2025
# echo 双引号 包裹 反斜号 ✅
➜ cyclic-express-server git:(main) ✗ $ echo "`date` \n"
Tue Mar 11 01:04:07 CST 2025 # ehco $var ❌
➜ cyclic-express-server git:(main) ✗ $ echo "$date \n"# ehco 双引号 包裹 string ❌
➜ cyclic-express-server git:(main) ✗ $ echo "date \n"
date# echo 单引号 包裹 反斜号 ❌
➜ cyclic-express-server git:(main) ✗ $ echo '`date` \n'
`date` # echo string ❌
➜ cyclic-express-server git:(main) ✗ $ echo date
date
(🐞 反爬虫测试!打击盗版⚠️)如果你看到这个信息, 说明这是一篇剽窃的文章,请访问 https://www.cnblogs.com/xgqfrms/ 查看原创文章!
refs
https://unix.stackexchange.com/questions/98391/what-is-the-difference-between-echo-date-echo-date-and-echo-date
©xgqfrms 2012-2021
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!