Bash/Python: Does `echo` Command Insert A Newline When Acting As Input To Another Command?
I have a simple python script line_printer.py: import fileinput i = 1 for line in fileinput.input(): print 'Line', str(i), ':', line.strip() i+=1 I'm trying to understand
Solution 1:
This command will answer your question:
echo 'hi' | od -c
The reason for the trailing \n
character is that stdout on a terminal by default uses line buffering - meaning it will only display output data that ends with the newline character.
Play around with the printf command:
printf "%s" foo
printf "%s\n" anotherfoo
Solution 2:
If you look in the bash source, bash-4.2/builtins/echo.def you can see that the builtin echo
command always (line 113) outputs a final \n
(line 194) unless the -n
was specified (line 198) or output of echo
is used as a string (line 166). You can test this by doing
echo `echo "Ho ho ho"` | od -c
You will see only one \n
because the output of echo "Ho ho ho"
is evaluated as a string in the expression echo `echo "Ho ho ho"`
.
It doesn't seem to have any relation to the terminal setup.
Post a Comment for "Bash/Python: Does `echo` Command Insert A Newline When Acting As Input To Another Command?"