shell 脚本未发出变量,但将其打印出来[duplicate]

44u64gxh  于 2022-11-16  发布在  Shell
关注(0)|答案(4)|浏览(117)

此问题在此处已有答案

How do I set a variable to the output of a command in Bash?(15个答案)
23天前关闭。

#!/bin/sh
#Saying the system time out loud

time= date +"%T"

say The current time now is, $time. Bye.

剧本是打印时间,不是说时间。

lawou6xi

lawou6xi1#

您不需要为变量赋值,因为您可以像这样包含date的输出

#!/bin/sh
#Saying the system time out loud
say "The time is now $(date +%T). Thank you."
fd3cxomn

fd3cxomn2#

试着像这样使用HereDoc ...

bash-3.2$ cat <<EOT
> The current time is now, `date +"%T"`
> EOT
The current time is now, 19:49:16
2lpgd968

2lpgd9683#

好的,这个成功了。谢谢。:)感谢你的帮助!

#!/bin/sh
#Saying the system time out loud

time=$(date +"%T")
say The time is now $time. Thank you.
0s0u357o

0s0u357o4#

time= date +"%T"
您希望将time设置为date命令的stdout,因此需要$(date +%T)%T两边的双引号不是必需的,因为%在此上下文中没有特殊含义)。
打印时间,而不是说出来。”
修正你的语法,它对我很有效,最近把时间说成了“214627”,例如。

time=$(date +%T)
say the time is $time

相关问题