Skip to main content
Mention GNU upfront. Clarify shell compatibility without OS and version specific details.
Source Link
RobertL
  • 6.9k
  • 1
  • 22
  • 39

@Jasen's elegant answer inspired me to think of this variation, also requiring GNU date:

#!/bin/sh D=${1-10152015} YYYY=${D#????} MMDD=${D%????} date -d"$YYYY$MMDD yesterday" +%m%d%Y 

This uses the POSIX compliant Parameter Expansion substring processing with pattern matching notation, thus also runs under /bin/sh (assuming /bin/sh is aany POSIX shell, on Solaris 10 and below, use /usr/xpg4/bin/sh) as well as bash. It still assumes GNU date.

This could also be accomplished without the intermediate variables YYYY and MMDD, but I think they help to clearly document the input and output formats. They give the observer an extra symbolic clue about what's being done, without the addition of extra logic. Especially helpful because I think this notation is slightly more cryptic than the bash version.

@Jasen's elegant answer inspired me to think of this variation:

#!/bin/sh D=${1-10152015} YYYY=${D#????} MMDD=${D%????} date -d"$YYYY$MMDD yesterday" +%m%d%Y 

This uses the POSIX compliant Parameter Expansion substring processing with pattern matching notation, thus also runs under /bin/sh (assuming /bin/sh is a POSIX shell, on Solaris 10 and below, use /usr/xpg4/bin/sh) as well as bash. It still assumes GNU date.

This could also be accomplished without the intermediate variables YYYY and MMDD, but I think they help to clearly document the input and output formats. They give the observer an extra symbolic clue about what's being done, without the addition of extra logic. Especially helpful because I think this notation is slightly more cryptic than the bash version.

@Jasen's elegant answer inspired me to think of this variation, also requiring GNU date:

#!/bin/sh D=${1-10152015} YYYY=${D#????} MMDD=${D%????} date -d"$YYYY$MMDD yesterday" +%m%d%Y 

This uses the POSIX compliant Parameter Expansion substring processing with pattern matching notation, thus also runs under any POSIX /bin/sh as well as bash.

This could also be accomplished without the intermediate variables YYYY and MMDD, but I think they help to clearly document the input and output formats. They give the observer an extra symbolic clue about what's being done, without the addition of extra logic. Especially helpful because I think this notation is slightly more cryptic than the bash version.

/bin/sh is not necessarily POSIX, still assumes GNU date.
Source Link
Stéphane Chazelas
  • 586.4k
  • 96
  • 1.1k
  • 1.7k

@Jasen's elegant answer inspired me to think of this variation:

#!/bin/sh D=${1-10152015} YYYY=${D#????} MMDD=${D%????} date +%m%d%Y -d"$YYYY$MMDD yesterday" +%m%d%Y 

This uses the POSIX compliant Parameter Expansion substring processing with pattern matching notation, thus also runs under /bin/sh (assuming /bin/sh is a POSIX shell, on Solaris 10 and below, use /usr/xpg4/bin/sh) as well as bash. It still assumes GNU date.

This could also be accomplished without the intermediate variables YYYY and MMDD, but I think they help to clearly document the input and output formats. They give the observer an extra symbolic clue about what's being done, without the addition of extra logic. Especially helpful because I think this notation is slightly more cryptic than the bash version.

@Jasen's elegant answer inspired me to think of this variation:

#!/bin/sh D=${1-10152015} YYYY=${D#????} MMDD=${D%????} date +%m%d%Y -d"$YYYY$MMDD yesterday" 

This uses the POSIX compliant Parameter Expansion substring processing with pattern matching notation, thus also runs under /bin/sh as well as bash.

This could also be accomplished without the intermediate variables YYYY and MMDD, but I think they help to clearly document the input and output formats. They give the observer an extra symbolic clue about what's being done, without the addition of extra logic. Especially helpful because I think this notation is slightly more cryptic than the bash version.

@Jasen's elegant answer inspired me to think of this variation:

#!/bin/sh D=${1-10152015} YYYY=${D#????} MMDD=${D%????} date -d"$YYYY$MMDD yesterday" +%m%d%Y 

This uses the POSIX compliant Parameter Expansion substring processing with pattern matching notation, thus also runs under /bin/sh (assuming /bin/sh is a POSIX shell, on Solaris 10 and below, use /usr/xpg4/bin/sh) as well as bash. It still assumes GNU date.

This could also be accomplished without the intermediate variables YYYY and MMDD, but I think they help to clearly document the input and output formats. They give the observer an extra symbolic clue about what's being done, without the addition of extra logic. Especially helpful because I think this notation is slightly more cryptic than the bash version.

Source Link
RobertL
  • 6.9k
  • 1
  • 22
  • 39

@Jasen's elegant answer inspired me to think of this variation:

#!/bin/sh D=${1-10152015} YYYY=${D#????} MMDD=${D%????} date +%m%d%Y -d"$YYYY$MMDD yesterday" 

This uses the POSIX compliant Parameter Expansion substring processing with pattern matching notation, thus also runs under /bin/sh as well as bash.

This could also be accomplished without the intermediate variables YYYY and MMDD, but I think they help to clearly document the input and output formats. They give the observer an extra symbolic clue about what's being done, without the addition of extra logic. Especially helpful because I think this notation is slightly more cryptic than the bash version.