How to Display Yesterday’s Date with Bash Script

In the world of scripting, having the ability to manipulate dates can be quite useful. With a simple Bash script, you can easily display yesterday’s date. This can be particularly helpful in various applications, such as logging, automation tasks, or simply for personal tracking. In this article, we will cover the prerequisites, the script itself, and how to run it effectively.

Prerequisites

  • Basic understanding of Bash scripting
  • Knowledge of date command
  • Familiarity with variables and echo commands

DID YOU KNOW?

The date command in Linux can format and display time, date, and even time zones using various flags.

The Script

Here is a simple Bash script that captures and displays yesterday’s date. It’s straightforward and utilizes the date command with some formatting options to get the desired output.

#!/bin/bash
yesterday=$(date -d "yesterday" +"%Y-%m-%d")
echo "Yesterday's date was: $yesterday"

Step-by-Step Explanation

NOTE!

This script utilizes date with the -d option, which is supported in many GNU/Linux distributions.

Let’s break down the script step by step:

  1. Shebang: The line #!/bin/bash tells the system which interpreter to use for executing the script.
  2. Variable Assignment: The line yesterday=$(date -d "yesterday" +"%Y-%m-%d") assigns the output of the date command to the variable yesterday.
  3. Output: Finally, echo is used to display the result in a formatted string.

How to Run the Script

Once you have the script ready, follow these simple steps to run it:

  1. Open your terminal.
  2. Create a new file: nano display_yesterday.sh or use your preferred text editor.
  3. Paste the script into the file and save it.
  4. Make the script executable by running: chmod +x display_yesterday.sh.
  5. Run the script with: ./display_yesterday.sh.

Conclusion

Displaying yesterday’s date using a Bash script is an easy task with the date command. This simple script can be expanded further to include functionalities like logging or notifications based on the date. With the steps outlined in this article, you can implement it in your projects or personal tasks with ease.

FAQ

  1. What if my `date` command does not support the `-d` option?

    You may be using a different version of the date command. Make sure you are on a GNU/Linux system or refer to the documentation for your system.

  2. Can this script be modified to display different date formats?

    Yes! You can change the format in the +"%Y-%m-%d" part of the command to other formats like +"%d/%m/%Y".

  3. Is it possible to display yesterday’s date in a different timezone?

    Yes, you can set the timezone by using the TZ environment variable before the date command.

  4. What file permissions should I set for the script?

    Setting it to executable using chmod +x is generally sufficient.

  5. Can I schedule this script to run automatically?

    Absolutely! You can use cron jobs to schedule the execution of this script at regular intervals.

Troubleshooting

If you encounter issues while running the script, check the following common errors:

  • Command not found: Ensure that you have Bash installed and properly configured.
  • Syntax error: Double-check your script for any typos or syntax mistakes.
  • Permission denied: Make sure you have made the script executable with chmod +x.