LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-11-2013, 12:24 AM   #1
ankitpandey
Member
 
Registered: Jan 2012
Location: Mumbai
Posts: 63

Rep: Reputation: Disabled
Difference between echo of file within double quotes and without double quotes


Hello All,

I had a file which i have copied in variable FIL, but when i do echo to FIL variable (without double quotes) it shows the content of file in un-ordered manner and even the other file name present in directory comes as part of content FIL variable whereas when i echo the FIL variable with double quotes it shows the exact content of file in FIL variable.

test is the name of file with say 50 lines

FIL is the variable in which i am storing it temporary.

Code:
FIL=`cat test`

echo $FIL
Above echo of FIL is showing absurd result.

Code:
FIL=`cat test`

echo "$FIL"
Whereas this code is showing correct result. Please explain the difference. How come all the new lines present in temp file are getting eaten and files present in directory also gets include in FIL when echoed without double quotes.

Thanks
 
Old 01-11-2013, 01:41 AM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 13,188

Rep: Reputation: 4181Reputation: 4181Reputation: 4181Reputation: 4181Reputation: 4181Reputation: 4181Reputation: 4181Reputation: 4181Reputation: 4181Reputation: 4181Reputation: 4181
you need to check how ' " and ` work in bash. Actually in the first case (echo $FIL) the content of $FIL will be evaluated by the shell during execution. Just try:
Code:
FIL='*'
echo $FIL
echo '$FIL'
echo "$FIL"
see here: http://www.linuxjournal.com/content/bash-quoting, http://wiki.bash-hackers.org/syntax/quoting, http://linuxreviews.org/beginner/Bas...O/en/x303.html, but others may know better links...
 
Old 01-11-2013, 10:02 AM   #3
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,398
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
It is a fairly unusual programming pattern to store all of the content of a multi-line text file in a single scalar variable. More often, a variable is used to hold a single line read from a file, or perhaps an array variable holding one line per element. If the file must be processed in some way, it is conventional to read it, usually line-by-line, into a variable in some programming loop structure. If you can explain some of the bigger picture for your intention, perhaps a better solution to you problem can be suggested.

--- rod.
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
translate value from single quotes to double quotes venkateshrupineni Linux - Newbie 2 06-14-2012 04:03 PM
Double Quotes Inside Double Quotes youarefunny Programming 6 06-09-2010 11:21 PM
Problems with quotes and double quotes Andruha Slackware 6 01-02-2010 05:44 PM
Using single quotes vs double quotes in PHP strings vharishankar Programming 6 07-11-2005 12:41 PM
In BASH shell, what is the difference in usage between single and double quotes? davidas Linux - Newbie 2 04-05-2004 04:00 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:55 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration