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.


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

  • I work done! thanks ...And understood the reason. Dec 15 '12 at 18:18
  • @Mat I was unaware to the concept filename expansion Dec 15 '12 at 18:19
  • @Mat: for i in *;do echo $i;done; i file is due to filename expansion ? 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
  • 1
    @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. Dec 15 '12 at 23:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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