bash 如何在 Linux 中创建带有特殊字符的文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49988312/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 16:54:04  来源:igfitidea点击:

How do I create files with special characters in Linux?

linuxbashshellspecial-charactersfilenames

提问by Cameron Shirley

I am using the touchcommand to try and create a file with the name "\?$*'KwaMe'*$?\"(quotation marks included as part of the file name). However when I type touch "\?$*'KwaMe'*$?\"in the Terminal, it doesn't give me the result I am expecting. How can I create this file?

我正在使用该touch命令尝试创建一个具有该名称的文件"\?$*'KwaMe'*$?\"(引号包含在文件名中)。但是,当我touch "\?$*'KwaMe'*$?\"在终端中输入时,它并没有给我预期的结果。我怎样才能创建这个文件?

回答by Ronan Boiteau

You need to escape special characterswith the backslash symbol (\).

您需要使用反斜杠符号 ( )转义特殊字符\



This command will create a file named "\?$*'KwaMe'*$?\":

此命令将创建一个名为的文件"\?$*'KwaMe'*$?\"

touch \"\\?$\*\'KwaMe\'\*$\?\\"


Explanation

解释

  1. Double your \, like this: \\, so that your shell does not interpret the backslashes from your filename as escape characters.
  2. Escape "and ', like this: \", \', so that your shell interprets the double quotes as part of the filename.
  3. Escape $, like this: \$, otherwise your shell will think you're using a variable.
  4. Escape ?and *, like this: \?, \*, to prevent filename expansion.
  1. 将您的\, 像这样: \\, 这样您的外壳就不会将文件名中的反斜杠解释为转义字符。
  2. 转义"',像这样:\", \',这样你的 shell 将双引号解释为文件名的一部分。
  3. Escape$,像这样: \$,否则你的shell 会认为你在使用一个变量
  4. 转义?*,像这样:\?, \*,以防止文件名扩展