October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk2 min

How to Escape Spaces in Linux Filenames: Backslashes vs. Quotes

In Bash, escape a filename space with a backslash or quote the filename. For scripts, quote the variable expansion itself: "$file".

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

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

In Bash, either escape the space with a backslash or put the filename in quotes. For a file named My File.txt, both commands pass one filename argument:

cat My File.txt
cat 'My File.txt'

The backslash and quote marks are shell syntax; they do not become part of the filename. These examples describe Bash behavior, not a change to how Linux stores the name.

As an Amazon Associate I earn from qualifying purchases.

Why an unquoted space causes trouble

Bash treats spaces and tabs as word separators. Without quoting or escaping, it splits My File.txt into two words—My and File.txt—and passes them as separate arguments to the command. Bash processes quoting before word splitting and filename expansion, which is why protecting the space keeps the filename together. See the Bash manual’s shell syntax definitions and its description of shell expansions.

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

Choose a backslash or quotes

Form Example Use it when Watch for
Backslash escape cat My File.txt You are typing a short, literal command. Escape each character that needs protection; long names are easy to mistype.
Single quotes cat 'My File.txt' You want the enclosed text treated literally. A single quote cannot appear directly inside a single-quoted string.
Double quotes cat "My File.txt" You need spaces grouped while retaining selected shell expansions. $ and backticks still have special meaning, and backslash remains significant in specific cases.

For a simple filename, the backslash form is compact; single quotes are often clearer for literal text. Bash’s manual explains that an unquoted backslash preserves the next character’s literal value, except newline, and that single quotes preserve the literal value of enclosed characters. Double quotes group words but are not fully literal. Read the manual’s sections on quoting and single quotes.

Quote variable expansions in scripts

If a variable contains a filename with spaces, put the quotes around the expansion where you use it:

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

The quotes around "$file" keep its value together as one argument and prevent wildcard characters in the value from triggering filename expansion. Putting quote characters inside the variable’s value does not make them active shell syntax when the variable expands. Bash describes these expansion rules in its sections on double quotes and shell expansions.

When a filename contains a single quote

A single quote cannot be written directly inside a single-quoted segment. Bash lets you join quoted and escaped segments to represent it. For example, this prints the text a'b:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
printf '%sn' 'a'''b'

The command consists of a single-quoted a, an escaped single quote, and a single-quoted b. For complicated names, shell completion can reduce manual typing. If copying names from command output, do not assume every tool prints shell-ready text. GNU Coreutils documents shell-oriented quoting modes and notes that its output is suitable for POSIX-compatible shells such as Bash but may not work in incompatible shells such as csh; see the Coreutils manual on quoting.

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

What the escape characters do—and do not do

The filename itself contains an ordinary space. Bash removes the quoting syntax during command processing, so the command receives the original filename characters as one argument. These instructions are for Bash and compatible command lines; other shells can have different quoting rules. The GNU Bash Reference Manual edition 5.3 was last updated 18 May 2025.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

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

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.