Home / code / shell

Files

You can easily read and write to files with bash.

You can write to a file with the following command :

echo "things to write" > file.txt
Note: this overwrite the file.txt if it already exists.
If you want to append to file use the following command.
echo "things to appand" >> file.txt
If you want to a blank line, you need echo -e
echo "things to write" > file.txt echo -e "\n" >> file.txt echo "New line" >> file.txt
Remember that echo already ends with a newline character (even if you don't print one)

Read a file into a variable

testvalue=`cat test.txt`
You read the file test.txt into the variable testvalue.
Those are backticks quotes not single quotes !

Read file line by line

while read line
do
  echo "This is a line : $line"
done < $FILE

 

TOP

Latest script:

 

Books: