End of line (new line) escapes in bash
Actually, \n
is not really a newline character -- it is an escape sequence that represents a newline (which is just one character in Linux). The \
at the end of a line escapes the actual newline character that you type in using the enter key. You can look at what ASCII values represent different characters using hexdump:
%echo $'\\n'
\n
%echo $'\\n' | hexdump -C
00000000 5c 6e 0a |\n.|
00000003
You will notice that echo printed out 3 characters: \
(5c), n
(6e), and a newline (0a). You will also notice that on the right hand side of the hexdump output, newline shows up as a ".", because it is considered a non-printing character.
Bash: Strip trailing linebreak from output
If your expected output is a single line, you can simply remove all newline characters from the output. It would not be uncommon to pipe to the tr
utility, or to Perl if preferred:
wc -l < log.txt | tr -d '\n'
wc -l < log.txt | perl -pe 'chomp'
You can also use command substitution to remove the trailing newline:
echo -n "$(wc -l < log.txt)"
printf "%s" "$(wc -l < log.txt)"
If your expected output may contain multiple lines, you have another decision to make:
If you want to remove MULTIPLE newline characters from the end of the file, again use cmd substitution:
printf "%s" "$(< log.txt)"
If you want to strictly remove THE LAST newline character from a file, use Perl:
perl -pe 'chomp if eof' log.txt
Note that if you are certain you have a trailing newline character you want to remove, you can use head
from GNU coreutils to select everything except the last byte. This should be quite quick:
head -c -1 log.txt
Also, for completeness, you can quickly check where your newline (or other special) characters are in your file using cat
and the 'show-all' flag -A
. The dollar sign character will indicate the end of each line:
cat -A log.txt
Echo newline in Bash prints literal \n
Use printf
instead:
printf "hello\nworld\n"
printf
behaves more consistently across different environments than echo
.
How to tell bash that the line continues on the next line
The character is a backslash \
From the bash manual:
The backslash character ‘\’ may be used to remove any special meaning
for the next character read and for line continuation.
ANSI escape code weird behavior at end of line
Filling the line with the currently-selected colors is a detail of bce
(back color erase) which could be implemented differently in different terminals—but Linux console and xterm happen to do it this way. It's an FAQ:
- My terminal shows some uncolored spaces (ncurses, see
indn
) - That color scheme is odd, say more? (xterm)
- Who did it? (some comments on the origin of this particular design)
How can I print a newline as \n in Bash?
Here's my solution:
sed 's/$/\\n/' | tr -d '\n'
How can I have a newline in a string in sh?
If you're using Bash, you can use backslash-escapes inside of a specially-quoted $'string'
. For example, adding \n
:
STR=$'Hello\nWorld'
echo "$STR" # quotes are required here!
Prints:
Hello
World
If you're using pretty much any other shell, just insert the newline as-is in the string:
STR='Hello
World'
Bash recognizes a number of other backslash escape sequences in the $''
string. Here is an excerpt from the Bash manual page:
Words of the form $'string' are treated specially. The word expands to
string, with backslash-escaped characters replaced as specified by the
ANSI C standard. Backslash escape sequences, if present, are decoded
as follows:
\a alert (bell)
\b backspace
\e
\E an escape character
\f form feed
\n new line
\r carriage return
\t horizontal tab
\v vertical tab
\\ backslash
\' single quote
\" double quote
\nnn the eight-bit character whose value is the octal value
nnn (one to three digits)
\xHH the eight-bit character whose value is the hexadecimal
value HH (one or two hex digits)
\cx a control-x character
The expanded result is single-quoted, as if the dollar sign had not
been present.
A double-quoted string preceded by a dollar sign ($"string") will cause
the string to be translated according to the current locale. If the
current locale is C or POSIX, the dollar sign is ignored. If the
string is translated and replaced, the replacement is double-quoted.
Bash: How to end printf or echo with a \ and then a new line
You need more slashes!
printf 'This is line one. \\\nThis is line two.'
The first slash escapes the second one, then the third one is part of the newline \n
.
Importantly, I've also changed the quotes to single. You could do it with double quotes but it'd look like this:
printf "This is line one. \\\\\nThis is line two."
The slashes used to escape other slashes need escaping themselves, which as I'm sure you will agree, is a mess!
escaping newlines in sed replacement string
Looks like you are on BSD or Solaris. Try this:
[jaypal:~/Temp] echo 'abc' | sed 's/b/\
> /'
a
c
Add a black slash and hit enter and complete your sed
statement.
Related Topics
Find All Zero-Byte Files in Directory and Subdirectories
How to Use Nohup to Run Process as a Background Process in Linux
How to Run Vi on Docker Container
How to Install Crontab on Centos
How to Copy Text from My Xterm Without a Mouse
Check Whether a Certain File Type/Extension Exists in Directory
Linux: Which Process Is Causing "Device Busy" When Doing Umount
Shell Script to Count Files, Then Remove Oldest Files
How to Call Curl Without Using Server-Side Cache
How to Set the Grep After Context to Be "Until the Next Blank Line"
Linux: Set Permission Only to Directories
How to Make Vim Play Typewriter Sound When I Write a Letter
Write-Through Ram Disk, or Massive Caching of File System
Git Forces Refresh Index After Switching Between Windows and Linux
Differencebetween Nptl and Posix Threads