For AI agents: A markdown version of this page is available at https://docs.datadoghq.com/security/code_security/static_analysis/static_analysis_rules/bash-code-quality/read-interprets-backslashes.md. A documentation index is available at /llms.txt.

Use read -r so backslashes in input are not treated as escapes

This product is not supported for your selected Datadog site. ().

Metadata

ID: bash-code-quality/read-interprets-backslashes

Language: Bash

Severity: Warning

Category: Code Style

Description

The read builtin treats backslashes as line-continuation and escape markers unless you pass -r (raw). Input lines that contain backslashes can be changed or split in surprising ways. Use read -r (or a combined short-option token that includes r, such as -re) for normal line input, especially when reading paths or data you do not control.

Non-Compliant Code Examples

#!/bin/bash
read line
read -p "Name? " name
read -a items
while read chunk; do
  printf '%s\n' "$chunk"
done < in.txt
read -- -r

Compliant Code Examples

#!/bin/bash
read -r line
read -re x
read -r -a arr
read -a arr -r
while IFS= read -r -d '' rec; do
  printf '%s\n' "$rec"
done < data.txt
read -r -- possibly_dashed_name
read
https://static.datadoghq.com/static/images/logos/github_avatar.svg https://static.datadoghq.com/static/images/logos/vscode_avatar.svg jetbrains

Seamless integrations. Try Datadog Code Security