Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 www.google.com  | awk '{print  $2 , $3}'
to www.google.com (
ABTS-North-Static- (
* * ( (

If I save output in a variable a

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

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

~/temp$ echo $a
to www.google.com ( 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
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.

See http://www.gnu.org/software/bash/manual/bashref.html#Shell-Expansions

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.