To get the current time in hours and minutes in Linux bash, you can use the date
command and specify the %H
and %M
format options to print the hour and minute respectively.
For example:
date +"%H:%M"
result:
15:34
This will output the current hour and minute in the format HH:MM
.
Note that this will print the time in the local timezone of the system on which the command is run.
To print the time in a different timezone, you can use the TZ environment variable. For example:
TZ='Europe/Paris' date +"%H:%M"
result:
14:34
This will print the current time in Paris, Europe.
More ways to get time in Bash
date +"%m %d %Y %H:%M:%S"
Get the current time with date and seconds:
01 04 2023 15:35:57
Use awk to get current time in Linux
date|awk '{print $4}'
Get the current time with seconds:
15:36:34
Get current time in Bash - printf
To get current time in bash with printf
we can use:
printf "%(%H:%M)T\n"
This prints locale time - hour and minute:
15:38