LinuxCommandLibrary

print.zsh

Print arguments to standard output

TLDR

Print input

$ print "Hello" "World"
copy

Print separated by newline(s)
$ print -l "Line1" "Line 2" "Line3"
copy

Print without trailing newline
$ print -n "Hello"; print "World"
copy

Enable backslash escapes
$ print -e "Line 1\nLine2"
copy

Print arguments as described by printf (for greater portability across shells, consider using the printf command instead)
$ print -f "%s is %d years old.\n" "Alice" 30
copy

SYNOPSIS

print [-adlmnNPruRsZ] [-f format] [-v var] [argument ...]

PARAMETERS

-a
    Interpret backslash escape sequences, but disable other special handling.

-d
    Removes trailing directory components from each argument before printing.

-l
    Put each argument on a separate line.

-m
    Merge all arguments into a single string, separated by spaces. Backslash escapes are then interpreted.

-n
    Do not append a newline to the output.

-N
    Suppresses backslash expansion.

-P
    Interpret backslash escape sequences in the same way as the `echo` command in other shells, if the `ECHO_STYLE_ESCAPES` option is set. Otherwise, it is the same as `-R`.

-r
    Print arguments raw, without interpreting backslash escapes, even if the `ECHINALIAS` option is set. It's the opposite of `-e` or `-P`.

-R
    Print arguments raw, same as -r.

-s
    Save the output into the history list.

-u
    Write to file descriptor n instead of standard output. n can be {varname}.

-v var
    Assign the output to the variable 'var' instead of displaying it.

-Z
    The same as the `-m` option, but it only prints non-empty strings that result from the expansion.

-f format
    Use the `printf` format to print.

[argument ...]
    The text or variables to be displayed. These are concatenated by spaces and outputted.

DESCRIPTION

The `print` command in Zsh is a built-in utility for displaying text to standard output. It's similar to `echo` in other shells but offers more features and flexibility, especially in formatting and handling escape sequences. `print` is crucial for scripting when you need to display information, variables, or messages to the user.

It can handle simple strings, variable expansion, and special characters. `print` does not automatically append a newline character, which can be both a benefit and a consideration for scripting. Using options like `-n` allows you to omit the newline, while other options allow enabling interpretation of backslash escapes. The command is a powerful tool for controlling the presentation of output from within Zsh scripts.

CAVEATS

Unlike `echo`, `print` doesn't automatically add a newline.
Backslash escapes interpretation depends on options and Zsh configuration.

VARIABLE ASSIGNMENT

The `-v var` option is useful for capturing the output of `print` into a variable for further processing within the script. This is a powerful feature for manipulating strings and generating dynamic content.

FILE DESCRIPTOR OUTPUT

The `-u n` option enables you to redirect the output to a specific file descriptor. This is useful for debugging (e.g., sending output to standard error) or for inter-process communication.

HISTORY

The `print` command has been part of Zsh since its early development. It provides a built-in mechanism for outputting text, allowing finer control compared to using external commands like `/bin/echo`. Its options and behavior have evolved to align with Zsh's overall scripting philosophy.

SEE ALSO

echo(1), printf(1), cat(1)

Copied to clipboard