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.
- With your editor open ./.bashrc, at the end of the file enter
alias epoch='python -c "import time;print(str(time.time()).split(\".\"));"'
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
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 :)