The same rule as discussed above follows for any number of hyphens in the name of the file and their occurrence. If you have lots of files in a folder the name of which contains a dash, and you want to delete all of them at once, do as: $ rm. To delete a file with a dashed filename, you can use the rm command as shown. To rename a file with a dashed filename, you can use the mv command as shown.įor example, to rename a file named “ -abc.txt” to “ -a.txt“, you would use: $ mv -abc.txt -a.txt You may replace nano with any other editor of your choice, for example, use vim editor as shown: $ vim -abc.txt Here’s an example using the nano text editor: $ nano -abc.txt To edit a file with a dashed filename, you can use various text editors available. rw-rw-r- tecmint tecmint 0 B Tue Jun 20 10:32:43 2023 -abc.txt Create Dashed File in Linux Edit Dashed File in Linux You may verify the file thus created by both the above ways by running commands ls or ls -l for long listing. There are two ways to resolve this error as: $ touch -abc.txt To resolve such an error, we have to tell the bash shell not to interpret anything after the special character (here dash), as an option. The reason for the above error is that the shell interprets anything after a dash (-), as an option, and obviously, there is no such option, hence the error. Sample Output: touch: invalid option - 'b' To work with dashed filenames in Linux, first, create a file that starts with a dash (-), say -abx.txt using the touch command. These filenames can sometimes cause issues when working with them because the leading dash can be misinterpreted as an option or flagged by command-line utilities. In Linux, filenames that begin with a dash ("-") are often called “ dashed filenames” or “ hyphenated filenames“. Also speaking technically every thing be it a folder, driver, or anything else is treated as a file in Linux. I agree with you, that such file names are not common still your shell should not break/give up when you have to deal with any such file names. One of the most obvious questions here is – who on earth create/deal with files/folder name having a Hash (#), a semi-colon ( ), a dash (-) or any other special character. Special Characters in Filenames in LinuxĮxamples of the most common file names are: abc.txtĮxamples of numeric file names are: 121.txtĮxamples of Alpha-Numeric file names are: eg84235.txtĮxamples of file names that have special character and is not very common: #232.txt.Therefore, if we write a lot of scripts, shellcheck should be in our toolbox because it enforces us to use best practices, eventually making us better at writing shell scripts. Sometimes, shellcheck will detect very subtle errors that we might not even notice. Since we’ve fixed the error, we don’t have any warnings. Let’s fix these errors and run shellcheck again. In Line 6, we’re starting a double quote, but the tool points out that it might be the ending quote for “Hello. In this case, we left the ending quote for the greeting variable. Fix to allow more checks.Īfter running shellcheck, we can see that it prints a lot of useful information. ^- SC1073 (error): Couldn't parse this double quoted string. ^- SC1079 (info): This is actually an end quote, but due to next char it looks suspect. ^- SC1078 (warning): Did you forget to close this double quoted string? ^- SC1009 (info): The mentioned syntax error was in this simple command. Command Substitution Inside Double Quotes On the other hand, the other two use-cases will yield the output in a list context – each word in the list is a field separated by whitespace.įor instance, if we process the positional arguments with “ ”, it will yield the arguments as a list, and so on, up to #!/bin/shĭone $ sh script.sh /etc/fstab /etc/hostnameġ /etc/hostname 2.2. home/user/Documents/Reference Manual.pdf Therefore, any amount of whitespaces and other special characters (?, [, \) inside the string will be a part of the string: #!/bin/sh In our case, we’re interested in the string context – the double quotes around the variable yields a single string. Treat each whitespace-separated field as a glob that can be expanded by the shell.Split the string into fields using whitespaces as the delimiter.Take the value of the HOME variable as a whole.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |