以与 Bash 相同的方式转义文件名
当我在 bash
中使用“tab”键时(当您开始输入文件名并且希望它完成时),bash
正确转义文件名,并且如果我完全使用“转义”文件名,它可以工作。
例如:
An-Beat - Mentally Insine (Original Mix).mp3
=> bash
使用“TAB”转义后 An-Beat\ -\ Mentally\ Insine\ \(Original\ Mix\).mp3
我正在寻找 bash
的函数,该函数将以相同的方式转义文件名“tab”转义文件名。
When I use the "tab" key in bash
(when you have started to type the filename and you want it to complete), bash
escapes the filename correctly, and if I use exactly that "escaped" filename, it works.
For Instance:
An-Beat - Mentally Insine (Original Mix).mp3
=> After bash
Escapes It Using "TAB"An-Beat\ -\ Mentally\ Insine\ \(Original\ Mix\).mp3
I'm search for a function for bash
that will escape a filename the same way "tab" escapes filenames.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
使用
printf
(1):将返回
Use
printf
(1):will return
我将详细阐述sehe对此的回应。
如果您想将要转换的参数传递为 shell 脚本参数,请将参数括在 " 中。
我真的很喜欢 printf 解决方案,因为它会处理每个特殊字符,就像 bash 一样。
I'm going to elaborate on sehe's response on this one.
If you want to pass the argument to be converted as a shell script parameter, encase the parameter in "'s.
I really like the printf solution, since it does every special character, just like bash.
“sehe”的解决方案工作正常,此外,您还可以使用双引号(“)而不是单撇号(')来使用变量:
当然,字符串本身可能不包含 $ 或 " ,否则您必须通过splash \$ 手动转义这些内容。
The solution from "sehe" works fine, in addition, you can also use double quotes (") instead of single apostrophe (') to by able to use variables:
Of course the string may not contain $ or " itself or you have to escape those manulally by splash \$.
这将输出转义的文件名,并且也可以使用 unicode 字符,printf 方法不适用于中文,它输出类似 $'\206\305...'
this will output the escaped filenames, and also work with unicode characters, printf method does not work with Chinese, it outputs something like $'\206\305...'
我可能有点晚了,但对我有用的是:
这样它也可以转义像
!
或'
这样的字符。I may be a little late to the party but what worked for me is:
This way it also escapes characters like
!
or'
.获取转义完整路径的解决方案
我创建了一个可移植函数来获取当前目录中所有项目的所有转义路径,并在 macOS 和 Linux 上进行了测试(需要安装 GNU bash)。
这是一个严格的测试用例场景:
仅获取转义基名的解决方案
这个(也是可移植的)函数将为您提供当前目录中所有项目的转义基名(这次不包括当前目录)。
在同一测试目录
test dir
中运行escbasenames
会给出转义的基名:最后注释
请注意,如果路径/文件名包含换行符或制表符,它将被编码为 ANSI- C 字符串。终端中的自动补全功能也可以使用 ANSI-C 字符串补全。示例 ANSI-C 字符串自动完成输出类似于
file\twith\tmany\ttabs.txt' touch 'file @*&$()!#[]:.txt' touch 'file with newlines.txt'my$'\n'newline$'\n'dir/
或my$'\t'tabbed$'\t'file.txt
。在测试目录
test dir
中执行escpaths
给出转义输出:仅获取转义基名的解决方案
这个(也是可移植的)函数将为您提供当前目录中所有项目的转义基名(这次不包括当前目录)。
在同一测试目录
test dir
中运行escbasenames
会给出转义的基名:最后注释
请注意,如果路径/文件名包含换行符或制表符,它将被编码为 ANSI- C 字符串。终端中的自动补全功能也可以使用 ANSI-C 字符串补全。示例 ANSI-C 字符串自动完成输出类似于
file\nwith\nnewlines.txt'my$'\n'newline$'\n'dir/
或my$'\t'tabbed$'\t'file.txt
。最后注释
请注意,如果路径/文件名包含换行符或制表符,它将被编码为 ANSI- C 字符串。终端中的自动补全功能也可以使用 ANSI-C 字符串补全。示例 ANSI-C 字符串自动完成输出类似于
file\twith\tmany\ttabs.txt' touch 'file @*&$()!#[]:.txt' touch 'file with newlines.txt'my$'\n'newline$'\n'dir/
或my$'\t'tabbed$'\t'file.txt
。在测试目录
test dir
中执行escpaths
给出转义输出:仅获取转义基名的解决方案
这个(也是可移植的)函数将为您提供当前目录中所有项目的转义基名(这次不包括当前目录)。
在同一测试目录
test dir
中运行escbasenames
会给出转义的基名:最后注释
请注意,如果路径/文件名包含换行符或制表符,它将被编码为 ANSI- C 字符串。终端中的自动补全功能也可以使用 ANSI-C 字符串补全。示例 ANSI-C 字符串自动完成输出类似于
/.../test dir/file\nwith\nnewlines.txt'my$'\n'newline$'\n'dir/
或my$'\t'tabbed$'\t'file.txt
。仅获取转义基名的解决方案
这个(也是可移植的)函数将为您提供当前目录中所有项目的转义基名(这次不包括当前目录)。
在同一测试目录
test dir
中运行escbasenames
会给出转义的基名:最后注释
请注意,如果路径/文件名包含换行符或制表符,它将被编码为 ANSI- C 字符串。终端中的自动补全功能也可以使用 ANSI-C 字符串补全。示例 ANSI-C 字符串自动完成输出类似于
file\twith\tmany\ttabs.txt' touch 'file @*&$()!#[]:.txt' touch 'file with newlines.txt'my$'\n'newline$'\n'dir/
或my$'\t'tabbed$'\t'file.txt
。在测试目录
test dir
中执行escpaths
给出转义输出:仅获取转义基名的解决方案
这个(也是可移植的)函数将为您提供当前目录中所有项目的转义基名(这次不包括当前目录)。
在同一测试目录
test dir
中运行escbasenames
会给出转义的基名:最后注释
请注意,如果路径/文件名包含换行符或制表符,它将被编码为 ANSI- C 字符串。终端中的自动补全功能也可以使用 ANSI-C 字符串补全。示例 ANSI-C 字符串自动完成输出类似于
/.../test dir/file\twith\tmany\ttabs.txt' /.../test\ dir /.../test\ dir/An-Beat\ -\ Mentally\ Insine\ \(Original\ Mix\).mp3 /.../test\ dir/\\dir\ \ with\\\ backslashes\\\\\\ /.../test\ dir/\\file\ with\ \\\\backslashes\\ /.../test\ dir/crazy\(\*@\$\):\{\}\[\]dir: /.../test\ dir/dir\ with\ 字\ chinese\ 鳥鸟\ characters /.../test\ dir/file\ @\*\&\$\(\)\!#\[\]:.txtmy$'\n'newline$'\n'dir/
或my$'\t'tabbed$'\t'file.txt
。仅获取转义基名的解决方案
这个(也是可移植的)函数将为您提供当前目录中所有项目的转义基名(这次不包括当前目录)。
在同一测试目录
test dir
中运行escbasenames
会给出转义的基名:最后注释
请注意,如果路径/文件名包含换行符或制表符,它将被编码为 ANSI- C 字符串。终端中的自动补全功能也可以使用 ANSI-C 字符串补全。示例 ANSI-C 字符串自动完成输出类似于
file\twith\tmany\ttabs.txt' touch 'file @*&$()!#[]:.txt' touch 'file with newlines.txt'my$'\n'newline$'\n'dir/
或my$'\t'tabbed$'\t'file.txt
。在测试目录
test dir
中执行escpaths
给出转义输出:仅获取转义基名的解决方案
这个(也是可移植的)函数将为您提供当前目录中所有项目的转义基名(这次不包括当前目录)。
在同一测试目录
test dir
中运行escbasenames
会给出转义的基名:最后注释
请注意,如果路径/文件名包含换行符或制表符,它将被编码为 ANSI- C 字符串。终端中的自动补全功能也可以使用 ANSI-C 字符串补全。示例 ANSI-C 字符串自动完成输出类似于
file\twith\tmany\ttabs.txt' An-Beat\ -\ Mentally\ Insine\ \(Original\ Mix\).mp3 \\dir\ \ with\\\ backslashes\\\\\\ \\file\ with\ \\\\backslashes\\ crazy\(\*@\$\):\{\}\[\]dir: dir\ with\ 字\ chinese\ 鳥鸟\ characters file\ @\*\&\$\(\)\!#\[\]:.txtmy$'\n'newline$'\n'dir/
或my$'\t'tabbed$'\t'file.txt
。最后注释
请注意,如果路径/文件名包含换行符或制表符,它将被编码为 ANSI- C 字符串。终端中的自动补全功能也可以使用 ANSI-C 字符串补全。示例 ANSI-C 字符串自动完成输出类似于
file\twith\tmany\ttabs.txt' touch 'file @*&$()!#[]:.txt' touch 'file with newlines.txt'my$'\n'newline$'\n'dir/
或my$'\t'tabbed$'\t'file.txt
。在测试目录
test dir
中执行escpaths
给出转义输出:仅获取转义基名的解决方案
这个(也是可移植的)函数将为您提供当前目录中所有项目的转义基名(这次不包括当前目录)。
在同一测试目录
test dir
中运行escbasenames
会给出转义的基名:最后注释
请注意,如果路径/文件名包含换行符或制表符,它将被编码为 ANSI- C 字符串。终端中的自动补全功能也可以使用 ANSI-C 字符串补全。示例 ANSI-C 字符串自动完成输出类似于
/.../test dir/file\nwith\nnewlines.txt'my$'\n'newline$'\n'dir/
或my$'\t'tabbed$'\t'file.txt
。仅获取转义基名的解决方案
这个(也是可移植的)函数将为您提供当前目录中所有项目的转义基名(这次不包括当前目录)。
在同一测试目录
test dir
中运行escbasenames
会给出转义的基名:最后注释
请注意,如果路径/文件名包含换行符或制表符,它将被编码为 ANSI- C 字符串。终端中的自动补全功能也可以使用 ANSI-C 字符串补全。示例 ANSI-C 字符串自动完成输出类似于
file\twith\tmany\ttabs.txt' touch 'file @*&$()!#[]:.txt' touch 'file with newlines.txt'my$'\n'newline$'\n'dir/
或my$'\t'tabbed$'\t'file.txt
。在测试目录
test dir
中执行escpaths
给出转义输出:仅获取转义基名的解决方案
这个(也是可移植的)函数将为您提供当前目录中所有项目的转义基名(这次不包括当前目录)。
在同一测试目录
test dir
中运行escbasenames
会给出转义的基名:最后注释
请注意,如果路径/文件名包含换行符或制表符,它将被编码为 ANSI- C 字符串。终端中的自动补全功能也可以使用 ANSI-C 字符串补全。示例 ANSI-C 字符串自动完成输出类似于
/.../test dir/file\twith\tmany\ttabs.txt' /.../test\ dir /.../test\ dir/An-Beat\ -\ Mentally\ Insine\ \(Original\ Mix\).mp3 /.../test\ dir/\\dir\ \ with\\\ backslashes\\\\\\ /.../test\ dir/\\file\ with\ \\\\backslashes\\ /.../test\ dir/crazy\(\*@\$\):\{\}\[\]dir: /.../test\ dir/dir\ with\ 字\ chinese\ 鳥鸟\ characters /.../test\ dir/file\ @\*\&\$\(\)\!#\[\]:.txtmy$'\n'newline$'\n'dir/
或my$'\t'tabbed$'\t'file.txt
。仅获取转义基名的解决方案
这个(也是可移植的)函数将为您提供当前目录中所有项目的转义基名(这次不包括当前目录)。
在同一测试目录
test dir
中运行escbasenames
会给出转义的基名:最后注释
请注意,如果路径/文件名包含换行符或制表符,它将被编码为 ANSI- C 字符串。终端中的自动补全功能也可以使用 ANSI-C 字符串补全。示例 ANSI-C 字符串自动完成输出类似于
file\twith\tmany\ttabs.txt' touch 'file @*&$()!#[]:.txt' touch 'file with newlines.txt'my$'\n'newline$'\n'dir/
或my$'\t'tabbed$'\t'file.txt
。在测试目录
test dir
中执行escpaths
给出转义输出:仅获取转义基名的解决方案
这个(也是可移植的)函数将为您提供当前目录中所有项目的转义基名(这次不包括当前目录)。
在同一测试目录
test dir
中运行escbasenames
会给出转义的基名:最后注释
请注意,如果路径/文件名包含换行符或制表符,它将被编码为 ANSI- C 字符串。终端中的自动补全功能也可以使用 ANSI-C 字符串补全。示例 ANSI-C 字符串自动完成输出类似于
my$'\n'newline$'\n'dir/
或my$'\t'tabbed$'\t'file.txt
。Solution to get escaped full paths
I've created a portable function to get all the escaped paths to all items in the current directory, tested on macOS and Linux (requires GNU bash installed).
Here's a rigorous test case scenario:
Solution to get escaped basenames only
This (also portable) function will get you the escaped basenames of all items in the current directory (this time excluding the current directory).
Running
escbasenames
in the same test directorytest dir
gives the escaped basenames:Final notes
Note that if the path/filename contains newlines or tabs, it will be encoded as an ANSI-C string. Autocompletion in the terminal also completes with ANSI-C strings. Example ANSI-C string autocompletion outputs would look like
file\twith\tmany\ttabs.txt' touch 'file @*&$()!#[]:.txt' touch 'file with newlines.txt'my$'\n'newline$'\n'dir/
ormy$'\t'tabbed$'\t'file.txt
.Executing
escpaths
in the test directorytest dir
gives the escaped output:Solution to get escaped basenames only
This (also portable) function will get you the escaped basenames of all items in the current directory (this time excluding the current directory).
Running
escbasenames
in the same test directorytest dir
gives the escaped basenames:Final notes
Note that if the path/filename contains newlines or tabs, it will be encoded as an ANSI-C string. Autocompletion in the terminal also completes with ANSI-C strings. Example ANSI-C string autocompletion outputs would look like
file\nwith\nnewlines.txt'my$'\n'newline$'\n'dir/
ormy$'\t'tabbed$'\t'file.txt
.Final notes
Note that if the path/filename contains newlines or tabs, it will be encoded as an ANSI-C string. Autocompletion in the terminal also completes with ANSI-C strings. Example ANSI-C string autocompletion outputs would look like
file\twith\tmany\ttabs.txt' touch 'file @*&$()!#[]:.txt' touch 'file with newlines.txt'my$'\n'newline$'\n'dir/
ormy$'\t'tabbed$'\t'file.txt
.Executing
escpaths
in the test directorytest dir
gives the escaped output:Solution to get escaped basenames only
This (also portable) function will get you the escaped basenames of all items in the current directory (this time excluding the current directory).
Running
escbasenames
in the same test directorytest dir
gives the escaped basenames:Final notes
Note that if the path/filename contains newlines or tabs, it will be encoded as an ANSI-C string. Autocompletion in the terminal also completes with ANSI-C strings. Example ANSI-C string autocompletion outputs would look like
/.../test dir/file\nwith\nnewlines.txt'my$'\n'newline$'\n'dir/
ormy$'\t'tabbed$'\t'file.txt
.Solution to get escaped basenames only
This (also portable) function will get you the escaped basenames of all items in the current directory (this time excluding the current directory).
Running
escbasenames
in the same test directorytest dir
gives the escaped basenames:Final notes
Note that if the path/filename contains newlines or tabs, it will be encoded as an ANSI-C string. Autocompletion in the terminal also completes with ANSI-C strings. Example ANSI-C string autocompletion outputs would look like
file\twith\tmany\ttabs.txt' touch 'file @*&$()!#[]:.txt' touch 'file with newlines.txt'my$'\n'newline$'\n'dir/
ormy$'\t'tabbed$'\t'file.txt
.Executing
escpaths
in the test directorytest dir
gives the escaped output:Solution to get escaped basenames only
This (also portable) function will get you the escaped basenames of all items in the current directory (this time excluding the current directory).
Running
escbasenames
in the same test directorytest dir
gives the escaped basenames:Final notes
Note that if the path/filename contains newlines or tabs, it will be encoded as an ANSI-C string. Autocompletion in the terminal also completes with ANSI-C strings. Example ANSI-C string autocompletion outputs would look like
/.../test dir/file\twith\tmany\ttabs.txt' /.../test\ dir /.../test\ dir/An-Beat\ -\ Mentally\ Insine\ \(Original\ Mix\).mp3 /.../test\ dir/\\dir\ \ with\\\ backslashes\\\\\\ /.../test\ dir/\\file\ with\ \\\\backslashes\\ /.../test\ dir/crazy\(\*@\$\):\{\}\[\]dir: /.../test\ dir/dir\ with\ 字\ chinese\ 鳥鸟\ characters /.../test\ dir/file\ @\*\&\$\(\)\!#\[\]:.txtmy$'\n'newline$'\n'dir/
ormy$'\t'tabbed$'\t'file.txt
.Solution to get escaped basenames only
This (also portable) function will get you the escaped basenames of all items in the current directory (this time excluding the current directory).
Running
escbasenames
in the same test directorytest dir
gives the escaped basenames:Final notes
Note that if the path/filename contains newlines or tabs, it will be encoded as an ANSI-C string. Autocompletion in the terminal also completes with ANSI-C strings. Example ANSI-C string autocompletion outputs would look like
file\twith\tmany\ttabs.txt' touch 'file @*&$()!#[]:.txt' touch 'file with newlines.txt'my$'\n'newline$'\n'dir/
ormy$'\t'tabbed$'\t'file.txt
.Executing
escpaths
in the test directorytest dir
gives the escaped output:Solution to get escaped basenames only
This (also portable) function will get you the escaped basenames of all items in the current directory (this time excluding the current directory).
Running
escbasenames
in the same test directorytest dir
gives the escaped basenames:Final notes
Note that if the path/filename contains newlines or tabs, it will be encoded as an ANSI-C string. Autocompletion in the terminal also completes with ANSI-C strings. Example ANSI-C string autocompletion outputs would look like
file\twith\tmany\ttabs.txt' An-Beat\ -\ Mentally\ Insine\ \(Original\ Mix\).mp3 \\dir\ \ with\\\ backslashes\\\\\\ \\file\ with\ \\\\backslashes\\ crazy\(\*@\$\):\{\}\[\]dir: dir\ with\ 字\ chinese\ 鳥鸟\ characters file\ @\*\&\$\(\)\!#\[\]:.txtmy$'\n'newline$'\n'dir/
ormy$'\t'tabbed$'\t'file.txt
.Final notes
Note that if the path/filename contains newlines or tabs, it will be encoded as an ANSI-C string. Autocompletion in the terminal also completes with ANSI-C strings. Example ANSI-C string autocompletion outputs would look like
file\twith\tmany\ttabs.txt' touch 'file @*&$()!#[]:.txt' touch 'file with newlines.txt'my$'\n'newline$'\n'dir/
ormy$'\t'tabbed$'\t'file.txt
.Executing
escpaths
in the test directorytest dir
gives the escaped output:Solution to get escaped basenames only
This (also portable) function will get you the escaped basenames of all items in the current directory (this time excluding the current directory).
Running
escbasenames
in the same test directorytest dir
gives the escaped basenames:Final notes
Note that if the path/filename contains newlines or tabs, it will be encoded as an ANSI-C string. Autocompletion in the terminal also completes with ANSI-C strings. Example ANSI-C string autocompletion outputs would look like
/.../test dir/file\nwith\nnewlines.txt'my$'\n'newline$'\n'dir/
ormy$'\t'tabbed$'\t'file.txt
.Solution to get escaped basenames only
This (also portable) function will get you the escaped basenames of all items in the current directory (this time excluding the current directory).
Running
escbasenames
in the same test directorytest dir
gives the escaped basenames:Final notes
Note that if the path/filename contains newlines or tabs, it will be encoded as an ANSI-C string. Autocompletion in the terminal also completes with ANSI-C strings. Example ANSI-C string autocompletion outputs would look like
file\twith\tmany\ttabs.txt' touch 'file @*&$()!#[]:.txt' touch 'file with newlines.txt'my$'\n'newline$'\n'dir/
ormy$'\t'tabbed$'\t'file.txt
.Executing
escpaths
in the test directorytest dir
gives the escaped output:Solution to get escaped basenames only
This (also portable) function will get you the escaped basenames of all items in the current directory (this time excluding the current directory).
Running
escbasenames
in the same test directorytest dir
gives the escaped basenames:Final notes
Note that if the path/filename contains newlines or tabs, it will be encoded as an ANSI-C string. Autocompletion in the terminal also completes with ANSI-C strings. Example ANSI-C string autocompletion outputs would look like
/.../test dir/file\twith\tmany\ttabs.txt' /.../test\ dir /.../test\ dir/An-Beat\ -\ Mentally\ Insine\ \(Original\ Mix\).mp3 /.../test\ dir/\\dir\ \ with\\\ backslashes\\\\\\ /.../test\ dir/\\file\ with\ \\\\backslashes\\ /.../test\ dir/crazy\(\*@\$\):\{\}\[\]dir: /.../test\ dir/dir\ with\ 字\ chinese\ 鳥鸟\ characters /.../test\ dir/file\ @\*\&\$\(\)\!#\[\]:.txtmy$'\n'newline$'\n'dir/
ormy$'\t'tabbed$'\t'file.txt
.Solution to get escaped basenames only
This (also portable) function will get you the escaped basenames of all items in the current directory (this time excluding the current directory).
Running
escbasenames
in the same test directorytest dir
gives the escaped basenames:Final notes
Note that if the path/filename contains newlines or tabs, it will be encoded as an ANSI-C string. Autocompletion in the terminal also completes with ANSI-C strings. Example ANSI-C string autocompletion outputs would look like
file\twith\tmany\ttabs.txt' touch 'file @*&$()!#[]:.txt' touch 'file with newlines.txt'my$'\n'newline$'\n'dir/
ormy$'\t'tabbed$'\t'file.txt
.Executing
escpaths
in the test directorytest dir
gives the escaped output:Solution to get escaped basenames only
This (also portable) function will get you the escaped basenames of all items in the current directory (this time excluding the current directory).
Running
escbasenames
in the same test directorytest dir
gives the escaped basenames:Final notes
Note that if the path/filename contains newlines or tabs, it will be encoded as an ANSI-C string. Autocompletion in the terminal also completes with ANSI-C strings. Example ANSI-C string autocompletion outputs would look like
my$'\n'newline$'\n'dir/
ormy$'\t'tabbed$'\t'file.txt
.