- Bash Read Variable From File
- Two Lines From The Brothers Grimm
- For Line In File Bash
- Bash Read Multiple Lines From File
Active22 days ago
Bash: Read File Line By Line – While Read Line Loop Posted on Tuesday December 27th, 2016 Sunday March 19th, 2017 by admin The while loop is the best way to read a file line by line in Linux. Open the two files on different file descriptors.Redirect the input of the read built-in to the descriptor that the file you want is connected to. In bash/ksh/zsh, you can write read -u 3 instead of read. H ow do I read a text file line by line under a Linux or UNIX-like system using KSH or BASH shell? You can use while.do.done bash loop to read file line by line on a Linux, OSX, *BSD, or Unix-like system. Looping through the content of a file in Bash. Ask Question. There are four elements that will alter the meaning of the file output read by many Bash solutions: The blank line 4; Leading or trailing spaces on two lines; Maintaining the meaning of individual lines (i.e., each line is a record). One of the most common errors when using scripts bash on GNU/Linux is to read a file line by line by using a for loop (for line in $ (cat file.txt) do..). In this example, the for loop leads to an assessment for each line, rather than as assessment of every word in the file.
I am writing a bash script to look for a file if it doesn't exist then create it and append this to it:
So
'line then new line 'tab' then text'
I think its a sensitive format. I know you can do this:But it seems weird since its two lines. Is there a way to append that in this format:
don_crissti54.7k18 gold badges152 silver badges179 bronze badges
TheLegendTheLegend1,7973 gold badges10 silver badges14 bronze badges
10 Answers
If sudo (other user privileges) is needed to write to the file, use this:
Isaac14.6k1 gold badge23 silver badges63 bronze badges
Hauke LagingHauke Laging60.2k12 gold badges93 silver badges141 bronze badges
Or, if it's a literal tab that you want (rather than the four spaces in your question):
You can achieve the same effect with
kyriasecho
, but exactly how varies from implementation to implementation, whereas printf
is constant.3,4351 gold badge18 silver badges30 bronze badges
evilsoupevilsoup4,5692 gold badges20 silver badges37 bronze badges
kendotwillkendotwill
Another approach is to use
tee
A few choice lines from
tee
's man page:The tee utility copies standard input to standard output, making a copy in zero or more files.
-a - Append the output to the files rather than overwriting them.
user206934
SED can append a line to the end of a file like so:
Source:http://www.yourownlinux.com/2015/04/sed-command-in-linux-append-and-insert-lines-to-file.html
EDIT
Does this approach have any added benefit than other solutions?
Yes, this approach has the added benefit of appending to any files return in a search, such as this:
sed -i '$ a text to be inserted' fileName.file
$
selects end of file, the a
tells it to append, and after this comes the text that is to be inserted. Then of course the file name.Source:http://www.yourownlinux.com/2015/04/sed-command-in-linux-append-and-insert-lines-to-file.html
EDIT
Does this approach have any added benefit than other solutions?
Yes, this approach has the added benefit of appending to any files return in a search, such as this:
find . -name '*.html' -exec sed -i '$ a </html>' {} ;
I used the above example to insert the ending html tag that was missing on every html page within a number of directories.
OB7DEVOB7DEV
I used sed because it can be used with sudo. For example:
the alternative is very ugly like :
and even uglier when done with ssh.
sam15.3k3 gold badges16 silver badges28 bronze badges
Fred MehrdadFred Mehrdad
Another one liner is:
I'd prefer the
-e
option thought, as it gives more control:Erdal G.Erdal G.
One can emulate
cat >> out.txt
with either Perl or Python to achieve same effect. Perl:And Python:
Note that for python you'll have to hit Ctrl+D twice. See related question on stackoverflow for more info.
Sergiy KolodyazhnyySergiy Kolodyazhnyy11.2k4 gold badges28 silver badges69 bronze badges
Here is an example to append multiple lines in a file:
user2753331user2753331
In addition to main answer, in case the file needs super user permissions, just adding
sudo
in front of echo
won't work.This is because shell breaks the commands and though
echo
did run as root, but >>
ran with normal privileges.This will work for super user:
sudo su -c 'echo 'Line 3' >> greetings.txt'
SJ00SJ00
protected by ArchemarAug 8 at 13:24
Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
Would you like to answer one of these unanswered questions instead?
Not the answer you're looking for? Browse other questions tagged shelltext-processingio-redirection or ask your own question.
Active5 months ago
I want to insert in my script a value (string) that I would read from a text file.
For example, instead of:
I want to read a string from another text file so the interpreter should read the string from the file and not the user input.
Braiam54.1k21 gold badges144 silver badges229 bronze badges
user208413user208413
10 Answers
To read variables from a file we can use the
source
or .
command.Lets assume the file contains the following line
we can then import this variable using
mook7656,1503 gold badges16 silver badges38 bronze badges
Bash Read Variable From File
TakkatTakkat113k40 gold badges260 silver badges385 bronze badges
Considering that you want all the content of your text file to be kept in your variable, you can use:
Or, in pure bash:
Radu RădeanuRadu Rădeanu126k36 gold badges265 silver badges333 bronze badges
From within your script you can do this:
You can even do this multiple times e.g. in a loop
thomthom5,2073 gold badges18 silver badges25 bronze badges
One alternative way to do this would be to just redirect standard input to your file, where you have all the user input in the order it's expected by the program. For example, with the program (called
script.sh
)and the input file (called
input.in
)you could run this from the terminal in one of the following two ways:
and it would be equivalent to just running the script and entering the data manually - it would print the line 'Hello Tomas. You're 26 years old, right?'.
As Radu Rădeanu has already suggested, you could use
cat
inside your script to read the contents of a file into a avariable - in that case, you need each file to contain only one line, with only the value you want for that specific variable. In the above example, you'd split the input file into one with the name (say, name.in
) and one with the age (say, age.in
), and change the read name
and read age
lines to name=$(cat name.in)
and age=$(cat age.in)
respectively.Community♦
Tomas AschanTomas Aschan1,4477 gold badges30 silver badges54 bronze badges
I found working solution here:https://af-design.com/2009/07/07/loading-data-into-bash-variables/
LukaszLukasz
From
abu_buaman bash:1785
, this command substitution is equivalent to name=$(cat '$file')
but faster.4,7988 gold badges18 silver badges36 bronze badges
hellorkhellork
Two Lines From The Brothers Grimm
nedimnedim
If you want to use multiple strings, you could go with:
OR
If you want the user to indicate the file
GeorgeGeorge
For Line In File Bash
Alberto Salvia NovellaAlberto Salvia Novella
I use this to get a single variable from a file
When
GET_VAR
is not found in ${FILE}
it will be blank rather than causing an error thanks to the || true
.It uses
grep -P
which is in GNU grep but not default in all grep at the time of writing this.Eric NemchikEric Nemchik