Bash Read File

Bash Read File – To read a file in Bash Scripting, you may use cat command or use “<” to open file and attach it to a standard input device handle of some application. In this Bash Tutorial, we shall present you with syntax and examples to read a file.

Example : Bash Read File using –cat fileName

Syntax

value=`cat FileName`

Sample File

Learn Bash Scripting.
Welcome to TutorialKart!
Learn Bash with examples.

Bash Script File

#!/bin/sh

value=`cat sampleFile.txt`
echo "$value"
$ ./read-file-example 
Learn Bash Scripting.
Welcome to TutorialKart!
Learn Bash with examples.
ADVERTISEMENT

Example : Bash Read File using –$(<fileName)

Syntax

value=$(<FileName)

Bash Shell

~/workspace/bash/file$ value=$(<sample.txt)
~/workspace/bash/file$ echo $value
Learn Bash Scripting.
Welcome to TutorialKart!
Learn Bash with examples.