1

I have a function that creates a new markdown file, but I was wondering how can I simplify this?

  today=$(date +"%F")
  title=$1:l
  clean_title=${title//[^a-zA-Z0-9]/-}
  filename="$today-$clean_title.markdown"

I figured I could combine the title/clean_title into one, but when I try something like ${1:l... or ${$1:l... I get console error about bad substitution. Is this possible?

1

Nest the ${...} bits, though do also consider how readable the code will be for future readers.

() { clean_title=${${1:l}//[^a-zA-Z0-9]/-}; printf "$clean_title" } " BLA "

Also the A-Z match probably doesn't make much sense given the earlier lowercase...

1

In zsh, you can nest parameter expansion operators. You can also get the date with the prompt expansion of %D{%F} (zsh also has a strftime builtin in the zsh/datetime module if you want some more verbose and readable code).

filename=${(%):-%D{%F}}-${${1//[^[:alnum:]]/-}:l}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.