BASH:OSX 中的 $(date) 结果为 "Illegal variable name"

标签 bash macos variables

在我的 bash 脚本之一中,我使用 $(date) 变量,该变量似乎在 LINUX 环境中工作正常。但是,当我尝试在 OSX 上使用该脚本时,出现错误:

echo $(date)
--> Illegal variable name.

最终,我想在两种环境中以相同的方式调用日期函数:

testvar=$(date +%s.%N)

您有 OSX 上的解决方法吗?也许一个自定义函数可以在两种环境中工作?

提前致谢!

最佳答案

听起来您正在使用 (C Shell)或 :

# ps
   PID TTY          TIME CMD
 37950 pts/51   00:00:00 csh
 37951 pts/51   00:00:00 ps
# echo $(date)
Illegal variable name.
# echo `date`
Thu Mar 23 17:30:02 CST 2017
# set date=`date`
# echo $date
Thu Mar 23 17:31:43 CST 2017
#

使用bash:

# bash
bash-4.4# echo $(date)
Thu Mar 23 17:28:19 CST 2017
bash-4.4#

关于BASH:OSX 中的 $(date) 结果为 "Illegal variable name",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42971767/

相关文章:

bash - 使用 bash cut 或 awk 根据列将一个文件拆分为多个文件

macos - Xamarin.Mac 应用程序在 Dock 中不可见

c++ - 高效快捷地访问 CoreFoundation 容器

r - 如何在 mutate 函数的计算中使用变量?

c++ - 在字符数组 C++ 中存储整数值

regex - 如何在触发 "unterminated substitute pattern"错误的 bash 脚本中重新组织 sed 正则表达式中的嵌套引号?

linux - 命令通过 'bash -c' 失败,但可以直接运行

c - 这个c代码行是做什么的? (const VAR = "string";)

bash - 我怎样才能在 bash 中找到 "try to do something and then detect if it fails"?

xcode - 如何通过 xcodeproj 在 Xcode 中构建 vlc 项目