Bash, the Bourne-Again Shell, is a very flexible and powerful command language interpreter used in Unix and Unix-like operating systems. It can do many things, including handling date and time information. One useful task is getting yesterday’s date. This may seem simple, but it is very helpful in script writing and automation tasks.
In this article, we’ll learn how to get yesterday’s date in Bash. We’ll start with basic Bash commands, learn about the date command, and finally, get yesterday’s date using Bash scripting.
The date Command
Before we get yesterday’s date, we need to understand the date command in Bash. The date command shows or sets the system date and time. By default, it shows the current date and time when run without any arguments.
date
The output will be the current date and time, like this:
Tue May 14 23:23:00 PDT 2023
The date command also lets you format the output. For example, to display the date in the format YYYY-MM-DD, use this command:
date +"%Y-%m-%d"
This will output:
2023-05-14
Getting Yesterday’s Date
Now, let’s learn how to get yesterday’s date. Using the -d
(or --date
) option of the date command, we can show the time described by a string, instead of ‘now’. This string can be something like ‘1 day ago’.
date -d "1 day ago" +"%Y-%m-%d"
This will output the date of one day ago in the format YYYY-MM-DD:
2023-05-13
It’s important to know that the date command and its -d option may work differently on different Unix-like systems. For example, on a macOS system, you would use -v-1d
to get yesterday’s date:
date -v-1d +"%Y-%m-%d"
Storing Yesterday’s Date in a Variable
Sometimes, you might want to store yesterday’s date in a variable for later use in your script. You can do this by using command substitution:
Y_DATE=$(date -d "1 day ago" +"%Y-%m-%d")
This command saves yesterday’s date in the variable Y_DATE. You can then use this variable in your script as needed.
Getting Other Previous Dates
Besides getting yesterday’s date, the date command also allows you to get any previous date by changing the relative expression given to the -d or –date option.
- For example, to get the date from 3 days ago, use:
date -d "3 days ago" +"%Y-%m-%d"
This command will show the date of three days ago in the format YYYY-MM-DD.
- Similarly, you can get the date of a week ago, a month ago, or even a year ago:
date -d "1 week ago" +"%Y-%m-%d"
date -d "1 month ago" +"%Y-%m-%d"
date -d "1 year ago" +"%Y-%m-%d"
These commands will show the dates of one week, one month, and one year ago, respectively, in the YYYY-MM-DD format.
- On macOS, you would use `-v-3d` to get the date from 3 days ago, `-v-1w` to get the date from 1 week ago, `-v-1m` to get the date from 1 month ago, and `-v-1y` to get the date from 1 year ago:
date -v-3d +"%Y-%m-%d"
date -v-1w +"%Y-%m-%d"
date -v-1m +"%Y-%m-%d"
date -v-1y +"%Y-%m-%d"
These commands give you a lot of flexibility in Bash scripting, allowing you to easily handle date and time data.
Conclusion
Knowing how to handle date and time data in Bash scripting is a valuable skill. Whether it’s for logging, report generation, or creating backup files, there are many situations where you might need to get yesterday’s date. This article has shown you how to do it in a simple and easy way.
Remember, the power of Bash comes from its flexibility and many features. Don’t hesitate to learn more about the date command and other Bash commands. Happy scripting!