save number of lines of a text file in a variable shell file

I'm working on terminal and running a .sh file and I want to count the lines in another file and save it as a variable to be used in this .sh file. I'm using this command sed -n '$=' /lines.txt when I run this it displays the number of lines on terminal but I want to save it in a variable named numberoflines. If anyone could advise how to do this.

1 Answer

numberoflines=$(sed -n '$=' /tmp/lines.txt)

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like