logo

Bash Leer archivo

Hay muchas formas que podemos utilizar para leer un archivo en Bash Shell Scripting. Algunos de los métodos importantes se detallan a continuación (suponiendo que el nombre del archivo que estamos leyendo es 'read_file.txt'):

Leyendo un archivo usando 'cat fileName'

Podemos usar la siguiente sintaxis para imprimir el contenido del archivo en una terminal.

 value=`cat file_name` 

Ejemplo

 #!/bin/bash value=`cat read_file.txt` echo '$value' 

Producción

Bash Leer archivo

Leer archivo usando '$()

La siguiente es la sintaxis para leer el contenido del archivo usando '$'

 value=$(file_name) 

Ejemplo

 #!/bin/bash value=$(<read_file.txt) echo '$value' < pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/bash-tutorial/66/bash-read-file-2.webp" alt="Bash Read File"> <h3>Reading File Content from Command-line</h3> <p>If we want to read a file line by line from commands-line without using the &apos;cat&apos; command, we can run the following command to perform a task:</p> <p> <strong>Command</strong> </p> <pre> while read line; do Command; done <input.file < pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/bash-tutorial/66/bash-read-file-3.webp" alt="Bash Read File"> <p>Here, while loop will reach each line of the file and store the content of the line in $line variable which will be printed later.</p> <h3>Reading File Content Using Script</h3> <p>To read the file content using the script, we need to create a bash file and add the following code:</p> <p> <strong>Bash Script</strong> </p> <pre> #!/bin/bash file=&apos;read_file.txt&apos; i=1 while read line; do #Reading each line echo &apos;Line No. $i : $line&apos; i=$((i+1)) done <$file < pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/bash-tutorial/66/bash-read-file-4.webp" alt="Bash Read File"> <p>Here, an existing filename is stored in $file variable, and $i variable is used to keep the value of the line number of that line.</p> <h3>Passing filename from Command line and reading the File</h3> <p>Create a bash and add the following script which will pass filename from the command line and read the file line by line. The first argument value is read by the variable $1, which will include the filename for reading. If the file is available in the specified location then while loop will read the file line by line and print the file content.</p> <p> <strong>Bash Script</strong> </p> <pre> #!/bin/bash file=$1 while read line; do #Readind each line in sequence echo $line done <read_file.txt < pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/bash-tutorial/66/bash-read-file-5.webp" alt="Bash Read File"> <p>Here, the filename is used as an argument value. The output will provide the content of &apos;read_file.txt&apos; with no extra spaces between words.</p> <h3>Reading file by omitting Backslash Escape</h3> <p>If we want to read each line of a file line by line by omitting backslash-escape then we are required to use the &apos;-r&apos; option with a &apos;read&apos; command in &apos;while&apos; loop, e.g.:</p> <p> <strong>Bash Script</strong> </p> <pre> #!/bin/bash while read -r line; do #Reading each line by omitting backslash escape echo $line done <read_file.txt < pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/bash-tutorial/66/bash-read-file-6.webp" alt="Bash Read File"> <p>We may require reading the file for several programming purposes. For example, we can search or match any specific content easily from the file line by line. Hence, it is a useful task for any programming language.</p> <hr></read_file.txt></pre></read_file.txt></pre></$file></pre></input.file></pre></read_file.txt)>