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
- What is the purpose of the shebang line, and what does #!/bin/bash do? [Basic]
- What is the difference between sh and bash? [Basic]
- How do you make a script executable and run it? [Basic]
- What is the difference between running a script with ./script.sh, bash script.sh, and source script.sh? [Basic]
- What does sourcing a script do differently from executing it? [Basic]
- How do you declare a variable in Bash, and why are spaces around = not allowed? [Basic]
- What is the difference between $var and ${var}? [Basic]
- What is the difference between single quotes and double quotes in Bash? [Basic]