In a unix shell, how to get yesterday's date into a variable?

前端 未结 17 1436
谎友^
谎友^ 2020-12-04 21:51

I\'ve got a shell script which does the following to store the current day\'s date in a variable \'dt\':

date \"+%a %d/%m/%Y\" | read dt
echo ${dt}
         


        
17条回答
  •  执念已碎
    2020-12-04 22:23

    You can use GNU date command as shown below

    Getting Date In the Past

    To get yesterday and earlier day in the past use string day ago:

    date --date='yesterday'

    date --date='1 day ago'

    date --date='10 day ago'

    date --date='10 week ago'

    date --date='10 month ago'

    date --date='10 year ago'

    Getting Date In the Future

    To get tomorrow and day after tomorrow (tomorrow+N) use day word to get date in the future as follows:

    date --date='tomorrow'

    date --date='1 day'

    date --date='10 day'

    date --date='10 week'

    date --date='10 month'

    date --date='10 year'

提交回复
热议问题