Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Linux by (9.5k points)

Below is my script and I want it to read a file in order to save it to a variable. 

 I want to read a file and save it in a variable, but I need to keep the variable and not just print out the file. How can I do this? I have written this script but it isn't quite what I needed:

#!/bin/sh

while read LINE  

do  

  echo $LINE  

done <$1  

echo 11111-----------  

echo $LINE  

My script takes a filename as a parameter which contains “aaaa”, the output would be 

aaaa

11111-----

Can anyone tell me how to save it to a variable instead of printing it?

1 Answer

0 votes
by (19.7k points)
  1. Use ‘sh’ in a cross-platform: 

#!/bin/sh

value=`cat config.txt`

echo "$value"

  1. Use ‘cat’ to read whole file as a variable in bash or zsh: 

#!/bin/bash

value=$(<config.txt)

echo "$value"

If you want to learn more about Linuxthen go through this Linux tutorial by Intellipaat for more insights.  

Related questions

0 votes
2 answers
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...