Bash Read File
Bash check if file Exists
Bash Check if Variable is Set
Bash Alias
There are many ways that we can use to read a file in Bash Shell Scripting. Some of the important methods are given below (Assuming, name of the file that we are reading is 'read_file.txt'):
Reading File Using 'cat fileName'
We can use the following syntax to take a print of the contents of the file to a terminal.
value=`cat file_name`
Example
#!/bin/bash
value=`cat read_file.txt`
echo "$value"
Reading File Using '$()
Following is the syntax to read the content of the file using '$'
value=$(file_name)
Example
#!/bin/bash
value=$(<read_file.txt)
echo "$value"
Reading File Content from Command-line
If we want to read a file line by line from commands-line without using the 'cat' command, we can run the following command to perform a task:
Command
while read line; do Command; done < input.file
Output
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.
Reading File Content Using Script
To read the file content using the script, we need to create a bash file and add the following code:
Bash Script
#!/bin/bash
file='read_file.txt'
i=1
while read line; do
#Reading each line
echo "Line No. $i : $line"
i=$((i+1))
done < $file
Output
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.
Passing filename from Command line and reading the File
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.
Bash Script
#!/bin/bash
file=$1
while read line; do
#Readind each line in sequence
echo $line
done <read_file.txt
Output
Here, the filename is used as an argument value. The output will provide the content of 'read_file.txt' with no extra spaces between words.
Reading file by omitting Backslash Escape
If we want to read each line of a file line by line by omitting backslash-escape then we are required to use the '-r' option with a 'read' command in 'while' loop, e.g.:
Bash Script
#!/bin/bash
while read -r line; do
#Reading each line by omitting backslash escape
echo $line
done < read_file.txt
Output
Bash check if file Exists
Most of the time, we may find a situation where we may need to perform an action that will check whether a file exists or not.
In Bash, we can use a 'test command' to check whether a file exists and determine the type of a file.
Following are the syntaxes of the test command, and we can use any of these commands:
test expression
[ expression ]
[[ expression ]]
We are required to use a single bracket '[' command to make our script portable for all POSIX shells. The upgraded version of the test command contains double brackets '[[' which is supported on most of the modern systems using Bash, Zsh, and Ksh as a default shell.
Check If File Exists
While checking if a file exists, the most commonly used file operators are -e and -f. The '-e' option is used to check whether a file exists regardless of the type, while the '-f' option is used to return true value only if the file is a regular file (not a directory or a device).
The most common option to check if the file exists or not is to use the test command with the 'if conditional statement'.
Following are the examples to check whether the 'read_file.txt' file exists:
Method 1
#!/bin/bash
File=read_file.txt
if test -f "$File"; then
echo "$File exist "
fi
Method 2
#!/bin/bash
File=read_file.txt
if [ -f "$File" ]; then
echo "$File exist "
fi
Method 3
#!/bin/bash
File=read_file.txt
if [[ -f "$File" ]]; then
echo "$File exist "
fi
Output
Output for all the three methods will be as below because we have a file (read_file.txt) present in the directory:
read_file.txt exist
If we want to perform an action which will provide a result based on whether the file exists or not, we can use the if/then construct in the following way:
Example
#!/bin/bash
File=read_file.txt
if [ -f "$File" ]; then
echo "$File exist"
else
echo "$File does not exist"
fi
Output
read_file.txt exist
We can also use the test command without the if statement. We can use any of the following methods:
Method 1
#!/bin/bash
File=read_file.txt
test -f read_file.txt && echo "$File exist"
Method 2
#!/bin/bash
File=read_file.txt
[ -f read_file.txt ] && echo "$File exist"
Method 3
#!/bin/bash
File=read_file.txt
[[ -f read_file.txt ]] && echo "$File exist"
Output
Output for all the three methods will be as below because we have a file (read_file.txt) present in the directory:
read_file.txt exist
If there are several commands to be run after the && operator, then enclose the commands in curly brackets separated by semicolon(;) or AND (&&), i.e.:
Example
#!/bin/bash
File=read_file.txt
[ -f read_file.txt ] && { echo "$File exist"; echo "Task Completed"; }
Unlike &&, the statement after the || operator is executed only if the exit status of the test command is 'false'.
Example
#!/bin/bash
File=read_file.txt
[ -f read_file.txt ] && echo "$File exist" || echo "$File does not exist"
Output
read_file.txt exist
These are the commonly used methods in Bash to check whether the file exists or not.
Check If Directory Exists
The operator '-d' allows us to test whether a file is a directory or not.
Following are the methods to check whether the 'Javatpoint' directory exists:
Method 1
#!/bin/bash
File=Javatpoint
if [ -d "$File" ]; then
echo "$File is a directory"
fi
Method 2
#!/bin/bash
File=Javatpoint
[ -d "$File" ] && echo "$File is a directory"
Note: We can also use double brackets '[[' instead of a single bracket '['.
Output
Output for both the above methods will be as below as we have a directory (named Javatpoint) present in the specified location:
Javatpoint is a directory
Check IF File does not Exist
The test expression can be negated by using the exclamation mark (! -logical NOT operator). Check out the following example:
Example
#!/bin/bash
File=missing_read_file.txt
if [ ! -f "$File" ]; then
echo "$File does not exist"
fi
Above script can also be written as below:
#!/bin/bash
File=missing_read_file.txt
[ ! -f "$File" ] && echo "$File unavailable"
Output
missing_read_file.txt unavailable
Top comments (0)