← Back to Bash Course | Chapter 1: Setup & Basics | Lesson 6 of 12

Comments का उपयोग

एक comment आपकी script में humans के लिए लिखा गया एक note है जिसे Bash file run करते समय पूरी तरह ignore करता है।
Syntax
bash
# full-line comment
command  # trailing comment

Single-Line Comments

एक # character से उस line के end तक कोई भी text एक comment है और Bash द्वारा execute नहीं होता। Comments अपनी खुद की line पर आ सकते हैं या एक असली command के बाद उसी line पर trail कर सकते हैं।

उदाहरण: Single-Line Comments

bash
#!/bin/bash
# This entire line is a comment and does nothing
echo "Hello"  # this trailing part is also a comment

Quotes के अंदर # Comment नहीं है

जब एक # single या double quotes के अंदर आता है, Bash इसे एक ordinary character मानता है, comment की शुरुआत नहीं। यह confusion का एक आम source है जब एक string को legitimately एक hash symbol चाहिए, जैसे एक hex color या एक hashtag।

उदाहरण: # Inside Quotes Is Not a Comment

bash
#!/bin/bash
color="#ff00ff"
echo "The color code is $color"

Block Comment को Fake करना

Bash में कोई असली multi-line comment syntax नहीं है, लेकिन एक common workaround : no-op builtin की ओर redirected एक here-document उपयोग करता है ताकि markers के बीच की कोई भी line execute न हो।

यह एक trick है, कोई first-class language feature नहीं, इसलिए इसे कम उपयोग करें और clarity के लिए normal # lines prefer करें।

उदाहरण: Faking a Block Comment

bash
#!/bin/bash
: <<'BLOCK_COMMENT'
This entire section
is ignored by Bash
no matter how many lines it spans
BLOCK_COMMENT
echo "Code after the block comment still runs"
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. यह मानना कि Bash /* ... */ जैसे multi-line block comments support करता है; यह नहीं करता -- हर comment line को अपना खुद का # चाहिए (here-documents उपयोग करने वाले workarounds हैं, लेकिन वे असली comments नहीं हैं)।
  2. एक string के बीच में # डालना और उम्मीद करना कि वह अब भी एक comment शुरू करे; quotes के अंदर # बस एक literal character है, comment marker नहीं।
  3. यह भूल जाना कि shebang line #!/bin/bash syntactically technically एक comment है, लेकिन इसका OS loader के लिए special meaning सिर्फ तब होता है जब यह बिल्कुल पहली line हो।
चैप्टर सारांश
  • एक # से line के end तक कुछ भी Bash द्वारा ignore किया जाता है, quotes के अंदर या escaped होने को छोड़कर।
  • Bash में कोई native multi-line comment syntax नहीं है; हर line को अपना # चाहिए।
  • एक 'block comment' के लिए एक common trick : की ओर भेजा गया एक here-document है, : <<COMMENT ... COMMENT, क्योंकि : एक no-op command है।
  • अच्छे comments बताते हैं *क्यों* code कुछ करता है, सिर्फ *क्या* करता है नहीं (code खुद वह what दिखा देता है)।

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.