Bash, the Bourne-Again SHell, is an incredibly flexible and powerful command language interpreter, commonly used in Unix and Unix-like operating systems. It provides a wide range of features, including the ability to manipulate date and time data. One such application is the ability to obtain yesterday’s date. This may seem like a simple task, but it’s a useful technique that often comes up in script writing and automation tasks.

Advertisement

In this article, we’ll take a practical walkthrough on how to get yesterday’s date in Bash. We’ll cover basic Bash commands, learn about the date command, and finally, explore the process of getting yesterday’s date using Bash scripting.

Understanding the Date Command

Before we dive into getting yesterday’s date, it’s important to first understand the date command in Bash. The date command is used to display or set the system date and time. By default, without any arguments, it displays the current date and time.

date 

The output will be the current date and time, like so:

Tue May 14 23:23:00 PDT 2023

The date command also allows you to format the output. For example, you can display the date in the format YYYY-MM-DD using the following command:

date +"%Y-%m-%d" 

This will output:

2023-05-14

Getting Yesterday’s Date

Now, let’s talk about how to get yesterday’s date. With the -d or --date option of the `date` command, we can display the time described by string, instead of ‘now’. This string can be a relative expression, 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 note that the date command and its -d option may behave differently in 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" 

Getting Other Previous Dates

Apart from getting yesterday’s date, the date command also gives you the flexibility to retrieve any previous date. This can be done by manipulating the relative expression provided to the -d or --date option.

  • For instance, if you want to get the date from 3 days ago, you would run:
    date -d "3 days ago" +"%Y-%m-%d" 
    

    This command will output 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 output the dates of one week, one month, and one year ago, respectively, in the YYYY-MM-DD format.

  • Remember, 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 offer a lot of flexibility in Bash scripting, allowing you to easily manipulate date and time data to suit your needs.

Conclusion

Knowing how to manipulate 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 in which you might need to retrieve yesterday’s date. This article has shown you how to do it in a simple and straightforward way.

Remember, the power of Bash comes from its flexibility and vast range of capabilities. Don’t hesitate to explore more about the date command and other Bash commands. Happy scripting!

Share.
Leave A Reply


Exit mobile version