Comments का उपयोग
# full-line comment
command # trailing comment
Single-Line Comments
एक # character से उस line के end तक कोई भी text एक comment है और Bash द्वारा execute नहीं होता। Comments अपनी खुद की line पर आ सकते हैं या एक असली command के बाद उसी line पर trail कर सकते हैं।
उदाहरण: Single-Line Comments
#!/bin/bash
# This entire line is a comment and does nothing
echo "Hello" # this trailing part is also a comment
Login to try C/C++/Java/PHP code in the editor
Quotes के अंदर # Comment नहीं है
जब एक # single या double quotes के अंदर आता है, Bash इसे एक ordinary character मानता है, comment की शुरुआत नहीं। यह confusion का एक आम source है जब एक string को legitimately एक hash symbol चाहिए, जैसे एक hex color या एक hashtag।
उदाहरण: # Inside Quotes Is Not a Comment
#!/bin/bash
color="#ff00ff"
echo "The color code is $color"
Login to try C/C++/Java/PHP code in the editor
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
#!/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"
Login to try C/C++/Java/PHP code in the editor
- यह मानना कि Bash
/* ... */जैसे multi-line block comments support करता है; यह नहीं करता -- हर comment line को अपना खुद का#चाहिए (here-documents उपयोग करने वाले workarounds हैं, लेकिन वे असली comments नहीं हैं)। - एक string के बीच में
#डालना और उम्मीद करना कि वह अब भी एक comment शुरू करे; quotes के अंदर#बस एक literal character है, comment marker नहीं। - यह भूल जाना कि shebang line
#!/bin/bashsyntactically 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 दिखा देता है)।
Chapter Quiz — Complete all 12 topics to unlock
0/12 topics done
Complete these topics first: