Output
Print text to the pipeline with optional escape sequence expansion.
Syntax
Section titled “Syntax”echo [OPTIONS] [STRING...]| Flag | Description | Example |
|---|---|---|
-n | Suppress trailing newline | echo -n 'no newline' |
-e | Enable interpretation of escape sequences | echo -e 'line1\nline2' |
-E | Disable interpretation of escape sequences (default) | echo -E 'literal \n' |
Escape sequences (-e)
Section titled “Escape sequences (-e)”When -e is active, the following sequences are expanded:
| Sequence | Meaning |
|---|---|
\n | Newline |
\t | Horizontal tab |
\r | Carriage return |
\a | Alert (bell) |
\b | Backspace |
\f | Form feed |
\v | Vertical tab |
\\ | Literal backslash |
Return type
Section titled “Return type”PsBash.TextOutput with a single property:
| Property | Type | Description |
|---|---|---|
BashText | string | The formatted output text (includes trailing newline unless -n is used) |
Examples
Section titled “Examples”Basic output:
echo 'Hello, world!'Hello, world!Multiple arguments are joined with spaces:
echo one two threeone two threeSuppress trailing newline with -n:
echo -n 'prompt> 'prompt>The output has no trailing newline, so subsequent output appears on the same line.
Escape sequences with -e:
echo -e 'column1\tcolumn2\nrow1\trow2'column1 column2row1 row2Access the object programmatically:
$out = echo -n 'hello'$out.BashText # "hello"$out.BashText.Length # 5Comparison
Section titled “Comparison”echo -e 'hello\tworld'# hello worldBash echo behavior varies across implementations (GNU coreutils vs built-in). The -e flag is not POSIX-standard; some shells enable escapes by default.
echo -e 'hello\tworld'# hello worldPsBash echo defaults to -E (no escape expansion), matching GNU coreutils behavior. The -e flag must be explicit.
Write-Output "hello`tworld"# hello worldNative PowerShell uses backtick escapes (`t, `n) inside double-quoted strings. Write-Output has no flag-based escape control.
printf
Section titled “printf”Format and print text using C-style format specifiers.
Syntax
Section titled “Syntax”printf FORMAT [ARGUMENTS...]The FORMAT string is required. If no ARGUMENTS are provided, format specifiers consume nothing and produce empty output for that position.
Format specifiers
Section titled “Format specifiers”| Specifier | Description | Example |
|---|---|---|
%s | String | printf '%s' 'hello' produces hello |
%d | Decimal integer | printf '%d' 42 produces 42 |
%f | Floating-point (6 decimal places) | printf '%f' 3.14 produces 3.140000 |
%% | Literal percent sign | printf '100%%' produces 100% |
Escape sequences in FORMAT
Section titled “Escape sequences in FORMAT”The format string automatically expands the same escape sequences as echo -e:
| Sequence | Meaning |
|---|---|
\n | Newline |
\t | Horizontal tab |
\r | Carriage return |
\a | Alert (bell) |
\b | Backspace |
\f | Form feed |
\v | Vertical tab |
\\ | Literal backslash |
Argument type conversion
Section titled “Argument type conversion”printf automatically converts string arguments to the type required by the format specifier:
- Arguments that parse as integers are passed as
[int]to%d - Arguments that parse as decimals are passed as
[double]to%f - All other arguments remain as
[string]
Return type
Section titled “Return type”PsBash.TextOutput with a single property:
| Property | Type | Description |
|---|---|---|
BashText | string | The formatted output text (no automatic trailing newline) |
Examples
Section titled “Examples”Simple string formatting:
printf 'Hello, %s!\n' 'world'Hello, world!Multiple arguments with mixed types:
printf 'Name: %s, Age: %d, Score: %f\n' 'Alice' 30 95.5Name: Alice, Age: 30, Score: 95.500000Tab-separated table:
printf '%s\t%s\t%s\n' 'Name' 'Role' 'Team'printf '%s\t%s\t%s\n' 'Alice' 'Dev' 'Platform'printf '%s\t%s\t%s\n' 'Bob' 'QA' 'Platform'Name Role TeamAlice Dev PlatformBob QA PlatformLiteral percent sign:
printf 'Disk usage: %d%%\n' 73Disk usage: 73%Access the object programmatically:
$out = printf '%s-%d' 'build' 42$out.BashText # "build-42"Error handling
Section titled “Error handling”Calling printf with no arguments throws an error:
printf# printf: usage: printf format [arguments]Comparison
Section titled “Comparison”printf 'Name: %s, Count: %d\n' 'items' 5# Name: items, Count: 5Bash printf supports the full POSIX set of specifiers including %x, %o, %e, %g, width modifiers, and precision. It also reuses the format string if there are more arguments than specifiers.
printf 'Name: %s, Count: %d\n' 'items' 5# Name: items, Count: 5PsBash printf supports %s, %d, %f, and %%. Unlike bash, it does not cycle the format string over excess arguments.
'Name: {0}, Count: {1}' -f 'items', 5# Name: items, Count: 5PowerShell uses .NET composite format strings with {0}, {1} placeholders and the -f operator. This supports full .NET format specifiers like {0:N2}, {1:X}, and padding via {0,-10}.