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

前端 未结 17 1439
谎友^
谎友^ 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:26

    If you have access to python, this is a helper that will get the yyyy-mm-dd date value for any arbitrary n days ago:

    function get_n_days_ago {
      local days=$1
      python -c "import datetime; print (datetime.date.today() - datetime.timedelta(${days})).isoformat()"
    }
    
    # today is 2014-08-24
    
    $ get_n_days_ago 1
    2014-08-23
    
    $ get_n_days_ago 2
    2014-08-22
    

提交回复
热议问题