Using date to get tomorrows date in Bash

2019-06-22 16:27发布

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"

标签: bash shell unix
7条回答
不美不萌又怎样
2楼-- · 2019-06-22 16:49

You can try below

#!/bin/bash
today=`date`
tomorrow=`date --date="next day"`
echo "$today"
echo "$tomorrow"
查看更多
不美不萌又怎样
3楼-- · 2019-06-22 17:02

Set your timezone, then run date.

E.g.

TZ=UTC-24 date 

Alternatively, I'd use perl:

perl -e 'print localtime(time+84600)."\n"'
查看更多
混吃等死
4楼-- · 2019-06-22 17:04
echo $(date --date="next day" +%Y%m%d)

This will output

20170623

查看更多
Luminary・发光体
5楼-- · 2019-06-22 17:08

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.

查看更多
等我变得足够好
6楼-- · 2019-06-22 17:09

$(...) 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.

查看更多
【Aperson】
7楼-- · 2019-06-22 17:10

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

date -d '+1 day'
查看更多
登录 后发表回答