← Back to Bash Course | Chapter 3: Control Flow | Lesson 13 of 14

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

एक here document किसी को एक पूरा paragraph तब तक ज़ोर से पढ़ने जैसा है जब तक आप एक secret stop word न कहें। Start और stop word के बीच की हर चीज़ command को एक message के रूप में सौंपी जाती है।

In this page:

  1. Here Documents (<<EOF)
Syntax
bash
command <<DELIMITER
text line 1
text line $variable
DELIMITER

Here Documents (<<EOF)

एक here document आपको literal text की कई lines को एक command के standard input में feed करने देता है, <<EOF से शुरू होते हुए और एक ऐसी line पर खत्म होते हुए जिसमें सिर्फ matching delimiter (convention से EOF, लेकिन कोई भी word काम करता है) हो।

अंदर के variables expand होते हैं जब तक delimiter quote न हो, जैसे <<EOF। Here documents आमतौर पर कई separate echo calls के बिना multi-line text को एक file में लिखने के लिए उपयोग किए जाते हैं।

Note: Delimiter को quote करें, <<EOF, here document के अंदर variable expansion रोकने के लिए।

उदाहरण: Here Documents (<<EOF)

bash
#!/bin/bash
name="World"

cat <<EOF
Hello, $name!
This is a multi-line
here document.
EOF
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 को spaces से indent करना, इसलिए EOF recognise नहीं होता और here document कभी खत्म नहीं होता।
  2. <<EOF उपयोग करना और variables के expand होने से surprised होना; इसे रोकने के लिए delimiter को quote करें, <<EOF।
  3. Closing delimiter की line पर extra text लिखना।

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.