2

This is the file name in question OD_Orders_2019-02-19.csv

I am trying to create a bash script to read into files with yesterday's date on the file name while retaining the rest of the files name. I would like for $y to equal the name of the file with a formula output with the exact date in the middle. So far this is what I have:

To get yesterday's date in the correct format, I made x:

x="date -d yesterday +%Y-%m-%d"

y="OD_Orders_$x.csv"

This issue with this is that the results when I do a echo $y gives me this: OD_Orders_date -d yesterday +%Y-%m-%d.csv

I need to show the value of $x when echo $y correctly without it showing the formula. I need $y to input the filename with yesterday's date in the correct format because I want to use it more later on within this script.

2

You need to use command substitution $( ... ) to substitute the output of the command instead of the command itself:

x="$(date -d yesterday +%Y-%m-%d)"
y="OD_Orders_${x}.csv"

Which of course could be simplified to:

y="OD_Orders_$(date -d yesterday +%Y-%m-%d).csv"
  • You Sir are the MAN! Thank you very much! I was up all night for hours and couldn't solve this. – Ibrahim A Feb 20 at 15:36

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.