Using date to get tomorrows date in Bash

2019-06-22 16:36发布

问题:

I want to write a bash script that will run on a given but process data with next days date, My current approach is to get the unix time stamp and add a days worth of seconds to it, but I cant get it working, and haven't yet found what I'm looking for online.

Here's what I've tried, I feel like the problem is that its a string an not a number, but I dont know enough about bash to be sure, is this correct? and how do I resolve this?

today="$(date +'%s')"

tomorrow="$($today + 86400)"

echo "$today"

echo "$tomorrow"

回答1:

$(...) is command substitution. You're trying to run $today + 86400 as a command.

$((...)) is arithmetic expansion. This is what you want to use.

tomorrow=$(( today + 86400 ))

Also see http://mywiki.wooledge.org/ArithmeticExpression for more on doing arithmetics in the shell.



回答2:

If you have gnu-date then to get next day you can just do:

date -d '+1 day'


回答3:

Some of the answers for this question depend on having GNU date installed. If you don't have GNU date, you can use the built-in date command with the -v option.

The command

$ date -v+1d

returns tomorrow's date.

You can use it with all the standard date formatting options, so

$ date -v+1d +%Y-%m-%d

returns tomorrow's date in the format YYYY-MM-DD.



回答4:

I hope that this will solve your problem here.

 date --date 'next day'


回答5:

Set your timezone, then run date.

E.g.

TZ=UTC-24 date 

Alternatively, I'd use perl:

perl -e 'print localtime(time+84600)."\n"'


回答6:

echo $(date --date="next day" +%Y%m%d)

This will output

20170623



回答7:

You can try below

#!/bin/bash
today=`date`
tomorrow=`date --date="next day"`
echo "$today"
echo "$tomorrow"


标签: bash shell unix