How to put content of file into a variable?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to put content of file into a variable?
# 1  
Old 10-13-2010
How to put content of file into a variable?

For example, I have a simple text file
Code:
note:
this a note
a simple note
a very very simple note

when I use this command,
Code:
temp=$(cat "note.txt")

then I echo temp, the result is in one line.
Code:
echo $temp
note: this a note a simple note a very very simple note

My variable doesn't have newline.
How to make the newline still exist?
And when I echo my variable, it shows same exact form of content.
thanks
# 2  
Old 10-13-2010
Code:
# cat file
note:
this a note
a simple note
a very very simple note
# var=$(<file)
# echo "$var"
note:
this a note
a simple note
a very very simple note

This User Gave Thanks to danmero For This Post:
# 3  
Old 10-13-2010
Quote:
Originally Posted by danmero
Code:
# cat file
note:
this a note
a simple note
a very very simple note
# var=$(<file)
# echo "$var"
note:
this a note
a simple note
a very very simple note

I don't know it will be as simple as that, because I'm new to bash and I only know cat.
Thanks.Smilie
# 4  
Old 10-13-2010
They are there, but doing echo $var instead of echo "$var" has the side-effect of flattening out all whitespace.

What are you attempting by putting an entire file in a variable? There's probably better and safer ways to do what you want. Many shells have a variable size limit, so this is only guaranteed to work when the file is tiny...

Last edited by Corona688; 10-13-2010 at 12:46 PM..
This User Gave Thanks to Corona688 For This Post:
# 5  
Old 10-13-2010
Quote:
Originally Posted by 14th
because I'm new to bash and I only know cat.
Be welcome, you should take Useless Use of Cat Award first.
This User Gave Thanks to danmero For This Post:
# 6  
Old 10-13-2010
Quote:
Originally Posted by Corona688
They are there, but doing echo $var instead of echo "$var" has the side-effect of flattening out all whitespace.

What are you attempting by putting an entire fire in a variable? There's probably better and safer ways to do what you want. Many shells have a variable size limit, so this is only guaranteed to work when the file is tiny...
yes, the file's size is small.
It's only contain less then 10 lines, and only have a few words each line.
thanks
# 7  
Old 10-13-2010
Yes, but what are you attempting by putting an entire file in a variable? There's probably better and safer ways to do what you want. As you've already discovered, it can have unintended side-effects.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

I must use first sentence from a file to put into variable

i am having some bash script which must use first sentence of the file. For example i have file which content is: test 213 So I must use word test into my bash script, and put it into variable. I am using a one variable named value value=$(</home/rusher/test.txt) so instead using test.txt... (1 Reply)
Discussion started by: tomislav91
1 Replies

2. Shell Programming and Scripting

Problem to match a path in a file and put it into a variable

Hello,:p I made a script which do a backup on remote servers with a rsync command. I have a config.cfg with the IPs and the paths where it will copy the directory. The problem is that it doesn't match the paths, So, here my script and its output with the debug : #!/bin/bash # PATHS... (7 Replies)
Discussion started by: Arnaudh78
7 Replies

3. UNIX for Dummies Questions & Answers

Getting ls content into a file using variable

hi i just cant figure out how can i do this ls -lt > log.txt using $PWD what i mean is how can i get the ls command content into a file using $PWD variable? :confused: (4 Replies)
Discussion started by: chinababy
4 Replies

4. Shell Programming and Scripting

Read value of a file, remove first 2 chars and put this value in a variable

Hi i have need of read a file value with cat command and remove first 2character for example cat /sys/class/rtc/day 0x12 Remove char 12 And put this value in a variable is possible with a script thanks for help (6 Replies)
Discussion started by: enaud
6 Replies

5. Shell Programming and Scripting

Variable of Content From Part of Other File

I may not being doing this description justice, but I'll give it a try. I created a mailx script; there will be several messages using the same script where the only difference is the content. So I figured I'd make the content of the message a variable retrieved from a separate file. I have five... (5 Replies)
Discussion started by: royarellano
5 Replies

6. UNIX for Dummies Questions & Answers

How to assign the content of a file to a variable?

Hi all, I have a problem here. I have a file and let we take the content of the file is just '32' (only a numeric value in that file). Now I need to assign this numeric value ( value in that file) to a variable. Is that possible? If so, can you plz advice me on this? Thanks in... (4 Replies)
Discussion started by: iamgeethuj
4 Replies

7. Shell Programming and Scripting

redirecting variable content to a file!

Hello! I'm having problems trying to extract the contents of a variable and placing it into a text file. Grateful for any help. Been trying something along the lines of: $variable > file.txt or `cat < $variable` > file.txt As you can see I'm a newbie to this :D (2 Replies)
Discussion started by: lloowen
2 Replies

8. Shell Programming and Scripting

put the contents of this file into a variable with multiple lines

I have a file that contains the following lines the brown quick fox jumped over the white laze dog 0123456789 I wanted to put the contents of this file into a variable so I used this code: VAR_LIST=`cat $2` where $2 is the file name passed as an argument to the script If I... (3 Replies)
Discussion started by: Nomaad
3 Replies

9. Shell Programming and Scripting

Put content of file into variables

How to put a line of strings (with some white spaces in between) from a file into variables? I have tried the following codes. However, the content is cut by space (not by line) for i in `cat ${source_file}` do echo $i done Please comment. Thanks. (2 Replies)
Discussion started by: Rock
2 Replies

10. AIX

Put one ligne from a file a variable

Hi everybody I looking the put the result of a commane to a Variable i explain here is my command: FJTS_UK:root:common@ukaix3:/> cat sortie | grep "^"| awk '{ print $1}' 15 FJTS_UK:root:common@ukaix3:/> sortie is a texte file I want to put the result of commande in a... (1 Reply)
Discussion started by: kykyboss
1 Replies
Login or Register to Ask a Question