简体   繁体   English

如何在Shell脚本中处理文件名中的冒号“:”

[英]How to deal with colons “:” in filename in shell script

I have a large group of files with ":" in their file names. 我有一大堆文件名中带有“:”的文件。 When I'm in the interactive shell, typing "\\" and then hitting tab does the trick to get them recognized as valid inputs to commands, but not so in the shell script. 当我在交互式外壳程序中时,键入“ \\”,然后单击tab可以使它们被识别为命令的有效输入,但是在外壳程序脚本中却并非如此。

I've tried 我试过了

less file:name.txt
less file\:name.txt
less 'file\:name.txt
less 'file:name.txt'

and it's not recognized as a valid file. 并且无法将其识别为有效文件。

However on the interactive command line I type less, followed by first file, then I type \\, and then hit the TAB key, everything then works... 但是,在交互式命令行上,我键入较少,然后键入第一个文件,然后键入\\,然后按TAB键,然后一切正常。

How do I do this in the shell script? 如何在Shell脚本中执行此操作?

Use double quotes: 使用双引号:

less "file:name.txt"

Bash recognizes the value within the double quotes as a full string. Bash将双引号中的值识别为完整字符串。

As seen in Using quotes to include spaces and characters in filenames : 使用引号在文件名中包含空格和字符所示

If you want to work with files with spaces or special characters in the filename, you may have to use quotes. 如果要使用文件名中带有空格或特殊字符的文件,则可能必须使用引号。

This is working even when you don't put anything. 即使您什么也没放,这仍然有效。

you can try: less file and it recognizes it for you... 您可以尝试:减少文件并为您识别...

But you can use less "file:name.txt" as string in your bash. 但是您可以在bash中使用less "file:name.txt"作为字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM