From a020c7ade368896f31d6185d29a3ee139dec118a Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Thu, 19 Sep 2019 11:22:33 +0300 Subject: [PATCH] docs: update --- README.md | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/README.md b/README.md index f883f2d..def46dd 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,8 @@ A collection of pure POSIX `sh` alternatives to external processes * [Check if string contains a sub-string](#check-if-string-contains-a-sub-string) * [Check if string starts with sub-string](#check-if-string-starts-with-sub-string) * [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) @@ -145,3 +147,33 @@ case $var in ;; esac ``` + +# FILES + +## Get the first N lines of a file + +Alternative to the `head` command. + +**Example Function:** + +```sh +head() { + # Usage: head "n" "file" + while read -r line; do + [ "$i" = "$1" ] && break + printf '%s\n' "$line" + i=$((i+1)) + done < "$2" +} +``` + +**Example Usage:** + +```shell +$ head 2 ~/.bashrc +# Prompt +PS1='➜ ' + +$ head 1 ~/.bashrc +# Prompt +```