bash echo 时间日期格式

Fra*_*cis 5 bash shell

我已经测试了我需要的格式,但是我无法获得回显输出,请尝试将引号放在不同的位置。

\n
[root@]# date '+%B %V %T.%3N:'\nJuly 29 21:01:24.766:\n
Run Code Online (Sandbox Code Playgroud)\n
echo 'date \xe2\x80\x99+%B %V %T.%3N:\xe2\x80\x99 %LINK-3-UPDOWN: Interface Port-channel1, changed state to up'\n
Run Code Online (Sandbox Code Playgroud)\n

预期产出

\n
July 29 21:01:24.766: %LINK-3-UPDOWN: Interface Port-channel1, changed state to up'\n
Run Code Online (Sandbox Code Playgroud)\n

daw*_*awg 10

你可以做:

tm=$(date '+%B %V %T.%3N:')
echo "${tm} %LINK-3-UPDOWN: Interface Port-channel1, changed state to up"
Run Code Online (Sandbox Code Playgroud)

印刷:

July 29 09:22:23.3N: %LINK-3-UPDOWN: Interface Port-channel1, changed state to up
Run Code Online (Sandbox Code Playgroud)

注意双"与单'。只有双引号中的字符串才会在 shell 中进行插值:

bash-5.1$ s1='hello'
bash-5.1$ s2='there'
bash-5.1$ echo "${s1} ${s2}"
hello there
Run Code Online (Sandbox Code Playgroud)

对比:

bash-5.1$ echo '${s1} ${s2}'
${s1} ${s2}
Run Code Online (Sandbox Code Playgroud)

如果你想把所有事情都写在一行中:

echo "$(date '+%B %V %T.%3N:') %LINK-3-UPDOWN: Interface Port-channel1, changed state to up"
Run Code Online (Sandbox Code Playgroud)

或者修改你的date格式字符串:

date '+%B %V %T.%3N: %%LINK-3-UPDOWN: Interface Port-channel1, changed state to up'
Run Code Online (Sandbox Code Playgroud)