【问题标题】:Get current time (and date) WITHOUT opening a subshell [duplicate]在不打开子shell的情况下获取当前时间(和日期)[重复]
【发布时间】:2021-03-29 00:32:38
【问题描述】:

是否可以在不通过子shell 的情况下获取当前时间(可能还有日期)?

因为如果我没记错的话,这个命令会打开一个子shell吗?

d=$(date)

【问题讨论】:

  • cat /sys/class/rtc/rtc0/time cat /sys/class/rtc/rtc0/date

标签: bash shell date subshell


【解决方案1】:

对于 Bash≥4.2,您可以使用 printf%(datefmt)T 格式:

printf '%(%c)T\n' -1

-1 表示现在。

The Bash reference at the printf entry

将其放入变量中(因此不使用子shell):

printf -v d '%(%c)T' -1
echo "$d"

【讨论】:

    猜你喜欢
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-26
    相关资源
    最近更新 更多