Here-Documents (<<EOF) का उपयोग
In this page:
command <<DELIMITER
text with $variable
DELIMITER
command <<'DELIMITER'
literal text
DELIMITER
A Basic Here-Document
<<DELIM एक here-document शुरू करता है; exactly DELIM वाली एक line तक सब कुछ पहले वाले command का stdin बन जाता है। यह ढेर सारी echo calls के बिना multi-line text embed करने का एक clean तरीका है।
उदाहरण: A Basic Here-Document
#!/bin/bash
cat <<EOF
Line one
Line two
Line three
EOF
Login to try C/C++/Java/PHP code in the editor
Variable Expansion Inside a Here-Document
Default रूप से, एक here-document के अंदर variables और command substitutions double quotes की तरह ही expand होते हैं। Delimiter को quote करना, जैसे <<EOF, उस expansion को disable करता है और block को literal text मानता है।
उदाहरण: Variable Expansion Inside a Here-Document
#!/bin/bash
name="Ada"
cat <<EOF
Hello, $name!
Today is a scripted day.
EOF
echo "---"
cat <<'EOF'
This $name will NOT be expanded.
EOF
Login to try C/C++/Java/PHP code in the editor
Generating a File with a Here-Document
एक here-document को एक file में redirect करना एक script के अंदर से configuration files या templates generate करने का एक common तरीका है।
उदाहरण: Generating a File with a Here-Document
#!/bin/bash
cat > config.txt <<EOF
host=localhost
port=8080
EOF
cat config.txt
rm -f config.txt
Login to try C/C++/Java/PHP code in the editor
Indented Here-Documents with <<-
<<-DELIM हर line से leading tab characters (spaces नहीं) strip करता है, closing delimiter शामिल, इसलिए आप एक function के अंदर surrounding code से match करने के लिए एक here-document को indent कर सकते हैं।
<<- सिर्फ leading tabs strip करता है, spaces नहीं, इसलिए spaces और tabs mix करना इस trick को तोड़ देगा।उदाहरण: Indented Here-Documents with <<-
#!/bin/bash
show_message() {
cat <<-EOF
This line was indented with a tab.
So was this closing delimiter.
EOF
}
show_message
Login to try C/C++/Java/PHP code in the editor
- यह भूल जाना कि closing delimiter (जैसे
EOF) अपनी खुद की line पर बिना किसी leading whitespace के होना चाहिए जब तक आप<<-उपयोग न करें। - यह न realize करना कि एक here-document के अंदर variables और command substitutions default रूप से expand होते हैं, जो surprising हो सकता है अगर आप literal text चाहते थे।
- Opening और closing delimiter names को mismatch करना, जो bash को script के बाकी हिस्से को here-document का हिस्सा मानते हुए पढ़ते रहने देता है।
<<DELIM ... DELIMबीच में सब कुछ एक command के stdin के रूप में feed करता है।- Delimiter को quote करना (
<<EOF) block के अंदर variable expansion disable करता है। <<-EOFclosing delimiter को tabs के साथ indent करने देता है, indented functions के अंदर उपयोगी।- Here-documents commonly config files या multi-line messages generate करने के लिए उपयोग होते हैं।
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first:
- Function Basics
- The read Command and Here-Strings
- Function Arguments
- stdin, stdout, stderr and File Descriptors
- Return Values
- Redirection Operators
- Local Variables
- Pipes and Chaining Commands
- Recursive Functions
- Here-Documents (<<EOF)
- Function Libraries
- The tee Command
- Discarding Output with /dev/null