LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 09-09-2011, 02:13 AM   #1
sunnieside-up
Member
 
Registered: Aug 2011
Distribution: Debian
Posts: 68

Rep: Reputation: Disabled
How to redirect cat into script variable


Hi,

I have a question about linux scripting. I'm trying to put the contents of a file using cat into a variable, how do I do this? Thanks in advance.
 
Old 09-09-2011, 02:20 AM   #2
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297
Hello,

Assuming you are referring to Bash for scripting:
Code:
VARIABLE=$(cat yourfile)
If your question is more specific you need to provide more information. Are you for example only looking to put the entire contents in a variable or are you trying to create an array from a file? The more verbose your question, the more complete answers you'll get.

Kind regards,

Eric
 
Old 09-09-2011, 02:26 AM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
You can also use redirection alone:
Code:
VARIABLE=$(<yourfile)
then use double quotes to embed the variable when you reference it, so that newlines are preserved:
Code:
echo "$VARIABLE"
 
1 members found this post helpful.
Old 09-09-2011, 02:27 AM   #4
salemeni
Member
 
Registered: Aug 2011
Posts: 64

Rep: Reputation: Disabled
So you can do
Code:
VARIABLE=`cat yourfile`
java socket

Last edited by salemeni; 01-26-2012 at 07:26 AM.
 
Old 09-09-2011, 07:48 AM   #5
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by salemeni View Post
So you can do
Code:
VARIABLE=`cat yourfile`
Unlike $(command), backticks can be confused with single quotes and don't nest easily.
 
Old 09-09-2011, 01:15 PM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
What MTK358 said.

To populate an array from a file, there's a new (as of bash 4.0) mapfile built-in (a.k.a. readarray). Each line from stdin will be stored in a separate array element.
Code:
mapfile -t arrayname <file
The -t option removes the trailing newline from the entry, something you'll usually want to do.

For older shell versions you can use something like this:
Code:
while read line ; do
	arrayname+=( $line )
done <file

Last edited by David the H.; 09-09-2011 at 01:17 PM.
 
Old 09-09-2011, 02:49 PM   #7
sunnieside-up
Member
 
Registered: Aug 2011
Distribution: Debian
Posts: 68

Original Poster
Rep: Reputation: Disabled
Yeah, I'm using bash, and these little things are a bit confusing to me at first, with what the spaces between equals signs counting and all that. Sweet, thanks for all the help!
 
  


Reply



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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] bash: for var in $(cat file): via redirect possible??? NOT a while loop jtwdyp Linux - General 5 04-12-2011 06:20 AM
[SOLVED] [LINUX] substituting variable upon cat execution Winterburn Programming 5 03-31-2010 05:30 AM
Piping cat output to variable? zizou86 Programming 12 01-13-2010 03:17 PM
cat file with $variable linuxbin Linux - Newbie 6 08-27-2009 03:27 AM
shell script: redirect to variable va1damar Programming 1 02-05-2007 04:11 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 09:16 AM.

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