010-160 practice question 14 of 15

Which of the following commands output the content of the file Texts 2.txt? (Choose two.)

Full set: 81 questions
Answer options
A: cat 'Texts 2.txt’ Correct answer
B: cat -- Texts 2.txt
C: cat |Texts 2.txt|
D: cat 'Texts\ 2.txt’
E: cat Texts\ 2.txt Correct answer
Correct Answer:
A. cat 'Texts 2.txt’; E. cat Texts\ 2.txt
Explanation

The shell uses spaces to separate arguments on the command line. To pass a filename containing a space, such as "Texts 2.txt", as a single argument to the cat command, the space must be handled specially. This can be done in two primary ways: quoting or escaping. Enclosing the entire filename in single quotes ('Texts 2.txt') causes the shell to treat the enclosed string as a single literal argument. Alternatively, escaping the space with a backslash (Texts\ 2.txt) tells the shell to treat the space as a literal character rather than an argument delimiter. Both methods correctly pass the full filename to cat.

Why Incorrect

B. cat -- Texts 2.txt passes "Texts" and "2.txt" as two separate arguments to cat, as the space is not escaped or quoted.
C. cat |Texts 2.txt| is invalid syntax. The pipe character (|) is used to redirect the output of one command to the input of another.
D. cat 'Texts\ 2.txt’ fails because a backslash within single quotes is treated as a literal character, not an escape character.

References

1. GNU Bash Reference Manual (Version 5.2), Section 3.1.2.2 Single Quotes: "Enclosing characters in single quotes (') preserves the literal value of each character within the quotes. A single quote may not occur between single quotes, even when preceded by a backslash."

2. GNU Bash Reference Manual (Version 5.2), Section 3.1.2.1 Escape Character: "A non-quoted backslash (\) is the Bash escape character. It preserves the literal value of the next character that follows, with the exception of ."

3. MIT OpenCourseWare, 6.S094: The Missing Semester of Your CS Education (Fall 2020), Lecture 1: The Shell: In the "Shell Scripting" section, the lecture explains: "To deal with arguments that contain spaces, you should quote them... Another way of dealing with spaces in arguments is to escape them with a \."

4. GNU Coreutils Manual (Version 9.4), Section 2.1 Common options: "The -- option signifies the end of options. Any arguments following it are treated as file names, even if they begin with a ‘-’." (This confirms -- does not affect space interpretation by the shell itself).

Scroll to Top