Sign up ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

I need to filter and store traceroute output in shell variable (array). But I am getting some unusual output.

As I try to explain in following lines.

My present directory is temp, having some files:

/temp$ ls 
ask.html  data_dump (2)  DB-Assignemnt .sql    

When I run traceroute on command line its output is correct as I desired.

/temp$ traceroute -m 5  | awk '{print  $2 , $3}'
to ( (
* * ( (

If I save output in a variable a

/temp$ a=$(traceroute -m 5 | awk '{print $2 , $3}')

When I echo $a The output is not understood to me.

~/temp$ echo $a
to ( ABTS-North-Static- ( ask.html data_dump (2) DB-Assignemnt .sql ask.html data_dump (2) DB-Assignemnt .sql ask.html data_dump (2) DB-Assignemnt .sql ask.html data_dump (2) DB-Assignemnt .sql (

Why ls executed itself and input is save in variable a.

share|improve this question

1 Answer 1

up vote 2 down vote accepted

When you echo $a, the shell does "word splitting" and all significant whitespace is lost.

Try echo "$a" with double quotes.

Quoting will also ensure filename expansion does not occur.


share|improve this answer
I work done! thanks ...And understood the reason. – Grijesh Chauhan Dec 15 '12 at 18:18
@Mat I was unaware to the concept filename expansion – Grijesh Chauhan Dec 15 '12 at 18:19
@Mat: for i in *;do echo $i;done; i file is due to filename expansion ? – Grijesh Chauhan Dec 15 '12 at 18:28
@GrijeshChauhan: the shell assigns values to i based on the result of filename expansion of * – Mat Dec 15 '12 at 18:30
@GrijeshChauhan, you should make it a habit of always quoting variables (echo "$i") -- unless you have a specific reason for using the word splitting and other effects of not quoting. – glenn jackman Dec 15 '12 at 23:10

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.