以与 Bash 相同的方式转义文件名

发布于 2024-10-31 12:28:12 字数 351 浏览 2 评论 0原文

当我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

不再见 2024-11-07 12:28:12

使用printf (1):

x='a real \good %* load of c$rap'
x=$(printf '%q' "$x")
echo $x

将返回

a\ real\ \\good\ %\*\ load\ of\ c\$rap

Use printf (1):

x='a real \good %* load of c$rap'
x=$(printf '%q' "$x")
echo $x

will return

a\ real\ \\good\ %\*\ load\ of\ c\$rap
忱杏 2024-11-07 12:28:12

我将详细阐述sehe对此的回应。

如果您想将要转换的参数传递为 shell 脚本参数,请将参数括在 " 中。

#!/bin/bash
x=$(printf '%q' "$1")
echo $x

我真的很喜欢 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.

#!/bin/bash
x=$(printf '%q' "$1")
echo $x

I really like the printf solution, since it does every special character, just like bash.

方觉久 2024-11-07 12:28:12
$ string="An-Beat - Mentally Insine (Original Mix).mp3"
$ echo ${string// /\\ }
An-Beat\ -\ Mentally\ Insine\ (Original\ Mix).mp3
$ string=${string// /\\ }
$ echo ${string//(/\\( }
An-Beat - Mentally Insine \( Original Mix).mp3
$ string="An-Beat - Mentally Insine (Original Mix).mp3"
$ echo ${string// /\\ }
An-Beat\ -\ Mentally\ Insine\ (Original\ Mix).mp3
$ string=${string// /\\ }
$ echo ${string//(/\\( }
An-Beat - Mentally Insine \( Original Mix).mp3
瑾夏年华 2024-11-07 12:28:12

“sehe”的解决方案工作正常,此外,您还可以使用双引号(“)而不是单撇号(')来使用变量:

x="a real \good %* load of crap from ${USER}"
echo $(printf '%q' "$x")

当然,字符串本身可能不包含 $ 或 " ,否则您必须通过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:

x="a real \good %* load of crap from ${USER}"
echo $(printf '%q' "$x")

Of course the string may not contain $ or " itself or you have to escape those manulally by splash \$.

冰之心 2024-11-07 12:28:12
ls  --quoting-style=escape /somedir

这将输出转义的文件名,并且也可以使用 unicode 字符,printf 方法不适用于中文,它输出类似 $'\206\305...'

ls  --quoting-style=escape /somedir

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...'

野心澎湃 2024-11-07 12:28:12

我可能有点晚了,但对我有用的是:

ls  --quoting-style=shell-escape

这样它也可以转义像 !' 这样的字符。

I may be a little late to the party but what worked for me is:

ls  --quoting-style=shell-escape

This way it also escapes characters like ! or '.

趁微风不噪 2024-11-07 12:28:12

我正在寻找一个 bash 函数,它可以像“tab”转义文件名一样转义文件名。

获取转义完整路径的解决方案

我创建了一个可移植函数来获取当前目录中所有项目的所有转义路径,并在 macOS 和 Linux 上进行了测试(需要安装 GNU bash)。

escpaths() {
    find "$PWD" -maxdepth 1 -print0 | xargs -0 -I {} bash -c 'printf "%q\n" "$0"' {} | sort
}

这是一个严格的测试用例场景:

# Generate test directory, containing test directories and files.
mkdir 'test dir'; cd 'test dir'

mkdir '\dir  with\ backslashes\\\'
touch '\file with \\backslashes\'
touch 'An-Beat - Mentally Insine (Original Mix).mp3'
mkdir 'crazy(*@$):{}[]dir:'
mkdir 'dir with 字 chinese 鳥鸟 characters'
touch 

在测试目录 test dir 中执行 escpaths 给出转义输出:


仅获取转义基名的解决方案

这个(也是可移植的)函数将为您提供当前目录中所有项目的转义基名(这次不包括当前目录)。

escbasenames() {
    find . -mindepth 1 -maxdepth 1 -exec printf '%s\0' "$(basename {})" \; | xargs -0 -I {} bash -c 'printf "%q\n" "${0#./}"' {} | sort
}

在同一测试目录 test dir 中运行 escbasenames 会给出转义的基名:


最后注释

请注意,如果路径/文件名包含换行符或制表符,它将被编码为 ANSI- C 字符串。终端中的自动补全功能也可以使用 ANSI-C 字符串补全。示例 ANSI-C 字符串自动完成输出类似于 my$'\n'newline$'\n'dir/my$'\t'tabbed$'\t'file.txt

file\twith\tmany\ttabs.txt' touch 'file @*&$()!#[]:.txt' touch 'file with newlines.txt'

在测试目录 test dir 中执行 escpaths 给出转义输出:


仅获取转义基名的解决方案

这个(也是可移植的)函数将为您提供当前目录中所有项目的转义基名(这次不包括当前目录)。


在同一测试目录 test dir 中运行 escbasenames 会给出转义的基名:


最后注释

请注意,如果路径/文件名包含换行符或制表符,它将被编码为 ANSI- C 字符串。终端中的自动补全功能也可以使用 ANSI-C 字符串补全。示例 ANSI-C 字符串自动完成输出类似于 my$'\n'newline$'\n'dir/my$'\t'tabbed$'\t'file.txt

/.../test dir/file\nwith\nnewlines.txt'

仅获取转义基名的解决方案

这个(也是可移植的)函数将为您提供当前目录中所有项目的转义基名(这次不包括当前目录)。


在同一测试目录 test dir 中运行 escbasenames 会给出转义的基名:


最后注释

请注意,如果路径/文件名包含换行符或制表符,它将被编码为 ANSI- C 字符串。终端中的自动补全功能也可以使用 ANSI-C 字符串补全。示例 ANSI-C 字符串自动完成输出类似于 my$'\n'newline$'\n'dir/my$'\t'tabbed$'\t'file.txt

file\twith\tmany\ttabs.txt' touch 'file @*&$()!#[]:.txt' touch 'file with newlines.txt'

在测试目录 test dir 中执行 escpaths 给出转义输出:


仅获取转义基名的解决方案

这个(也是可移植的)函数将为您提供当前目录中所有项目的转义基名(这次不包括当前目录)。


在同一测试目录 test dir 中运行 escbasenames 会给出转义的基名:


最后注释

请注意,如果路径/文件名包含换行符或制表符,它将被编码为 ANSI- C 字符串。终端中的自动补全功能也可以使用 ANSI-C 字符串补全。示例 ANSI-C 字符串自动完成输出类似于 my$'\n'newline$'\n'dir/my$'\t'tabbed$'\t'file.txt

/.../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\ @\*\&\$\(\)\!#\[\]:.txt

仅获取转义基名的解决方案

这个(也是可移植的)函数将为您提供当前目录中所有项目的转义基名(这次不包括当前目录)。

在同一测试目录 test dir 中运行 escbasenames 会给出转义的基名:

最后注释

请注意,如果路径/文件名包含换行符或制表符,它将被编码为 ANSI- C 字符串。终端中的自动补全功能也可以使用 ANSI-C 字符串补全。示例 ANSI-C 字符串自动完成输出类似于 my$'\n'newline$'\n'dir/my$'\t'tabbed$'\t'file.txt

file\twith\tmany\ttabs.txt' touch 'file @*&$()!#[]:.txt' touch 'file with newlines.txt'

在测试目录 test dir 中执行 escpaths 给出转义输出:

仅获取转义基名的解决方案

这个(也是可移植的)函数将为您提供当前目录中所有项目的转义基名(这次不包括当前目录)。

在同一测试目录 test dir 中运行 escbasenames 会给出转义的基名:

最后注释

请注意,如果路径/文件名包含换行符或制表符,它将被编码为 ANSI- C 字符串。终端中的自动补全功能也可以使用 ANSI-C 字符串补全。示例 ANSI-C 字符串自动完成输出类似于 my$'\n'newline$'\n'dir/my$'\t'tabbed$'\t'file.txt

file\nwith\nnewlines.txt'

最后注释

请注意,如果路径/文件名包含换行符或制表符,它将被编码为 ANSI- C 字符串。终端中的自动补全功能也可以使用 ANSI-C 字符串补全。示例 ANSI-C 字符串自动完成输出类似于 my$'\n'newline$'\n'dir/my$'\t'tabbed$'\t'file.txt

file\twith\tmany\ttabs.txt' touch 'file @*&$()!#[]:.txt' touch 'file with newlines.txt'

在测试目录 test dir 中执行 escpaths 给出转义输出:

仅获取转义基名的解决方案

这个(也是可移植的)函数将为您提供当前目录中所有项目的转义基名(这次不包括当前目录)。

在同一测试目录 test dir 中运行 escbasenames 会给出转义的基名:

最后注释

请注意,如果路径/文件名包含换行符或制表符,它将被编码为 ANSI- C 字符串。终端中的自动补全功能也可以使用 ANSI-C 字符串补全。示例 ANSI-C 字符串自动完成输出类似于 my$'\n'newline$'\n'dir/my$'\t'tabbed$'\t'file.txt

/.../test dir/file\nwith\nnewlines.txt'

仅获取转义基名的解决方案

这个(也是可移植的)函数将为您提供当前目录中所有项目的转义基名(这次不包括当前目录)。

在同一测试目录 test dir 中运行 escbasenames 会给出转义的基名:

最后注释

请注意,如果路径/文件名包含换行符或制表符,它将被编码为 ANSI- C 字符串。终端中的自动补全功能也可以使用 ANSI-C 字符串补全。示例 ANSI-C 字符串自动完成输出类似于 my$'\n'newline$'\n'dir/my$'\t'tabbed$'\t'file.txt

file\twith\tmany\ttabs.txt' touch 'file @*&$()!#[]:.txt' touch 'file with newlines.txt'

在测试目录 test dir 中执行 escpaths 给出转义输出:

仅获取转义基名的解决方案

这个(也是可移植的)函数将为您提供当前目录中所有项目的转义基名(这次不包括当前目录)。

在同一测试目录 test dir 中运行 escbasenames 会给出转义的基名:

最后注释

请注意,如果路径/文件名包含换行符或制表符,它将被编码为 ANSI- C 字符串。终端中的自动补全功能也可以使用 ANSI-C 字符串补全。示例 ANSI-C 字符串自动完成输出类似于 my$'\n'newline$'\n'dir/my$'\t'tabbed$'\t'file.txt

/.../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\ @\*\&\$\(\)\!#\[\]:.txt

仅获取转义基名的解决方案

这个(也是可移植的)函数将为您提供当前目录中所有项目的转义基名(这次不包括当前目录)。

在同一测试目录 test dir 中运行 escbasenames 会给出转义的基名:

最后注释

请注意,如果路径/文件名包含换行符或制表符,它将被编码为 ANSI- C 字符串。终端中的自动补全功能也可以使用 ANSI-C 字符串补全。示例 ANSI-C 字符串自动完成输出类似于 my$'\n'newline$'\n'dir/my$'\t'tabbed$'\t'file.txt

file\twith\tmany\ttabs.txt' touch 'file @*&$()!#[]:.txt' touch 'file with newlines.txt'

在测试目录 test dir 中执行 escpaths 给出转义输出:

仅获取转义基名的解决方案

这个(也是可移植的)函数将为您提供当前目录中所有项目的转义基名(这次不包括当前目录)。

在同一测试目录 test dir 中运行 escbasenames 会给出转义的基名:

最后注释

请注意,如果路径/文件名包含换行符或制表符,它将被编码为 ANSI- C 字符串。终端中的自动补全功能也可以使用 ANSI-C 字符串补全。示例 ANSI-C 字符串自动完成输出类似于 my$'\n'newline$'\n'dir/my$'\t'tabbed$'\t'file.txt

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\ @\*\&\$\(\)\!#\[\]:.txt

最后注释

请注意,如果路径/文件名包含换行符或制表符,它将被编码为 ANSI- C 字符串。终端中的自动补全功能也可以使用 ANSI-C 字符串补全。示例 ANSI-C 字符串自动完成输出类似于 my$'\n'newline$'\n'dir/my$'\t'tabbed$'\t'file.txt

file\twith\tmany\ttabs.txt' touch 'file @*&$()!#[]:.txt' touch 'file with newlines.txt'

在测试目录 test dir 中执行 escpaths 给出转义输出:

仅获取转义基名的解决方案

这个(也是可移植的)函数将为您提供当前目录中所有项目的转义基名(这次不包括当前目录)。

在同一测试目录 test dir 中运行 escbasenames 会给出转义的基名:

最后注释

请注意,如果路径/文件名包含换行符或制表符,它将被编码为 ANSI- C 字符串。终端中的自动补全功能也可以使用 ANSI-C 字符串补全。示例 ANSI-C 字符串自动完成输出类似于 my$'\n'newline$'\n'dir/my$'\t'tabbed$'\t'file.txt

/.../test dir/file\nwith\nnewlines.txt'

仅获取转义基名的解决方案

这个(也是可移植的)函数将为您提供当前目录中所有项目的转义基名(这次不包括当前目录)。

在同一测试目录 test dir 中运行 escbasenames 会给出转义的基名:

最后注释

请注意,如果路径/文件名包含换行符或制表符,它将被编码为 ANSI- C 字符串。终端中的自动补全功能也可以使用 ANSI-C 字符串补全。示例 ANSI-C 字符串自动完成输出类似于 my$'\n'newline$'\n'dir/my$'\t'tabbed$'\t'file.txt

file\twith\tmany\ttabs.txt' touch 'file @*&$()!#[]:.txt' touch 'file with newlines.txt'

在测试目录 test dir 中执行 escpaths 给出转义输出:

仅获取转义基名的解决方案

这个(也是可移植的)函数将为您提供当前目录中所有项目的转义基名(这次不包括当前目录)。

在同一测试目录 test dir 中运行 escbasenames 会给出转义的基名:

最后注释

请注意,如果路径/文件名包含换行符或制表符,它将被编码为 ANSI- C 字符串。终端中的自动补全功能也可以使用 ANSI-C 字符串补全。示例 ANSI-C 字符串自动完成输出类似于 my$'\n'newline$'\n'dir/my$'\t'tabbed$'\t'file.txt

/.../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\ @\*\&\$\(\)\!#\[\]:.txt

仅获取转义基名的解决方案

这个(也是可移植的)函数将为您提供当前目录中所有项目的转义基名(这次不包括当前目录)。

在同一测试目录 test dir 中运行 escbasenames 会给出转义的基名:

最后注释

请注意,如果路径/文件名包含换行符或制表符,它将被编码为 ANSI- C 字符串。终端中的自动补全功能也可以使用 ANSI-C 字符串补全。示例 ANSI-C 字符串自动完成输出类似于 my$'\n'newline$'\n'dir/my$'\t'tabbed$'\t'file.txt

file\twith\tmany\ttabs.txt' touch 'file @*&$()!#[]:.txt' touch 'file with newlines.txt'

在测试目录 test dir 中执行 escpaths 给出转义输出:

仅获取转义基名的解决方案

这个(也是可移植的)函数将为您提供当前目录中所有项目的转义基名(这次不包括当前目录)。

在同一测试目录 test dir 中运行 escbasenames 会给出转义的基名:

最后注释

请注意,如果路径/文件名包含换行符或制表符,它将被编码为 ANSI- C 字符串。终端中的自动补全功能也可以使用 ANSI-C 字符串补全。示例 ANSI-C 字符串自动完成输出类似于 my$'\n'newline$'\n'dir/my$'\t'tabbed$'\t'file.txt

I'm search for a function for bash that will escape a filename the same way "tab" escapes filenames.

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).

escpaths() {
    find "$PWD" -maxdepth 1 -print0 | xargs -0 -I {} bash -c 'printf "%q\n" "$0"' {} | sort
}

Here's a rigorous test case scenario:

# Generate test directory, containing test directories and files.
mkdir 'test dir'; cd 'test dir'

mkdir '\dir  with\ backslashes\\\'
touch '\file with \\backslashes\'
touch 'An-Beat - Mentally Insine (Original Mix).mp3'
mkdir 'crazy(*@$):{}[]dir:'
mkdir 'dir with 字 chinese 鳥鸟 characters'
touch 

Executing escpaths in the test directory test 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).

escbasenames() {
    find . -mindepth 1 -maxdepth 1 -exec printf '%s\0' "$(basename {})" \; | xargs -0 -I {} bash -c 'printf "%q\n" "${0#./}"' {} | sort
}

Running escbasenames in the same test directory test 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/ or my$'\t'tabbed$'\t'file.txt.

file\twith\tmany\ttabs.txt' touch 'file @*&$()!#[]:.txt' touch 'file with newlines.txt'

Executing escpaths in the test directory test 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 directory test 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/ or my$'\t'tabbed$'\t'file.txt.

/.../test dir/file\nwith\nnewlines.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 directory test 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/ or my$'\t'tabbed$'\t'file.txt.

file\twith\tmany\ttabs.txt' touch 'file @*&$()!#[]:.txt' touch 'file with newlines.txt'

Executing escpaths in the test directory test 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 directory test 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/ or my$'\t'tabbed$'\t'file.txt.

/.../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\ @\*\&\$\(\)\!#\[\]:.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 directory test 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/ or my$'\t'tabbed$'\t'file.txt.

file\twith\tmany\ttabs.txt' touch 'file @*&$()!#[]:.txt' touch 'file with newlines.txt'

Executing escpaths in the test directory test 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 directory test 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/ or my$'\t'tabbed$'\t'file.txt.

file\nwith\nnewlines.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 my$'\n'newline$'\n'dir/ or my$'\t'tabbed$'\t'file.txt.

file\twith\tmany\ttabs.txt' touch 'file @*&$()!#[]:.txt' touch 'file with newlines.txt'

Executing escpaths in the test directory test 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 directory test 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/ or my$'\t'tabbed$'\t'file.txt.

/.../test dir/file\nwith\nnewlines.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 directory test 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/ or my$'\t'tabbed$'\t'file.txt.

file\twith\tmany\ttabs.txt' touch 'file @*&$()!#[]:.txt' touch 'file with newlines.txt'

Executing escpaths in the test directory test 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 directory test 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/ or my$'\t'tabbed$'\t'file.txt.

/.../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\ @\*\&\$\(\)\!#\[\]:.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 directory test 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/ or my$'\t'tabbed$'\t'file.txt.

file\twith\tmany\ttabs.txt' touch 'file @*&$()!#[]:.txt' touch 'file with newlines.txt'

Executing escpaths in the test directory test 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 directory test 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/ or my$'\t'tabbed$'\t'file.txt.

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\ @\*\&\$\(\)\!#\[\]:.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 my$'\n'newline$'\n'dir/ or my$'\t'tabbed$'\t'file.txt.

file\twith\tmany\ttabs.txt' touch 'file @*&$()!#[]:.txt' touch 'file with newlines.txt'

Executing escpaths in the test directory test 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 directory test 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/ or my$'\t'tabbed$'\t'file.txt.

/.../test dir/file\nwith\nnewlines.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 directory test 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/ or my$'\t'tabbed$'\t'file.txt.

file\twith\tmany\ttabs.txt' touch 'file @*&$()!#[]:.txt' touch 'file with newlines.txt'

Executing escpaths in the test directory test 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 directory test 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/ or my$'\t'tabbed$'\t'file.txt.

/.../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\ @\*\&\$\(\)\!#\[\]:.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 directory test 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/ or my$'\t'tabbed$'\t'file.txt.

file\twith\tmany\ttabs.txt' touch 'file @*&$()!#[]:.txt' touch 'file with newlines.txt'

Executing escpaths in the test directory test 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 directory test 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/ or my$'\t'tabbed$'\t'file.txt.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文