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