Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7013717
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:25:38+00:00 2026-05-27T22:25:38+00:00

I am writing a shell script that needs to do some date string manipulation.

  • 0

I am writing a shell script that needs to do some date string manipulation. The script should work across as many *nix variants as possible, so I need to handle situations where the machine might have the BSD or the GNU version of date.

What would be the most elegant way to test for the OS type, so I can send the correct date flags?

EDIT:
To clarify, my goal is to use date’s relative date calculation tools which seem distinct in BSD and GNU.

BSD example

date -v -1d

GNU example

date --date="1 day ago"
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T22:25:39+00:00Added an answer on May 27, 2026 at 10:25 pm

    Use portable flags. The standard is available here

    For the particular problem of printing a relative date, it is probably easier to use perl than date:

    perl -E 'say scalar localtime(time - 86400)'
    

    (note that this solution utterly fails on 23 or 25 hour days, but many perl solutions are available to address that problem. See the perl faq.)

    but you could certainly use a variation of Keith’s idea and do:

    if date -v -1d > /dev/null 2>&1; then
      DATE='date -v -1d'
    else
      DATE='date --date="1 day ago"'
    fi
    eval "$DATE"
    

    or just

    DATE=$(date -v -1d 2> /dev/null) || DATE=$(date --date="1 day ago")
    

    Another idea is to define a function to use:

    if date -v -1d > /dev/null 2>&1; then
        date1d() { date -v -1d; }
    else
        date1d() { date --date='1 day ago'; }
    fi
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a small shell script that needs to reverse the lines of a
I am writing some BASH shell script that will continuously check a file to
I'm writing a shell script that opens a file and needs to find a
I'm writing a shell script that needs to compress a dir with the same
I am writing a shell script that takes file paths as input. For this
I am writing a POSIX shell script that may or may not receive input
I'm writing a shell script to do some web server configuration. I need to
I am writing a shell script (which I suck at) and I need some
I'm writing a shell script that makes sure my DNS server is looking. Here's
I am writing a Bash shell script for Mac that sends an email notification

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.