Interview Scripting (Bash, Groovy)

What is a here-string (<<<), and how does it differ from a here-doc? [Intermediate]

Answer

A here-string, written command <<< word, passes a single string to a command's stdin. A here-doc is better for multi-line blocks; a here-string is convenient for one variable or one short value.

Technical explanation

Here-strings append a newline to the supplied string.

They are Bash/Ksh/Zsh features, not strictly POSIX sh.

Use process substitution or printf pipelines when you need more control over streaming or portability.

Hands-on example

data="api:8080:active"

IFS=: read -r name port status <<< "$data"

echo "name=$name port=$port status=$status"

# Multi-line input is clearer as a here-doc:

cat <<'EOF'

line one

line two

EOF

Preparing 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

← All Scripting (Bash, Groovy) questions