文件名带有当前时间戳

文件名带有当前时间戳

本文介绍了Bash别名创建文件,文件名带有当前时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个bash别名,该别名将在每次调用别名时打印当前的UNIX时间戳.我的bash个人资料中包含以下内容:

I am trying to create a bash alias that will print the current UNIX timestamp every time the alias is called. I have the following in my bash profile:

alias unix="echo "$(date +%s)""

但是,似乎当前的unix时间戳是在bash配置文件来源后立即存储的,并且每次调用别名时都不会更改.

However, it seems that the current unix timestamp is being stored as soon as the bash profile is sourced, and it doesn't change each time the alias is called.

例如,如果我以10秒间隔三次调用unix别名,则输出始终是相同的.如何创建一个别名,该别名将在调用别名时评估unix时间戳.这可能吗?

For example, if I call the unix alias three times 10 seconds apart, the output is always the same. How can I create an alias that will evaluate the unix timestamp at the time the alias is called. Is this possible?

推荐答案

使用单引号防止立即扩展.

Use single quotes to prevent immediate expansion.

alias unix='echo $(date +%s)'

更新:虽然我很高兴能够解释单引号和双引号之间的不同扩展行为,但也请参阅Robby Cornelissen的另一个答案,以获得更有效的方法来获得相同的结果.在这里echo是不必要的,因为它仅输出date本身已经输出的内容.因此,date不需要在子shell中运行.

Update: While I'm happy to have been able to explain the different expansion behavior between single and double quotes, please also see the other answer, by Robby Cornelissen, for a more efficient way to get the same result. The echo is unnecessary here since it only outputs what date already would output by itself. Therefore, date doesn't need to be run in a subshell.

这篇关于Bash别名创建文件,文件名带有当前时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 13:21