Basta usare datee secondi fidati:
Come giustamente fai notare, molti dettagli sul calcolo sottostante sono nascosti se fai affidamento sull'aritmetica del tempo inglese. Ad esempio -d yesterday, e -d 1 day agoavrà un comportamento diverso.
Invece, puoi affidarti in modo affidabile ai secondi (documentati con precisione) dai tempi dell'UNIX epoca UTC e bash aritmetica per ottenere il momento desiderato:
date -d @$(( $(date +"%s") - 24*3600)) +"%Y-%m-%d"
Questo è stato sottolineato in un'altra risposta . Questo modulo è più portabile su più piattaforme con diversi dateflag della riga di comando, è indipendente dalla lingua (ad es. "Ieri" vs "hier" in locale francese) e francamente (a lungo termine) sarà più facile da ricordare, perché anche tu lo so già. Altrimenti potresti continuare a chiederti: "Era -d 2 hours agoo di -d 2 hour agonuovo?" o "È -d yesterdayo -d 1 day agoquello che voglio?"). L'unico aspetto difficile qui è il @.
Armato di bash e nient'altro:
Bash solo su bash, puoi anche ottenere l'ora di ieri, tramite il printf incorporato:
%(datefmt)T
causes printf to output the date-time string resulting from using
datefmt as a format string for strftime(3). The corresponding argu‐
ment is an integer representing the number of seconds since the
epoch. Two special argument values may be used: -1 represents the
current time, and -2 represents the time the shell was invoked.
If no argument is specified, conversion behaves as if -1 had
been given.
This is an exception to the usual printf behavior.
Così,
# inner printf gets you the current unix time in seconds
# outer printf spits it out according to the format
printf "%(%Y-%m-%d)T\n" $(( $(printf "%(%s)T" -1) - 24*3600 ))
o, equivalentemente con una variabile temp (subshell esterno opzionale, ma mantiene pulite le variabili d'ambiente).
(
now=$(printf "%(%s)T" -1);
printf "%(%Y-%m-%d)T\n" $((now - 24*3600));
)
Nota: nonostante la manpage affermi che nessun argomento per il %()Tformatter assumerà un valore predefinito -1, mi sembra di ottenere invece uno 0 (grazie, manuale di Bash versione 4.3.48)