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.

Files with spaces in the name

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
Running the command without using escape character or quotations

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

  1. Hold Ctrl + Alt keys and Press T to open Terminal.
  2. 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.

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

  1. Hold Ctrl + Alt keys and Press T to open Terminal.
  2. 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.

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

The difference between quotations and escape character
ABOUT THE AUTHOR

Kevin Arrows


Kevin Arrows is a highly experienced and knowledgeable technology specialist with over a decade of industry experience. He holds a Microsoft Certified Technology Specialist (MCTS) certification and has a deep passion for staying up-to-date on the latest tech developments. Kevin has written extensively on a wide range of tech-related topics, showcasing his expertise and knowledge in areas such as software development, cybersecurity, and cloud computing. His contributions to the tech field have been widely recognized and respected by his peers, and he is highly regarded for his ability to explain complex technical concepts in a clear and concise manner.