bashtextawkgreptac

How to only read the last line from a text file


I am working on a tool project. I need to grab the last line from a file & assign into a variable. This is what I have tried:

line=$(head -n $NF input_file)
echo $line

Maybe I could read the file in reverse then use

line=$(head -n $1 input_file)
echo $line

Any ideas are welcome.


Solution

  • Use tail ;)

    line=$(tail -n 1 input_file)
    echo $line