Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

In Bash, use a backslash or quotes to keep a filename with spaces together as one argument. For example, both cat My File.txt and cat 'My File.txt' pass the filename My File.txt to cat. The backslash and quote marks are shell syntax; they do not become part of the filename.

Use a backslash or single quotes for a literal filename

Bash treats unquoted spaces and tabs as word separators. Without protection, cat My File.txt is split into multiple arguments rather than passed as one filename.

  • Backslash: cat My File.txt escapes the space. In Bash, an unquoted backslash preserves the literal value of the next character, except a newline, which it can join to the preceding line.
  • Single quotes: cat 'My File.txt' preserve the characters inside literally, including the space. A literal single quote cannot appear directly inside a single-quoted string, even if you put a backslash before it.

Both forms result in the same filename argument. The GNU Bash Reference Manual, edition 5.3, last updated 18 May 2025, describes these quoting rules in its Quoting documentation.

Choose the form that fits the command

Form Example Best for Caution
Backslash escape cat My File.txt A short, literal command-line filename. Escape each character that needs protection; long names are easy to mistype.
Single quotes cat 'My File.txt' Literal text, especially when the name contains several shell-special characters. A single quote cannot be included directly inside the quoted string.
Double quotes cat "My File.txt" Grouping a filename that may also contain intended variable or command substitutions. Dollar signs and backticks retain special meaning; double quotes do not make all enclosed characters literal.

For a plain filename, single quotes are often the clearest way to show that the enclosed text should be taken literally. Use double quotes when you want selected expansions to happen while spaces remain grouped.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Quote variable expansions in scripts

When a filename is stored in a variable, put the quotes around the expansion wherever you use it:

file='My File.txt'
cat "$file"

The quotes keep the expanded value together as one argument and prevent wildcard characters in that value from being treated as filename patterns. Putting quote marks inside the variable’s value does not make Bash interpret them as syntax after expansion; write "$file" in the command.

Bash performs word splitting and filename expansion on unquoted expansion results before removing the original quote characters. Quoting therefore protects a name not only from being split at spaces but also from having unquoted *, ?, or [ treated as pattern syntax. See the GNU Bash Reference Manual’s Shell Expansions documentation.

Handle names with single quotes or other awkward characters

To include a literal single quote in shell text, close the quoted portion, escape the quote, and start a new quoted portion. For example, this prints a'b followed by a newline:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

printf '%sn' 'a'''b'

For complicated names, shell completion can reduce typing errors. If a program is printing filenames for you to copy into a command, do not assume its output is shell-ready. GNU Coreutils documents shell-oriented quoting modes in its Quoting guidance; it notes that its shell quoting suits POSIX-compatible shells such as Bash but may not work in incompatible shells such as csh.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Remember: escaping changes the command line, not the filename

Linux filenames do not need to contain backslashes before spaces. The shell interprets the backslash or quotes while parsing your command, then passes the ordinary filename characters—including spaces—as an argument to the program. The examples here describe Bash behavior; other shells can have different quoting rules or edge cases.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.