Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash alias create file with current timestamp in filename

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)""

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.

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?

like image 787
flyingL123 Avatar asked Jun 16 '14 03:06

flyingL123


1 Answers

Use single quotes to prevent immediate expansion.

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

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.

like image 64
Ivan X Avatar answered Oct 01 '22 11:10

Ivan X