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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With