In the world of system administration and shell scripting, handling dates and times is very important. Whether you’re scheduling tasks, rotating logs, or simply adding timestamps to files, knowing how to work with dates in your scripts can save you a lot of time and effort. Bash, the Bourne Again Shell, has powerful tools for this purpose.
In this article, we’ll show you a simple example: how to get tomorrow’s date using Bash. There are several ways to do this, and we’ll go through a few examples using basic tools like date and GNU date.
The Date Command in Bash
The date command in Bash is a versatile tool that lets you format and change dates. By default, it shows the current date and time when you run it without any arguments.
date
Wed May 15 10:34:12 UTC 2023
The real power of the date command comes from its options for formatting and changing dates. By using the -d (or --date
) option, you can specify a different date instead of the current date. This can be a specific date like “2023-05-15” or a relative date like “yesterday” or “next week”.
Getting Tomorrow’s Date
To get tomorrow’s date, you can use the date command with the -d
option and the string “tomorrow”.
date -d "tomorrow"
Thu May 16 10:34:12 UTC 2023
This command will show the date and time 24 hours from now. If you only want the date, you can use the + option to format the output:
date -d "tomorrow" '+%Y-%m-%d'
2023-05-16
The +%Y-%m-%d
option tells date to format the output as “YYYY-MM-DD”, which is a common and useful date format.
Store Result in Variable
While working with the bash scripting, many times we need to store dates in a variables for the further uses. The following sample shell script will hep you to store the tomorrows days in a variable.