Linux: create a python alias that will print epoch time.

Scenario: We need an alias for our shell that will print epoch time upon execution, this will be useful for scripts that will run locally.


  1. With your editor open ./.bashrc, at the end of the file enter
alias epoch='python -c "import time;print(str(time.time()).split(\".\")[0]);"'

What this alias does is:

a. Call python and execute everything after -c parameter

b. Import the time library

c. print the first part of time.time() splited by dot, because time.time() returns epoch as a float

2. Save file and on the shell enter

source ./.bashrc

With this command we reload file bashrc and its aliases

3. Confirm that the alias is loaded and works

By entering alias on the command prompt we get all aliases defined

By entering epoch we can see what our alias prints

And also is a very good idea to examine what exit code our alias returns, most likely as any succesfull command will return 0, but its good to verify this because if we use this in a script and is called withing another command like this epoch && ls and the epoch alias returns !=0 the ls command will not executed. We can verify the exit code with the echo $? which returns the exit code of the last executed command.

I hope you found my article interesting :)

Written by

DevOps engineer, loves Linux, Python, cats and Amiga computers

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store