为什么 zsh `for` 循环出错时出现 `command not found: filename.txt`?尽管 filename.txt 是参数而不是命令
why does zsh `for` loop error with `command not found: filename.txt`? despite filename.txt being a parameter and not a command
以下 shell 历史记录显示了两次重命名文件的尝试,第二次尝试成功了。但是第一次尝试没有,我不知道为什么...
❯ ls
step-10.txt step-12.txt step-14.txt step-16.txt step-18.txt step-1.txt step-3.txt step-5.txt step-7.txt step-9.txt
step-11.txt step-13.txt step-15.txt step-17.txt step-19.txt step-2.txt step-4.txt step-6.txt step-8.txt
❯ for f in *.txt; do
newname=$(echo $f | sed 's/txt/md/')\
mv $f $newname
done
zsh: command not found: step-10.txt
zsh: command not found: step-11.txt
zsh: command not found: step-12.txt
zsh: command not found: step-13.txt
zsh: command not found: step-14.txt
zsh: command not found: step-15.txt
zsh: command not found: step-16.txt
zsh: command not found: step-17.txt
zsh: command not found: step-18.txt
zsh: command not found: step-19.txt
zsh: command not found: step-1.txt
zsh: command not found: step-2.txt
zsh: command not found: step-3.txt
zsh: command not found: step-4.txt
zsh: command not found: step-5.txt
zsh: command not found: step-6.txt
zsh: command not found: step-7.txt
zsh: command not found: step-8.txt
zsh: command not found: step-9.txt
↵ Error. Exit status 127.
❯ for f in *.txt; do
mv $f $(echo $f | sed 's/txt/md/')
done
❯ ls
step-10.md step-12.md step-14.md step-16.md step-18.md step-1.md step-3.md step-5.md step-7.md step-9.md
step-11.md step-13.md step-15.md step-17.md step-19.md step-2.md step-4.md step-6.md step-8.md
为什么当我将 $(echo $f | sed 's/txt/md/')
放在 mv
行时它起作用,但当它被分配给变量时却不起作用?当我的循环提供 $f
作为 mv
命令的参数时,为什么它说 command not found: $f
?
那个反斜杠表示它正在计算
newname=$(echo $f | sed 's/txt/md/')mv $f $newname
这当然将文件名视为 运行 的命令名称(mv 是 newname
变量的一部分)
这里根本不需要sed
;只需使用参数扩展:
for f in *.txt; do
mv "$f" "${f/%txt/md}"
done
还有几个名为 rename
(prename
、rename.ul
等,取决于 OS)的不同程序可以批量重命名文件;检查您已安装的手册页(如果有)和使用情况。
以下 shell 历史记录显示了两次重命名文件的尝试,第二次尝试成功了。但是第一次尝试没有,我不知道为什么...
❯ ls
step-10.txt step-12.txt step-14.txt step-16.txt step-18.txt step-1.txt step-3.txt step-5.txt step-7.txt step-9.txt
step-11.txt step-13.txt step-15.txt step-17.txt step-19.txt step-2.txt step-4.txt step-6.txt step-8.txt
❯ for f in *.txt; do
newname=$(echo $f | sed 's/txt/md/')\
mv $f $newname
done
zsh: command not found: step-10.txt
zsh: command not found: step-11.txt
zsh: command not found: step-12.txt
zsh: command not found: step-13.txt
zsh: command not found: step-14.txt
zsh: command not found: step-15.txt
zsh: command not found: step-16.txt
zsh: command not found: step-17.txt
zsh: command not found: step-18.txt
zsh: command not found: step-19.txt
zsh: command not found: step-1.txt
zsh: command not found: step-2.txt
zsh: command not found: step-3.txt
zsh: command not found: step-4.txt
zsh: command not found: step-5.txt
zsh: command not found: step-6.txt
zsh: command not found: step-7.txt
zsh: command not found: step-8.txt
zsh: command not found: step-9.txt
↵ Error. Exit status 127.
❯ for f in *.txt; do
mv $f $(echo $f | sed 's/txt/md/')
done
❯ ls
step-10.md step-12.md step-14.md step-16.md step-18.md step-1.md step-3.md step-5.md step-7.md step-9.md
step-11.md step-13.md step-15.md step-17.md step-19.md step-2.md step-4.md step-6.md step-8.md
为什么当我将 $(echo $f | sed 's/txt/md/')
放在 mv
行时它起作用,但当它被分配给变量时却不起作用?当我的循环提供 $f
作为 mv
命令的参数时,为什么它说 command not found: $f
?
那个反斜杠表示它正在计算
newname=$(echo $f | sed 's/txt/md/')mv $f $newname
这当然将文件名视为 运行 的命令名称(mv 是 newname
变量的一部分)
这里根本不需要sed
;只需使用参数扩展:
for f in *.txt; do
mv "$f" "${f/%txt/md}"
done
还有几个名为 rename
(prename
、rename.ul
等,取决于 OS)的不同程序可以批量重命名文件;检查您已安装的手册页(如果有)和使用情况。