Linked Questions
16 questions linked to/from Why does shell Command Substitution gobble up a trailing newline char?
10
votes
1
answer
2k
views
When printing a variable that contains newlines, why is the last newline stripped? [duplicate]
Contents of file.txt (no weirdness, text file as defined by POSIX)
iguana
gecko
anole
Sample script:
#!/bin/sh
string="$(cat file.txt)"
printf '%s' "$string"
Sample output:
[coolguy@somemachine ~]...
0
votes
2
answers
1k
views
Bash and other shells $(...) remove the ending newline character [duplicate]
Why the shell structure $(...) removes the ending newline character?
#!/bin/bash
S='a
b
'
printf '%i [%s]\n' "${#S}" "$S"
T=$(printf '%s' "$S")
printf '%i [%s]\n' "...
0
votes
1
answer
258
views
Why bash strips new lines on command substitution/function assignments? [duplicate]
So let's say we have this code:
a=$(echo -e "a\n\n\n")
echo "_${a}_"
the output is: _a_ which is kind of a surprise to me. Any ideas why this is happening?
0
votes
1
answer
148
views
Why can't echo and printf output variables that have only whitespace characters? [duplicate]
var1=$(printf "\n\x0A\n")
var2=$(printf "\na\nb\nc")
Using Android Terminal, the output of echo -e "$var1" (same for echo and printf) iss nothing, not even the 3 new lines. But echo -e "$var2" or ...
1
vote
0
answers
55
views
Why are trailing line breaks stripped when used as an argument (for echo)? [duplicate]
When I run this…
echo "
fdsfsd
dsfsdf
"
I really get the output of all these lines.
However, if I use this output as an argument for echo again…:
echo "$(echo "
fdsfsd
dsfsdf
")"
…I do ...
134
votes
9
answers
245k
views
How to add newlines into variables in bash script
When I do
str="Hello World\n===========\n"
I get the \n printed out too. How can I have newlines then?
69
votes
5
answers
125k
views
Why do newline characters get lost when using command substitution?
I have a text file named links.txt which looks like this
link1
link2
link3
I want to loop through this file line by line and perform an operation on every line. I know I can do this using while loop ...
14
votes
6
answers
3k
views
What is the reverse of echo -e?
If I have a string with nonprintable characters, new lines, or tabs, is there a way I can use echo to print this string and show codes for these characters (e.g., \n for new line, \b for backspace)?
16
votes
2
answers
4k
views
Where has the trailing newline char gone from my command substitution?
The following code best describes the situation. Why is the last line not outputting the trailing newline char? Each line's output is shown in the comment. I'm using GNU bash, version 4.1.5
...
3
votes
5
answers
4k
views
Reading from a text file and printing the words character by character at a time
I would like to write a program that reads from a text file and prints in Terminal the words of that file character by character every one second.
For example, in a text file log.txt let's say I have ...
3
votes
4
answers
6k
views
How do I use newlines inside () with sed?
When I want to replace, say Dot with
Dot
##empty line##
I do this:
sed 's/Dot/Dot\
/g'
Yes, a literal new line, this is the way it works on BSD sed. But, I can't do the same when the sed is inside ...
3
votes
1
answer
3k
views
How do I turn entire stdin into a command line argument verbatim?
Attempt 1
xargs -I '{}' -0 -n 1 myprogram --arg '{}' --other-options
But that does not preserve zero bytes. Also the program may run multiple times. But instead of failing in case of zero byte ...
8
votes
2
answers
2k
views
Elegant way to prevent command substitution from removing trailing newline
I'm customizing my zsh PROMPT and calling a function that may or may not echo a string based on the state of an environment variable:
function my_info {
[[ -n "$ENV_VAR"]] && echo "Some ...
1
vote
2
answers
4k
views
Saving Output In a Variable and Re-using it
page=`grep $x /var/www/vhosts/example.com/statistics/logs/access_log | awk '{print $7}'| sort |uniq -c |sort -nr`
times=`$page | wc -l`
I am trying to save output of grep in a variable as page.
But ...
1
vote
1
answer
70
views
Visible content of 2 variables in bash is the same, but the length is different
These 2 variables will have the same visible content
x_sign1="aabbccdd_and_somthing_else"
var1="...."
[........]
x_sign2=$(echo -n "${var1}${var2}${var3}" | shasum -a 256)
echo $x_sign2
...