I do not have much experience in Linux and would like some insight into whether there is a possibility to change a variable within a script before it is automatically started through a Cron Job.

For example, that the variable VORDATE="04%2F15%2F2019" would be changed (automatically) to one week prior to today, present in the variable CURRENTDATE="09%2F09%2F2019".

Thank you!

  • 1
    Is your variable inside the script that you want to run in cron? would you explain more about your script? – Rasool Ziafaty Sep 12 at 10:30
  • Yes, inside. It is a download script, so it downloads data from week to week :) – Bruna B Sep 12 at 11:40

I assume your cron job is going to run a bash script. Your script could use the date command to specify a relative date.

A week in the past

VORDATE=$(date -d "7 days ago")

A week in the future

VORDATE=$(date -d "7 days")

man date for more information.

edit: Howto format output

You can format the output by adding a format string by using the + option, the %<codes> are described in the manual page, to add a literal % use %% here's an example with spaces to aid reading.

$ date -d "7 days ago" +"%m %%2f %d %%2f %Y"
09 %2f 19 %2f 2019
  • 1
    should that be -d "7 days ago" ? – user4556274 Sep 12 at 10:00
  • Oops, I thought @Bruna B wanted a week ahead, yes adding ago is last week, I'll update the answer. – X Tian Sep 12 at 10:20
  • Thank you very much @XTian I will try this way :)) – Bruna B Sep 12 at 11:41
  • @XTian hi, just one more question... it is possible to format the output date? Because it is part of a url, which means I need the 08%2F17%2F2019 format. – Bruna B Sep 12 at 13:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.