I want to read a text file called history.txt that contains multiple lines and store the content of the last line into a variable called "tag".
Thanks
To look at the last few lines of a file, use the tail command. tail works the same way as head: type tail and the filename to see the last 10 lines of that file, or type tail -number filename to see the last number lines of the file.
${str##*$'\n'} will remove the longest match till \n from start of the string thus leaving only the last line in input. Show activity on this post. Then, the first line would be ${lines[0]} , and the last line would be ${lines[-1]} .
We use the read command with -r argument to read the contents without escaping the backslash character. We read the content of each line and store that in the variable line and inside the while loop we echo with a formatted -e argument to use special characters like \n and print the contents of the line variable.
tag=$( tail -n 1 history.txt )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With