How to Handle Passing Filenames with Spaces in Bash
Linux has a default shell Bash (aka Bourne again shell) to execute commands for the system. Most programmers prefer bash over cmd because of the flexibility and powerful command line interpreter that bash provides. However, most users will still have issues while trying to handle passing filenames with spaces in bash. This is because spaces are not considered the same in bash as they are in file names.
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:
cat file name.txt
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
- Hold Ctrl + Alt keys and Press T to open Terminal.
- Now change directory to where the file is located.
(you can also drag and drop the file in the terminal after the command ‘cat‘, which will automatically put quotations on the file path/directory)cd Desktop
Changing directory Note: Desktop can be changed to the location you are trying to access.
- Type the following command to read a text file with spaces in the name:
cat 'file name.txt'
or
cat "file name.txt"
Using quotations to avoid spaces in name error - Single and double quotations will result in the same. In some cases, you need to try both and see which one works.
Method 2: Using Backlash Escape Character
- Hold Ctrl + Alt keys and Press T to open Terminal.
- Change the directory to where the file is located by using the following command.
cd Desktop
Note: Put your location name in place of Desktop.
- Now type the command and use escape character anywhere there is space in the name:
cat file\ name.txt
Using escape character to avoid spaces error
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.