Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have a scirpt like this:

#!/bash/bin
file=`cat /proc/net/arp`
echo $file

And when I run the script it outputs the whole folder where the script is like this:

IP address HW type Flags HW address Mask Device 192.168.1.102 0x1 0x2 a0:88:b4:e3:f6:6c naloga1.png naloga1.txt naloga1.txt~ naloga2.png naloga2.txt naloga2.txt~ naloga3.png naloga3.sh naloga3.sh~ naloga3.txt naloga3.txt~ naloga4.png naloga4.txt naloga5.png naloga5.sh naloga5.sh~ naloga5.txt naloga6.png naloga6.txt naloga6.txt~ naloga7.png naloga7.txt naloga7.txt~ naloga8.sh naloga8.sh~ porocilo.odt tekst.txt tekst.txt~ eth0 192.168.1.1 0x1 0x2 58:6d:8f:15:fd:c3 naloga1.png naloga1.txt naloga1.txt~ naloga2.png naloga2.txt naloga2.txt~ naloga3.png naloga3.sh naloga3.sh~ naloga3.txt naloga3.txt~ naloga4.png naloga4.txt naloga5.png naloga5.sh naloga5.sh~ naloga5.txt naloga6.png naloga6.txt naloga6.txt~ naloga7.png naloga7.txt naloga7.txt~ naloga8.sh naloga8.sh~ porocilo.odt tekst.txt tekst.txt~ eth0

Any idea why it is acting this way?

share|improve this question
1  
Gordon just gave the answer before I finished mine. I got got one more thing to add: set -x. Start your script with the normal shebang line (#!/usr/bin/env bash) and on the next line add set -x. This will show you debug information. –  Hennes Apr 13 '13 at 15:39
    
Copy-paste your scripts, do not rewrite them. It helps others to detect the real problem. I've never seen /bash/bin. –  ignis Apr 14 '13 at 15:01

1 Answer 1

up vote 2 down vote accepted

The file contains a wildcard (probably *), and when you echo it you don't have double-quotes around the $file variable reference. When the shell expands a variable reference like that, it goes back through the value and parses a number of things: spaces (treated as separators between arguments), wildcards (which are replaced by the appropriate list of filenames), etc. Putting the variable reference inside double-quotes avoids this extra parsing. In general, it's a good idea to put all variable references in double-quotes unless you specifically want the extra parsing for some reason.

BTW, I also prefer $() over backquotes -- they do essentially the same thing, but the parenthesis version is much easier to read. Also, in this context bash will let you skip the cat command and just use a < redirection (note that this is a bash-only feature, though).

#!/bash/bin
file=$(</proc/net/arp)
echo "$file"
share|improve this answer

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.