Search code examples
bashshellshksh

How to parse a file with one line in shell


This is probably a silly question, but what is the best way to get the string from a text file which has only one line using shell. I know I can use while read, but seems like it might be unecessary.

The file I am reading contains a string directory path, and all I want to do is set that string value to a variable.

if [ -e ${DIR}/test.txt ] ; then
    # set the string within the file to a variable
fi

Solution

  • My personal preference is:

    DIR=$( cat file )
    

    but

    read DIR < file
    

    works nicely as well