My name.txt has contains:
Tom
Daniel
James
In Perl,
my $names = `cat names.txt`;
print $names;
gives me:
Tom
Daniel
James
In Bash,
names=`cat names.txt`
echo $names
gives me:
Tom Daniel James
Here's my output of od -c name.txt:
0000000    T  o  m  \n   D   a   n   i   e   l  \n   J   a   m   e   s
0000020
What is the reason of the difference?
Bash is a Unix shell and command language which is commonly used for system administration tasks whereas Python/Perl/Ruby is used for general-purpose programming. But all of these can handle all kind of problems and all have different strengths over the rest.
Shell scripting is more handy when it comes to typing a few lines & if your script targets internal commands. When you need to call external programs, use Perl. Perl is more targeted at text-processing, so it makes it more powerful in that regard as compared to shell scripts.
Perl is much faster than BASH ( but then, so is almost everything else. Time how long it takes both to count to 500,000,000, or to compute a factorial ).
Both variables receive the same value, but the way you're examining the value in Bash is flawed: use echo "$names" - note the double quotes - to see the true value.
Unquoted use of $names in Bash makes its value subject to word-splitting, which means that the whitespace-separated words in the value - Tom, Daniel, and James - are passed as separate arguments  to echo, and echo concatenates those values with spaces on output.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With