Lesson 15 / الدرس 15

Quoting, spaces and escaping / الاقتباس والمسافات والتهريب

The shell splits your line on spaces before anything runs. Every quoting rule in this lesson exists to control that split — and to stop a filename with a space in it becoming two arguments.

تقسّم الـshell سطرك عند المسافات قبل تنفيذ أي شيء. وكل قاعدة اقتباس في هذا الدرس وُجدت للتحكم في ذلك التقسيم — ولمنع اسم ملف فيه مسافة من أن يصير وسيطين.

Lesson 2 said the shell splits your line on spaces and treats the first word as the command. That is fine until a filename contains a space. rm my notes.txt does not remove one file called my notes.txt — it tries to remove two files, my and notes.txt, and neither exists.

rm my notes.txt        two arguments — wrong
rm "my notes.txt"       one argument — right
rm 'my notes.txt'       also one argument
rm my\ notes.txt        the backslash escapes just that space
Three ways to say the same thing. Quotation marks around the whole name are the clearest; the backslash escapes a single character and is what Tab completion produces for you automatically.

The two kinds of quotation mark differ

$ name=Ahmed
$ echo "Hello $name"      Hello Ahmed        double: the shell still substitutes
$ echo 'Hello $name'      Hello $name        single: everything is literal
This is the distinction worth learning properly. Double quotation marks still let the shell substitute variables and run commands inside them. Single quotation marks turn off everything — what you typed is exactly what the command receives. Run the demonstration for when each is right.

This explains the warning in lesson 9. grep "$100" file asks the shell for a variable called 1 followed by 00, finds nothing, and hands grep an empty pattern. grep '$100' passes the four characters through untouched. The same applies to *: unquoted, the shell expands it against your filenames before the command ever sees it.

Where this bites hardest

CharacterMeans to the shellTo use it literally
spaceSeparator between argumentsQuote the whole thing
$Start of a variableSingle quotes, or \$
* ? [Wildcards, expanded against filenamesQuote it
"Start or end of a quoted string\" inside double quotes
& ; |Command separators and pipesQuote it
\Escape the next character\\

You do not need to memorise this. You need to recognise that these characters mean something to the shell, so that when a command behaves oddly and one of them is in it, you know where to look first.

Try it live / جرّب بنفسك

Preview / المعاينة

Check yourself / اختبر نفسك

1. What is the difference between "$name" and '$name'?

2. Why must $file be quoted in rm "$file"?

3. Which quotation marks should a grep pattern like ^body$ use?

Your task / مهمتك

Create a file whose name contains a space and demonstrate, on your own machine, what goes wrong without quotes and what works with them. Then show the difference between the two kinds of quotation mark using a variable of your own. Finish with your own rule for when to use which, and a sentence on why you would avoid spaces in filenames in the first place.

أنشئ ملفًا في اسمه مسافة واعرض على جهازك ما الذي يختلّ بلا اقتباس وما الذي يعمل معه. ثم أظهر الفرق بين نوعي علامة الاقتباس بمتغيّر من عندك. واختم بقاعدتك أنت لمتى تستخدم أيهما، وجملة عن سبب تجنّبك المسافات في أسماء الملفات أصلًا.

  • The failure and the fix, both shown for real الفشل والإصلاح، معروضان فعلًا
  • Single and double quotes compared with a variable مقارنة الاقتباس المفرد والمزدوج بمتغيّر
  • Your own rule for which to use قاعدتك أنت لأيهما تستخدم
  • Why you would avoid spaces in names anyway لماذا تتجنب المسافات في الأسماء أصلًا
How do you want to submit? / كيف تريد التسليم؟