2
0
mirror of https://github.com/chubin/cheat.sheets synced 2024-11-15 06:12:59 +00:00
cheat.sheets/sheets/printf

29 lines
1.3 KiB
Plaintext
Raw Normal View History

2020-03-05 21:33:46 +00:00
# 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.
2020-02-17 01:55:05 +00:00
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.
2020-02-17 01:55:05 +00:00
printf "%'d\n" {1..1000000}
2020-03-05 21:33:46 +00:00
# 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.
2020-02-29 23:36:14 +00:00
2020-03-05 21:33:46 +00:00
# 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 (`#`).
2020-02-29 23:36:14 +00:00
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