Take the 2-minute tour ×
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 www.google.com  | awk '{print  $2 , $3}'
to www.google.com
172.22.0.1 (172.22.0.1)
ABTS-North-Static-085.3.160.122.airtelbroadband.in (122.160.3.85)
* *
203.101.83.197 (203.101.83.197)
182.79.254.249 (182.79.254.249

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 172.22.0.1 (172.22.0.1) ABTS-North-Static-
085.3.160.122.airtelbroadband.in (122.160.3.85) 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
203.101.100.186 (203.101.100.186)

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

share|improve this question
add comment

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.

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
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. –  glenn jackman Dec 15 '12 at 23:10
show 2 more comments

Your Answer

 
discard

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.