Why Filename with Spaces is not Recognized by Bash?
In Bash, if you type multiple words without escape character () or quotations, it will consider all the words as arguments. That applies for all the operations, whether you are changing directory with ‘cd‘ or trying to access files with ‘cat‘ commands. Anything you write after using these will be considered as an argument. For example: Note: Your filename can be anything but for this article, we will be using “file name.txt” as an example. Here ‘cat‘ command will consider file and name as two arguments rather than a single argument. However, if you use escape character or quotations then bash shell will consider that as a single argument, which will be the “file name.txt“.
Filename with Spaces in Bash
There are few methods which can be used for the spaces in the name. The best practice is avoiding spaces for file names in the future. A simple method will be to rename the file that you are trying to access and remove spaces. Some other methods are using single or double quotations on the file name with spaces or using escape () symbol right before the space. We will be providing methods with applied examples to show you how it works.
Method 1: Using Single and Double Quotations
Method 2: Using Backlash Escape Character
Bonus: Usage of Quotations and Escape
Sometimes when you are using the directory in the command, there can be consequences for using quotations on the overall path. This is because some commands like ‘mv‘ or ‘cp‘ will consider the path as file source if the quotation is used on whole. You need to provide quotations for both source and destination individually so that commands like ‘cp‘ can work properly. You can also check the example below which will show you that using escape character for the path is much more complex and users can make mistake with it.
Jeff Bezos Intends to Donate Majority of His Wealth to Charity Before PassingFix: Facebook is currently unable to handle this request.Fix: Invalid File Handle on Windows 10Fix: The Handle is Invalid Error Message When Logging Into Windows 10