As the title says, I want to eliminate trailing newlines and store the result in variables (Bash).
The following does not work.
[xxx@ ~]$ var=`echo 'abc\ndef\n' | sed 's/\\n$//g'`
[xxx@ ~]$ echo $var
abc\ndef\n
The following works!
[xxx@ ~]$ var=`echo 'abc\ndef\n' | sed 's/\\\\n$//g'`
[xxx@ ~]$ echo $var
abc\ndef
I thought that only one backslash escape was needed for sed since it is enclosed in single quotes, but I guess my understanding is wrong.
Please tell me why the second command works correctly?
Regarding:
This command produces the shown output:
As for why you get this unexpected output with old, deprecated backticks:
which is different from this expected output with modern command substitution (
$(...)):it's because backslashes (
\) inside backticks are handled in a non-obvious manner as stated and demonstrated at the top of the related FAQ, https://mywiki.wooledge.org/BashFAQ/082, and that is one of the reasons to use$(...)instead of backticks to execute commands.So, if you want to remove a trailing
\nstring using a pipe tosedthen do this:otherwise, if you really want to remove newlines as your Subject line says then read on:
echo 'abc\ndef\n'produces this output:The only newline present in that output is the one at the end of the line that makes that a valid POSIX text file. The
\ns are literally the 2 characters\and\n.You should use
printfinstead ofechofor portability and consistency. In this case it would convert\nstrings to newlines:Now, to get RID of the newlines - sed by default reads input 1 line at a time so you can't remove a newline from a string that sed sees as there cannot be a newline within 1 line that's part of newline-separated input. You can do things with sed to make it read multiple lines but it's more robust and portable to use awk instead, e.g.:
to remove just the final newline:
or to remove all newlines:
or to remove all except the final newline:
or similar depending on whether you want the string to end in a newline (and so be a valid POSIX text line/file) or not.
To execute a command, store it's result in a variable, and then print the contents of that variable, one way using command substitution would be:
Note the use of
$(...)instead of the long deprecated backticks (https://mywiki.wooledge.org/BashFAQ/082), and the necessary quotes"..."around the shell variable (see https://mywiki.wooledge.org/Quotes). Get into the habit of copy/pasting your code into http://shellcheck.net and it'll help you find and fix errors.