bash 在命令行上获取当前年份

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32161888/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 13:29:37  来源:igfitidea点击:

Get the current year on the commandline

bashdate

提问by Uli K?hler

Using wget, I need to download a file whose URL contains the year. How can I embed the current 4-digit year (at the time of running the command, e.g. 2015)? For example, what would the command currentyearbe in

使用 wget,我需要下载一个 URL 包含年份的文件。如何嵌入当前的 4 位数年份(在运行命令时,例如 2015)?例如,命令currentyear是什么

wget "http://example.com/data-$(currentyear).txt"

What would be the corresponding command if I'd need the two-digit year (e.g. 15 for 2015)?

如果我需要两位数的年份(例如 2015 年为 15),相应的命令是什么?

回答by Uli K?hler

You can simply use the datecommand as date +%Y:

您可以简单地将date命令用作date +%Y

wget "http://example.com/data-$(date +%Y).txt"

If you need the 2-digit year, you can simply use date +%y.

如果您需要 2 位数年份,您可以简单地使用date +%y.