To get the current time in a shell script, you can use the date
command. The date
command allows you to display and manipulate the date and time on your system.
Here is an example of how you can use the date
command to get the current time in a shell script:
# Get the current time current_time=$(date +%T) # Print the current time echo "Current time: $current_time"
This will print the current time in the format HH:MM:SS
.
You can also use the date
command to get other information about the current time, such as the current date, the day of the week, or the time zone. For example, to get the current date and time in the format YYYY-MM-DD HH:MM:SS
, you can use the following command:
current_datetime=$(date +%Y-%m-%d\ %T) echo "Current date and time: $current_datetime"