Interview › Scripting (Bash, Groovy)
What does read -r do, and why is the -r flag important? [Basic]
Answer
read -r tells read not to treat backslashes as escape characters. It is important because paths, regexes, Windows-style strings, and log lines can contain backslashes that must be preserved exactly.
Technical explanation
Without -r, read may remove or alter backslashes, corrupting input data.
Use IFS= read -r line for reliable line-oriented file processing.
Use read -r -a array when intentionally splitting a line into a Bash array.
Hands-on example
printf '%s\n' 'C:\temp\app.log' > paths.txt
while IFS= read -r path; do
printf 'path=%s\n' "$path"
done < paths.txtPreparing for an interview?
Check how well your resume matches the role with our free resume checker— match score, ATS check, and the skills you're missing.
More Scripting (Bash, Groovy) interview questions
- What does IFS do, and why set it when reading lines? [Basic]
- How do you write and call a function in Bash? [Basic]
- How do you pass arguments to a function and return a value or status? [Basic]
- How do you write a for loop over a list and a C-style for loop? [Basic]
- What is local scope in a Bash function? [Basic]
- What is OPTARG, and how does getopts handle options with values? [Intermediate]
- What are the common file test operators (-f, -d, -e, -r, -w, -x)? [Basic]
- What is a here-string (<<<), and how does it differ from a here-doc? [Intermediate]