LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-21-2011, 01:05 AM   #1
ted_chou12
Member
 
Registered: Aug 2010
Location: Zhongli, Taoyuan
Distribution: slackware, windows, debian (armv4l GNU/Linux)
Posts: 431
Blog Entries: 32

Rep: Reputation: 3
Reading file into variable in bash


I know that cat can output the file, but how do you store that output in a variable to process:
Code:
CONTENT=cat file.txt
This doesnt seem to work?
Thanks
 
Old 03-21-2011, 01:38 AM   #2
kheldar
Member
 
Registered: Jul 2004
Location: Nottingham, England
Distribution: Fedora 17, Centos 6.3 x86_64
Posts: 35

Rep: Reputation: 2
Try this...

Code:
CONTENT=$(cat file.txt)
which is the more elegant way, or the older backquote method would be
Code:
CONTENT=`cat file.txt`
Both methods substitute the standard output of the command (whatever the command is) into the main command, whatever that is.
 
Old 03-21-2011, 01:44 AM   #3
ted_chou12
Member
 
Registered: Aug 2010
Location: Zhongli, Taoyuan
Distribution: slackware, windows, debian (armv4l GNU/Linux)
Posts: 431

Original Poster
Blog Entries: 32

Rep: Reputation: 3
thanks!
 
Old 03-21-2011, 01:52 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
To add to this I would also recommend you reconsider why you would want to place an entire file in a variable??
Normally there is specific data you require from the file and it is generally (of course not always) better to get this information
stored in your variable and then proceed.
 
  


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
How to zip a variable with PERL without reading or writing a file? maxmeier12 Programming 4 06-04-2013 04:50 AM
reading variable while remaining on the same line - bash GSMD Programming 3 04-24-2011 09:47 AM
How to get variable from text file into Bash variable mcdef Linux - Software 2 06-10-2009 01:15 PM
Reading a bash variable in bash scripting problem freeindy Programming 3 11-27-2008 02:29 AM
Saving a text file as a variable and reading it every second in java script mrobertson Programming 4 03-26-2007 08:25 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:09 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
Open Source Consulting | Domain Registration