zsh扩展根据目录名创建文件名

zsh expansion to create file name based on the directory name

我正在尝试使用 pandoc 将目录中的所有 .markdown 文件编译成一个 pdf,pdf 的名称是目录名称加上 space 和 notes.pdf。如果我硬编码 pdf 名称,我可以将所有 markdown 文件编译成一个 pdf,但是当我尝试生成 pdf 名称时,它失败了。

例如,如果我在一个文件路径名为path/to/markdown files的目录中,我想编译成一个名为markdown files notes.pdf.

的pdf

我试过以下方法:

pandoc *.markdown -o $PWD notes.pdf

错误 - "Pandoc can convert to PDF, but not from PDF."

pandoc *.markdown -o ${PWD notes.pdf}

错误 - "bad substitution"

pandoc *.markdown -o '$PWD notes.pdf'

生成名为 $PWD(:t) 的 pdf notes.pdf

pandoc *.markdown -o $PWD" notes.pdf"

错误="no matches found"

有谁知道我怎样才能让它工作?

非常感谢

$PWD:t 扩展为当前目录的基本名称。这需要不加引号或双引号,而不是单引号,因为单引号会阻止扩展。 space 也需要被引用,因为它是文件名的一部分。

pandoc -o "$PWD:t notes.pdf" *.markdown

请注意,这使用了 zsh 语法。如果您需要在纯 sh 中使用它(例如对于 makefile),您可以使用

pandoc -o "${PWD##*/} notes.pdf" *.markdown

(在 makefile 中,加倍 $。)