diff --git a/README.md b/README.md index def46dd..50a085b 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,7 @@ A collection of pure POSIX `sh` alternatives to external processes * [Check if string ends with sub-string](#check-if-string-ends-with-sub-string) * [FILES](#files) * [Get the first N lines of a file](#get-the-first-n-lines-of-a-file) + * [Get the number of lines in a file](#get-the-number-of-lines-in-a-file) @@ -177,3 +178,30 @@ PS1='➜ ' $ head 1 ~/.bashrc # Prompt ``` + +## Get the number of lines in a file + +Alternative to `wc -l`. + +**Example Function:** + +```sh +lines() { + # Usage: lines "file" + while read -r _; do + lines=$((lines+1)) + done < "$1" + + printf '%s\n' "$lines" +} +``` + +**Example Usage:** + +```shell +$ lines ~/.bashrc +48 + +$ lines_loop ~/.bashrc +48 +```