← Back to Bash Course | Chapter 8: Input/Output & Redirection | Lesson 10 of 13

Here-Documents (<<EOF) का उपयोग

एक here-document आपको एक multi-line block of text सीधे एक script के अंदर embed करने और इसे एक file जैसे किसी command को feed करने देता है।
Syntax
bash
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

bash
#!/bin/bash
cat <<EOF
Line one
Line two
Line three
EOF

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

bash
#!/bin/bash
name="Ada"
cat <<EOF
Hello, $name!
Today is a scripted day.
EOF

echo "---"
cat <<'EOF'
This $name will NOT be expanded.
EOF

Generating a File with a Here-Document

एक here-document को एक file में redirect करना एक script के अंदर से configuration files या templates generate करने का एक common तरीका है।

उदाहरण: Generating a File with a Here-Document

bash
#!/bin/bash
cat > config.txt <<EOF
host=localhost
port=8080
EOF
cat config.txt
rm -f config.txt

Indented Here-Documents with <<-

<<-DELIM हर line से leading tab characters (spaces नहीं) strip करता है, closing delimiter शामिल, इसलिए आप एक function के अंदर surrounding code से match करने के लिए एक here-document को indent कर सकते हैं।

Warning: <<- सिर्फ leading tabs strip करता है, spaces नहीं, इसलिए spaces और tabs mix करना इस trick को तोड़ देगा।

उदाहरण: Indented Here-Documents with <<-

bash
#!/bin/bash
show_message() {
	cat <<-EOF
	This line was indented with a tab.
	So was this closing delimiter.
	EOF
}
show_message
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. यह भूल जाना कि closing delimiter (जैसे EOF) अपनी खुद की line पर बिना किसी leading whitespace के होना चाहिए जब तक आप <<- उपयोग न करें।
  2. यह न realize करना कि एक here-document के अंदर variables और command substitutions default रूप से expand होते हैं, जो surprising हो सकता है अगर आप literal text चाहते थे।
  3. Opening और closing delimiter names को mismatch करना, जो bash को script के बाकी हिस्से को here-document का हिस्सा मानते हुए पढ़ते रहने देता है।
चैप्टर सारांश
  • <<DELIM ... DELIM बीच में सब कुछ एक command के stdin के रूप में feed करता है।
  • Delimiter को quote करना (<<EOF) block के अंदर variable expansion disable करता है।
  • <<-EOF closing delimiter को tabs के साथ indent करने देता है, indented functions के अंदर उपयोगी।
  • Here-documents commonly config files या multi-line messages generate करने के लिए उपयोग होते हैं।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.