--- tags: - "\U0001F6A9purpose/\U0001F5FA️guide" - ⭐topic/⌗shell/zsh --- # `head` & `tail` in pure zsh It is very common to use `head` and `tail` in shells. But as far as I can tell, they are not deployed as builtins for any shell and are instead separate binaries, which isn’t good for optimization. This is true for zsh as well. But, at least with zsh, [parameter expansion](https://zsh.sourceforge.io/Doc/Release/Expansion.html#Parameter-Expansion) can replace the need for them easily. For example, if you wanted to pick the first editor from a list of potential choices: ```zsh export EDITOR=$(whence -p nvim vim vi micro nano emacs | head -1) ``` You could easily replace `head` with: ```zsh export EDITOR=${s$(whence -p nvim vim vi micro nano emacs)[(f)1]} ``` Where `(f)` is the [array subscript flag](https://zsh.sourceforge.io/Doc/Release/Parameters.html#Subscript-Flags) that, when used in combination with scalars (strings) instead of arrays, will break the scalar into lines. This will thus return the first line of the [`whence` builtin](https://zsh.sourceforge.io/Doc/Release/Shell-Builtin-Commands.html#Shell-Builtin-Commands#index-whence). You could also replace `whence` with a subscript into the [`$commands` array](https://zsh.sourceforge.io/Doc/Release/Zsh-Modules.html#index-commands), but that’s another concept entirely.