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 have a file called test and the contents are:

ubuntu@regina:~$ cat test
** test **

catting this file via command line works fine, but if I use command substitution I get an understandable but undesirable result.

ubuntu@regina:~$ A=$(cat test)
ubuntu@regina:~$ echo $A
deployment detect.sh htpasswd.py logs test uwsgi-1.0.4 uwsgi-1.0.4.tar.gz test deployment        detect.sh htpasswd.py logs test uwsgi-1.0.4 uwsgi-1.0.4.tar.gz

Because of the asterisks that exist in the file test it basically executes an echo * and lists the directory contents along with the file contents.

Is there a parameter I can pass to the command substitution syntax that will not provide this result, or is there another idiom that should be used for this?

share|improve this question
    
Note: using cat to read a single file is less efficient then reading the file, example: A=$(cat test) VS. A=$(<test) –  Tim Jun 7 '12 at 20:27
    
Thank you, I didn't know that about that idiom –  glenbot Jun 7 '12 at 20:38
add comment

1 Answer

up vote 4 down vote accepted

You want to do echo "$A". Wrapping the variable in the quotes makes it a string.

Example:

[root@talara test]# A=$(<test)
[root@talara test]# echo $A
FILE1 ham test test FILE1 ham test
[root@talara test]# echo "$A"
** test **
share|improve this answer
    
Thanks tim! That's exactly what I was looking for. Funny, I knew about quotes and now that I see the answer I feel pwned. –  glenbot Jun 7 '12 at 20:39
    
no prob. Shell quotes are like that, I forget often as well! –  Tim Jun 7 '12 at 20:49
    
Glen, don't forget to accept this answer if it resolved your question. –  bahamat Jun 7 '12 at 22:11
add comment

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.