Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

perl and bash external command difference

Tags:

bash

perl

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?

like image 279
Mint.K Avatar asked Mar 11 '17 01:03

Mint.K


People also ask

What is difference between Perl and Bash?

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.

What is the difference between Perl and shell script?

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.

Is Perl faster than bash?

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 ).


1 Answers

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.

like image 120
mklement0 Avatar answered Oct 26 '22 22:10

mklement0