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 لا يحذف ملفًا واحدًا اسمه my notes.txt — بل يحاول حذف ملفين، my وnotes.txt، ولا وجود لأي منهما.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
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 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.
grep "$100" file يطلب من الـshell متغيّرًا اسمه 1 يتبعه 00، فلا تجد شيئًا، فتسلّم grep نمطًا فارغًا. أما grep '$100' فيمرر المحارف الأربعة كما هي. والأمر نفسه ينطبق على *: فبلا اقتباس توسّعها الـshell على أسماء ملفاتك قبل أن يراها الأمر.Where this bites hardest
$file يحوي my notes.txt، فإن rm $file ينقسم إلى وسيطين وrm "$file" لا ينقسم. وفي نص برمجي يدور على أسماء ملفات فهذا ليس تفضيلًا أسلوبيًا — بل هو الفرق بين العمل الصحيح وحذف أشياء لم تسمّها. اقتبس كل متغيّر قد يحمل مسارًا.| Character | Means to the shell | To use it literally |
|---|---|---|
| space | Separator between arguments | Quote the whole thing |
| $ | Start of a variable | Single quotes, or \$ |
| * ? [ | Wildcards, expanded against filenames | Quote it |
| " | Start or end of a quoted string | \" inside double quotes |
| & ; | | Command separators and pipes | Quote 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.
my-notes.txt أو my_notes.txt. فالاقتباس يعمل، لكن كل أمر يمسّ ذلك الملف يحتاجه، وزوج علامتين منسي في نص برمجي عيبٌ لا يظهر إلا مع الملف الوحيد الذي في اسمه مسافة.Try it live / جرّب بنفسك
Check yourself / اختبر نفسك
1.
What is the difference between "$name" and '$name'?
"$name" و'$name'؟Single quotation marks turn off every kind of expansion. Double ones still let the shell substitute variables, which is sometimes exactly what you want and sometimes the bug.
2.
Why must $file be quoted in rm "$file"?
$file في rm "$file"؟The split happens after the substitution, so an unquoted variable holding my notes.txt becomes two arguments. In a loop, that is how a script deletes the wrong thing.
my notes.txt يصير وسيطين. وفي حلقة، هكذا يحذف نص برمجي الشيء الخطأ.
3.
Which quotation marks should a grep pattern like ^body$ use?
^body$؟The $ anchors the end of the line for grep, but means "a variable" to the shell. Single quotation marks are what stop the shell interfering.
$ يثبّت نهاية السطر عند grep، لكنه يعني "متغيّر" عند الـshell. وعلامتا الاقتباس المفردتان هما ما يمنع الـshell من التدخل.Score / النتيجة: 0 / 3
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 لماذا تتجنب المسافات في الأسماء أصلًا