bash 在bash脚本中创建时间戳变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17066250/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create timestamp variable in bash script
提问by Dan
I am trying to create a timestamp variable in a shell script to make the logging a little easier. I want to create the variable at the beginning of the script and have it print out the current time whenever I issue echo $timestamp
. It proving to be more difficult then I thought. Here are some things I've tried:
我正在尝试在 shell 脚本中创建一个时间戳变量,以使日志记录更容易一些。我想在脚本的开头创建变量,并在我每次发出echo $timestamp
. 事实证明,这比我想象的要困难得多。以下是我尝试过的一些事情:
timestamp="(date +"%T")"
echo prints out (date +"%T")
timestamp="(date +"%T")"
回声打印出来 (date +"%T")
timestamp="$(date +"%T")"
echo prints the time when the variable was initialized.
timestamp="$(date +"%T")"
echo 打印变量初始化的时间。
Other things I've tried are just slight variations that didn't work any better. Does anyone know how to accomplish what I'm trying to do?
我尝试过的其他事情只是轻微的变化,但效果并不好。有谁知道如何完成我想要做的事情?
采纳答案by giordano
In order to get the current timestamp and not the time of when a fixed variable is defined, the trick is to use a function and nota variable:
为了获得当前时间戳而不是定义固定变量的时间,技巧是使用函数而不是变量:
#!/bin/bash
# Define a timestamp function
timestamp() {
date +"%T"
}
# do something...
timestamp # print timestamp
# do something else...
timestamp # print another timestamp
# continue...
If you don't like the format given by the %T
specifier you can combine the other time conversion specifiers accepted by date
. For GNU date
, you can find the complete list of these specifiers in the official documentation here: https://www.gnu.org/software/coreutils/manual/html_node/Time-conversion-specifiers.html#Time-conversion-specifiers
如果您不喜欢%T
说明符给出的格式,您可以将date
. 对于 GNU date
,您可以在此处的官方文档中找到这些说明符的完整列表:https: //www.gnu.org/software/coreutils/manual/html_node/Time-conversion-specifiers.html#Time-conversion-specifiers
回答by dchakarov
If you want to get unix timestamp, then you need to use:
如果要获取unix时间戳,则需要使用:
timestamp=$(date +%s)
%T
will give you just the time; same as %H:%M:%S
(via http://www.cyberciti.biz/faq/linux-unix-formatting-dates-for-display/)
%T
会给你时间;与%H:%M:%S
(通过http://www.cyberciti.biz/faq/linux-unix-formatting-dates-for-display/)相同
回答by Girdhar Singh Rathore
DATE=`date "+%Y%m%d"`
DATE_WITH_TIME=`date "+%Y%m%d-%H%M%S"` #add %3N as we want millisecond too
回答by Caner
ISO 8601 format (2018-12-23T12:34:56
) is more readable than UNIX timestamp. However on some OSs you cannot have :
in the filenames. Therefore I recommend using something like this instead:
ISO 8601 格式 ( 2018-12-23T12:34:56
) 比 UNIX 时间戳更具可读性。但是,在某些操作系统:
上,文件名中不能包含。因此我建议使用这样的东西:
2018-12-23_12-34-56
You can use the following command to get the timestamp in this format:
您可以使用以下命令以这种格式获取时间戳:
TIMESTAMP=`date +%Y-%m-%d_%H-%M-%S`
This is the format I have seen many applications use. Another nice thing about this is that if your file names start with this, you can sort them alphabetically and they would be sorted by date.
这是我见过的许多应用程序使用的格式。另一个好处是,如果您的文件名以此开头,您可以按字母顺序对它们进行排序,并且它们将按日期排序。
回答by Sean Lin
I am using ubuntu 14.04.
我正在使用 ubuntu 14.04。
The correct way in my system should be date +%s
.
我系统中的正确方法应该是date +%s
.
The output of date +%T
is like 12:25:25
.
的输出date +%T
就像12:25:25
。
回答by choroba
Use command substitution:
使用命令替换:
timestamp=$( date +%T )
回答by Bill
You can use
您可以使用
timestamp=`date --rfc-3339=seconds`
This delivers in the format 2014-02-01 15:12:35-05:00
这以格式提供 2014-02-01 15:12:35-05:00
The back-tick (`
) characters will cause what is between them to be evaluated and have the result included in the line. date --help
has other options.
反引号 ( `
) 字符将导致对它们之间的内容进行评估,并将结果包含在该行中。 date --help
有其他选择。
回答by chepner
Recent versions of bash
don't require call to the external program date
:
bash
不需要调用外部程序的最新版本date
:
printf -v timestamp '%(%T)T'
%(...)T
uses the corresponding argument as a UNIX timestamp, and formats it according to the strftime
-style format between the parentheses. An argument of -1
corresponds to the current time, and when no ambiguity would occur can be omitted.
%(...)T
使用相应的参数作为 UNIX 时间戳,并根据strftime
括号之间的 -style 格式对其进行格式化。的参数-1
对应于当前时间,当不会出现歧义时可以省略。
回答by Christoffer Nissen
And for my fellow Europeans, try using this:
对于我的欧洲同胞,请尝试使用以下方法:
timestamp=$(date +%d-%m-%Y_%H-%M-%S)
will give a format of the format: "15-02-2020_19-21-58"
将给出格式的格式:“15-02-2020_19-21-58”
You call the variable and get the string representation like this
您调用变量并获得这样的字符串表示
$timestamp
回答by Steven Penny
timestamp=$(awk 'BEGIN {srand(); print srand()}')
srand without a value uses the current timestamp with most Awk implementations.
没有值的 srand 在大多数 awk 实现中使用当前时间戳。