Keep Filenames in One Argument
Keep Filenames in One Argument
Let's say the teacher named a report class marks.txt. That space is perfectly acceptable in a filename, but it needs care in a command. The shell does not automatically know that two unquoted words are one name. Before a utility receives the line, the shell processes the text and decides which arguments to pass.
An argument is one item supplied to a command: perhaps an option or a filename. Quotes help you keep a value together as one argument. When a variable holds a path, double-quoting its expansion is usually the straightforward choice. You get the stored value without inviting the shell to split it into words or expand wildcard characters in it.
What you’ll learn
- Explain the operation and choose it for the stated school task.
- Read the command arguments and check the result rather than relying on silence alone.
- Apply the worked task and recognize this boundary: Single quotes preserve literal text; double quotes still allow parameter expansion. A wildcard pattern can be interpreted by the shell or passed literally to a tool, depending on quoting.
One filename must remain one argument
Stored name
The variable contains one filename with a space.
class marks.txtQuoted expansion
Double quotes keep the expanded value together.
cat "$report"Utility receives
cat receives one filename rather than two separate operands.
[class marks.txt]
Read a report whose filename contains a space
Prepare the example
Use a new practice folder. We will store a filename in a variable called report, write a mark to that file, then read it back. You do not type a dollar sign when assigning the variable. You use the dollar sign later when asking for its stored value.
Bash · Linux terminal
report='class marks.txt'
printf 'Diya: 72\n' > "$report"
cat "$report"Expected result
Diya: 72In report='class marks.txt', the single quotes preserve the filename text during assignment. The expression "$report" is different: double quotes allow the variable to expand while keeping the resulting name together. The redirection therefore writes to one file with a space in its name.
The final cat receives that same single filename. If you removed the double quotes around its expansion, the shell could split the value into class and marks.txt. If you used single quotes around $report, it would pass the literal characters $report instead of the stored name. Both mistakes happen before cat can do the job you intended.
Try it, then check the worked answer
Your task: Keep the filename stored in report as one argument.
Use a new practice folder. We will store a filename in a variable called report, write a mark to that file, then read it back. You do not type a dollar sign when assigning the variable. You use the dollar sign later when asking for its stored value.
Bash · complete example for this question
report='class marks.txt'
printf 'Diya: 72\n' > "$report"
cat "$report"Expected result
Diya: 72Worked answer
cat "$report"
Choose cat "$report". You want two things at once: expand the variable and preserve its value as one argument. Double quotes do both here. After running the command, a file listing should show one report name containing a space, not two files named after its separate words.
Check your decision
Quote the expanded filename, not the variable name
Quote according to what should reach the command. Single quotes preserve literal text; double quotes still permit variable expansion while keeping the expanded value together. When a path comes from a variable, get into the habit of quoting that expansion rather than relying on every future filename to contain no spaces.
Reaching the recap and completing the quizzes are separate milestones.