Skip to content

Instantly share code, notes, and snippets.

@ff4500

ff4500/myscript_v03_printf.sh Secret

Created Jan 23, 2020
Embed
What would you like to do?
#!/usr/bin/env bash
filepath="$HOME/Desktop/"
filename="mysecondnewfile.txt"
printf "I'm a printf statement. Enjoy your new file - now with content!"
printf "This is my test file." > "${filepath}${filename}"
# Unlike echo, printf needs an argument
# ==========================================
#
# printf arguments:
# ------------------------------------------
# %c - character
# %d - decimal (integer) number (base 10)
# %e - exponential floating-point number
# %f - floating-point number
# %i - integer (base 10)
# %o - octal number (base 8)
# %s - a string of characters
# %u - unsigned decimal (integer) number
# %x - number in hexadecimal (base 16)
# %% - print a percent sign
# \% - print a percent sign
# These don't print anything to the console
# ------------------------------------------------------------------------
printf ""
printf "\n"
# This prints an empty line to the console
# ------------------------------------------------------------------------
printf "%s\n"
# This will throw an error of 'invalid option', because printf
# is expecting an argument
# ------------------------------------------------------------------------
printf "------------------------------------------------------"
# ====================================================================== #
# We're going to put some echoes in each examples below, just for
# the simplicity of making a new line. It will make the prinf output
# a little bit more obvious
# ====================================================================== #
# Example 1 - regular printf command
# Each "\n" will actually create a new line
#
# Otherwise, printf statements will continue where previous prinf left
# off, as seen on the third and fourth lines. There is no space between
# the third and fourth line outputs.
# ------------------------------------------------------------------------
echo
printf "I'm a line of text, you\nknow... for testing.\n"
printf "I'm a second line of text."
echo
printf "This is a third line of example text."
printf "And this is the fourth line, which is pretty great."
echo
# Example 2
# printf using a string argument with \n (new line)
#
# This will print all \n instances as literal inside of the quotes. The
# \n specified in the printf statement will automatically insert the new
# line at the end of the printf statement for that specific line.
# ------------------------------------------------------------------------
echo
printf '%s\n' "------------------------------------------------------"
printf '%s\n' "I'm a line of text,\nyou know... for testing.\n"
printf '%s\n' "I'm a second line of text."
printf '%s\n' "------------------------------------------------------"
echo
# Example 3
# printf (without arguments), printing a variable
#
# This will print our variable "line" before and after the lines of text
# Notice and we're using the %s (string) before our divider dashed line.
# If we don't do this, printf will try to use our dashes as an argument
# delimiter and will throw an error. We're also adding a \n to add a new
# line at the end of our variable.
# ------------------------------------------------------------------------
line="%s------------------------------------------------------\n"
echo
printf "${line}"
printf "I'm a line of text,\nyou know... for testing.\n"
printf "I'm a second line of text."
printf "${line}"
echo
# Example 4
# printf to equal our original echo example
#
# In order to replicate Example 4 in myscript_v03_echo.sh using a printf
# statement, we'll have to do the following:
# ------------------------------------------------------------------------
printf "\n"
printf "%s------------------------------------------------------\n"
printf "I'm a line of text,\nyou know... for testing.\n"
printf '%s\n' "I'm a second line of text."
printf "%s------------------------------------------------------\n"
printf "\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.