Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash: difference between cat and echo

Tags:

bash

This is file.txt (without an end-of-line for the last line):

foo:bar:baz:qux:quux
one:two:tree:four:five:six:seven
alpha:beta:gamma:delta:epsilon:zeta:eta:teta:iota:kappa:lambda:mu
the quick brown fox jumps over the lazy dog

File read.sh

while read -r line
do
    echo $line
done < file.txt

This is what I tried in the terminal:

./read.sh

Output:

foo:bar:baz:qux:quux
one:two:tree:four:five:six:seven
alpha:beta:gamma:delta:epsilon:zeta:eta:teta:iota:kappa:lambda:mu

Why doesn't read.sh show the last end of line like cat file.txt does?

like image 236
Maria Ines Parnisari Avatar asked Sep 08 '13 03:09

Maria Ines Parnisari


People also ask

What is the difference between echo and cat?

You can also use echo to to redirect the standard output of the command on the left and append it to the end of the file on the right. The output will be added to the file hello. txt. Whereas the command cat displays the contents of one or more files to the terminal.

What is the difference between echo and printf in Bash?

Printf provides for the creation of a formatting string and offers a non-zero quit status when it fails. Whereas echo normally leaves with a 0 status and typically outputs inputs headed by the end of line character upon this standard result. The “printf” gives you more options for the output format than the “echo”.

What does echo cat do in Linux?

The cat command is a very popular and versatile command in the 'nix ecosystem. There are 4 common usages of the cat command. It can display a file, concatenate (combine) multiple files, echo text, and it can be used to create a new file.

What does cat mean in Bash?

by Aqsa Yasin. The “cat” command in Bash stands for “concatenate”. This command is very frequently used for viewing, creating, and appending files in Linux.


2 Answers

Because there is no end of line in file.txt, if you:

$ od -c file.txt
0000000   f   o   o   :   b   a   r   :   b   a   z   :   q   u   x   :
0000020   q   u   u   x  \n   o   n   e   :   t   w   o   :   t   r   e
0000040   e   :   f   o   u   r   :   f   i   v   e   :   s   i   x   :
0000060   s   e   v   e   n  \n   a   l   p   h   a   :   b   e   t   a
0000100   :   g   a   m   m   a   :   d   e   l   t   a   :   e   p   s
0000120   i   l   o   n   :   z   e   t   a   :   e   t   a   :   t   e
0000140   t   a   :   i   o   t   a   :   k   a   p   p   a   :   l   a
0000160   m   b   d   a   :   m   u  \n   t   h   e       q   u   i   c
0000200   k       b   r   o   w   n       f   o   x       j   u   m   p
0000220   s       o   v   e   r       t   h   e       l   a   z   y    
0000240   d   o   g

There are no \n at the end of the file.

echo on the other other hand will always add a new line when you echo a message if there isn't one.

like image 106
Baiyan Huang Avatar answered Sep 22 '22 04:09

Baiyan Huang


Other answers are right, there is simply no newline character in the end of your file.txt.

Most text editors will end a file with a newline automatically, even nano does that. But your file was generated by a script, right? To reproduce this behavior all you have to do is:

echo -n 'hello world' >> file.txt

-n flag tells echo not to output the trailing newline.

Also, if you want your read code to work, you can use this:

while read -r line
do
    printf "%s\n" "$line"
done < file.txt
[[ -n $line ]] && printf '%s' "$line"

This is going to work because actually read will place the last line into the variable, but it also will return false, thus breaking the while loop.

like image 26
Aleks-Daniel Jakimenko-A. Avatar answered Sep 24 '22 04:09

Aleks-Daniel Jakimenko-A.