Bash Script To Generate Markdown File With Yaml Header

Dec 27, 2022

Usage: . new.sh tutorials/bash/bash-script-to-generate-markdown-file-with-yaml-header

Output

---title: "Bash Script To Generate Markdown File With Yaml Header"description: date: 2022-12-27T17:36:55+08:00tags: ["bash"]weight: 1000---

Bash Script: new.sh

#!/bin/bash# file path formatFILE="content/$1.md"# split by /PARTS=(${1//\// })# get last path, replace - with spaceTITLE=$(echo ${PARTS[-1]} | tr "-" " ")# capitalize each wordTITLE=$(echo $TITLE | sed -e "s/\b\(.\)/\u\1/g")# use previous directory as tagTAG=${PARTS[-2]}if test -f "$FILE"; then    echo "$FILE exists"else echo "---title: \"$TITLE\"description: date: $(date -Is)tags: [\"$TAG\"]weight: 1000---" > $FILE && echo "$FILE created"fi

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.