bash - 无法在 rmdir 中使用带波浪号的文件夹路径

标签 bash macos path filenames tilde-expansion

我需要能够删除与任何用户系统的 Documents 文件夹相关的目录。

rmdir: ~/Documents/Folder: No such file or directory

如果我手动输入扩展路径(/Users/ricky/Documents/Folder),它工作正常。

我认为 bash 会自动扩展路径开头的波浪号?

更新:

按照建议尝试了一堆不同的方法后,我现在非常有信心问题出在我如何存储路径上。我从逐行读取的文本文件中获取路径:

...
export_folder_path="$(echo $line | cut -f2 -d=)"
...

echo $export_folder_path
rmdir $export_folder_path
rmdir "$HOME/Documents/Folder\ 1"

输出如下:

$HOME/Documents/Folder\ 1
rmdir: $HOME/Documents/Folder\ 1: No such file or directory
rmdir: /Users/ricky/Documents/Folder\ 1: Directory not empty (This is actually what I want)

我无法弄清楚手动输入导出路径和使用变量之间有什么区别。为什么变量拒绝扩展$HOME?我尝试了许多添加引号的变体,但没有成功。

最佳答案

波形符扩展并非在所有情况下都有效。您可以改为使用 HOME 变量:

rmdir $HOME/Documents/Folder

来自bash manual :

Tilde Expansion If a word begins with an unquoted tilde character ('~'), all of the characters preceding the first unquoted slash (or all characters, if there is no unquoted slash) are considered a tilde-prefix. If none of the characters in the tilde-prefix are quoted, the characters in the tilde-prefix following the tilde are treated as a possible login name. If this login name is the null string, the tilde is replaced with the value of the shell parameter HOME. If HOME is unset, the home directory of the user executing the shell is substituted instead. Otherwise, the tilde-prefix is replaced with the home directory associated with the specified login name.

If the tilde-prefix is a '~+', the value of the shell variable PWD replaces the tilde-prefix. If the tilde-prefix is a '~-', the value of the shell variable OLDPWD, if it is set, is substituted. If the characters following the tilde in the tilde-prefix consist of a number N, optionally prefixed by a '+' or a '-', the tilde-prefix is replaced with the corresponding element from the directory st, as it would be displayed by the dirs builtin invoked with the tilde- prefix as an argument. If the characters following the tilde in the tilde-prefix consist of a number without a leading '+' or '-', '+' is assumed.

If the login name is invalid, or the tilde expansion fails, the word is unchanged.

关于bash - 无法在 rmdir 中使用带波浪号的文件夹路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41329592/

相关文章:

python - 如何在 Mac OSX 10.5.8 上为 Google App Engine 安装 PIL?

css - 如何在 OSX 的 vscode 中禁用所有自动格式化?

android - 沿着直线(通过路径?)

windows - 文件路径太长无法删除

java - 使用 File.separator 的相对路径

c - 在 Linux 中查找命令的内置和可执行文件的路径

bash - 如何在 Makefile 中运行子 Shell 脚本?

linux - 解析GETOPTS中带引号的字符串

linux - 将时间转换为 unix 时间戳

iphone - 在 iOS 应用程序中加入自定义单词词典