To make Bash’s echo print text without a trailing newline, use echo -n "text". For portable shell scripts and more predictable handling of special characters, use printf '%s' "text" instead.
What a newline does
A newline is the line-ending character that moves the cursor to the start of the next line in a terminal. By default, echo prints its arguments and then adds a newline, so the next shell prompt or command output begins on a fresh line. Suppressing that final character lets output continue on the same line.
For example, the command echo "Loading" prints “Loading” and ends the line. The command echo -n "Loading" prints the same word but leaves the cursor immediately after it. If the shell prompt appears next, it may therefore sit beside the text.
Use echo -n in common shells
In Bash and many other shells, -n tells the built-in echo not to append a newline:
echo -n "Enter your name: "
read name
The prompt stays on the current line, allowing the user to type an answer beside it. Without -n, the input begins on the following line.
This option is common, but the behavior of echo is not fully consistent across every shell and system. Some implementations treat options or backslash sequences differently. That makes echo -n convenient for interactive commands, but a less reliable choice when a script must behave the same across different environments.
Why printf is often more reliable
printf gives the script direct control over formatting. It does not add a newline unless the format string contains one:
printf '%s' "Hello"
printf '\n'
The first command prints Hello without a line break. The second explicitly prints a newline. You can combine them when you want the content and line ending on separate, visible parts of the command:
printf '%s\n' "Hello"
For a prompt, use printf without \n:
printf '%s' 'Enter your name: '
read name
The %s format marker means “insert a string.” Passing the text as a separate argument keeps characters in the text from being interpreted as formatting instructions. In particular, avoid building a format string by inserting untrusted or arbitrary text directly into it.
Echo and printf compared
Use echo for quick, simple output when its behavior is suitable for the shell you are using. Choose printf when exact output matters, when you need formatted values, or when writing a portable script.
- Quick terminal output:
echo -n "Working..."is brief and easy to read in common shells. - Portable script output:
printf '%s' "Working..."makes the absence of a newline explicit. - Output with a line break:
printf '%s\n' "Done"requests the newline directly. - Formatted values:
printf 'Count: %d\n' "$count"uses a format marker for an integer.
These commands write to standard output by default. To send output elsewhere, shell redirection applies to both. For example, printf '%s' "text" > file.txt writes the text to a file without adding a trailing newline.
Common problems and details to check
The prompt or next output appears on the same line. That is the expected result when no newline is printed. To start a new line afterward, run printf '\n' or include \n in a later printf format string.
The text appears on a new line anyway. Check whether the command contains another output operation that prints a newline, or whether the text being printed itself includes a line break. A shell prompt may also redraw or reposition itself after commands; that is separate from the output command’s newline behavior.
echo -n prints a surprising result. The command named echo can be a shell built-in or an external utility, and details vary by implementation. If a script depends on exact output, replace it with printf and specify the format explicitly.
Backslashes are treated unexpectedly. Some versions of echo interpret sequences such as \n; others may print them literally or use an option to control interpretation. With printf, the format string controls escapes: printf 'A\nB\n' prints two lines. To print a backslash as ordinary data, pass it through a string argument, for example printf '%s' '\n'.
A newline is not the same as a space. If two pieces of output are printed without a newline, they join directly unless you add a space yourself: printf '%s' 'Hello'; printf '%s' 'world' produces Helloworld. To separate them, include a space in the text or print one explicitly.
Using no-newline output in scripts
Suppressing a newline is useful for prompts, progress indicators, and output that another program expects to receive as one continuous string. For example:
printf 'Progress: %s' 'working'
Whether that is appropriate for a progress display depends on what follows. A script that repeatedly updates a terminal line may use carriage returns or terminal-control behavior, which is distinct from merely omitting a newline. For ordinary text output, add a newline when the logical line is complete; this improves readability in terminals and makes text files easier to inspect.
When writing a value for another command or file, consider the receiving format. Some tools treat a final newline as conventional, while others require an exact byte sequence. If the presence or absence of a newline matters, printf makes the choice visible in the command and is easier to review.
Frequently Asked Questions
How do I use echo without a newline in Bash?
Use echo -n "text". For scripts where exact or portable behavior matters, use printf '%s' "text".
Does echo -n work in every shell?
It works in Bash and many common shells, but echo behavior can vary between shells and systems. Use printf when consistent output is important.
How do I add a newline with printf?
Put \n in the format string, as in printf '%s\n' "text". You can also print a newline separately with printf '\n'.
How can I keep a shell prompt on the same line?
Print the prompt without a newline before reading input, for example printf '%s' 'Name: '; read name.
Does omitting a newline add a space?
No. It simply leaves out the line break. Add a space to the printed text if you want adjacent output separated.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Google Assistant IFTTT Recipes: 7 Useful Automations
- OLED TVs in Dark Rooms: Why They Look Better and When They Don’t
- Philips DST7060/20 7000 Series Iron: Specs & Fit
- Coal Fueled Jet Aircraft: Could It Work?675 L
- Remote Desktop on Raspberry Pi: How to Enable It
- Word Select Last Line in Paragraph?