简体   繁体   中英

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.

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

How do I do this in the shell script?

Use double quotes:

less "file:name.txt"

Bash recognizes the value within the double quotes as a full string.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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