2
0
mirror of https://github.com/chubin/cheat.sheets synced 2024-11-19 03:25:44 +00:00
cheat.sheets/sheets/printf
2020-11-12 03:44:19 +00:00

33 lines
1.5 KiB
Plaintext

# printf
# Format and print data
# This command is typically available as a built-in to many shells, such as the
# Bourne shell and the Bourne Again Shell. However, there also exists a GNU
# alternative, sometimes found over at `/usr/bin/printf`.
# Assign the current date (timestamp style) as a shell variable, using the Bash
# builtin, and make it a suitable filename for a Gzip-compressed Tar archive.
printf -v FileName 'Backup_%(%F_%X)T.tgz' -1
# Simple, feature-full, and portable way by which to echo(1) output to STDOUT.
# Here, the current user's username is displayed, followed by a new line.
printf '%s\n' "$USER"
# Using the Bash builtin, this will output one integer per line, from one to
# one million, in a human-readable kind of way, by appropriately
# comma-separating the units.
printf "%'d\n" {1..1000000}
# Getting these results by using the comma is actually also viable in AWK, but
# you'll likely have to jump through a quotation hoop to get access to it.
# Zero-pad a number in order to maintain a width of 3 characters. It's also
# possible to instead provide a `0` in-place of the hash (`#`).
printf '%#.3d\n' 12
# As above, but instead, space-pad the number. Prefix the `3` with a hyphen
# (`-`) to left-align the number, causing the padding to occur on the right.
printf '%3d\n' 12
# Set a field's spacing by using an integer provided as a variable. This is
# incredibly useful when you're dealing with inconsistent field lengths.
printf '%*s\n' $Integer 'Example Field'