dylanaraps / pure-bash-bible

📖 A collection of pure bash alternatives to external processes.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Typo in function date

MegaV0lt opened this issue · comments

I think there is an extra '\' in the printf command

date() {
    # Usage: date "format"
    # See: 'man strftime' for format.
    printf "%($1)T\\n" "-1"
}
commented

@MegaV0lt since the printf format string uses double quotes instead of single-quotes, parameter expansion and command substitutions will be applied, as well as backslash escapes - twice. Hence the \\n is reduced to \n when you declare the function and printf ... prints a \n.

Since someone may go ahead and stupidly copy-paste this in like

cat > my_source-able_file.sh <<EOF
date() {
    # Usage: date "format"
    # See: 'man strftime' for format.
    printf "%($1)T\\n" "-1"
}
EOF

... they'll still get a working function even if they make that mistake.