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

Shebang Line और Scripts चलाना

Shebang एक script में एक special पहली line है जो computer को exactly बताती है कि कौन सा program बची हुई file पढ़े और run करे।
Syntax
bash
#!/bin/bash
# script commands

chmod +x script_name.sh
./script_name.sh

Anatomy of the Shebang

Shebang #!/bin/bash operating system के loader को बताता है कि सीधे execute होने पर file का बाकी हिस्सा किस interpreter को handover करे।

यह file के पहले दो characters होनी चाहिए, कोई leading blank lines या spaces न हों, otherwise यह बस एक ordinary comment की तरह treat होती है।

Note: $0 script का अपना filename/path रखता है, यही कारण है कि head -n 1 "$0" shebang line वापस print करता है।

उदाहरण: Anatomy of the Shebang

bash
#!/bin/bash
echo "This script's interpreter is chosen by the first line"
head -n 1 "$0"

एक Script को Executable बनाना

./script.sh के रूप में एक script run करने से पहले, filesystem को chmod +x script.sh उपयोग करके इसे executable मार्क करना ज़रूरी है। उस permission bit के set न होने पर, kernel file को सीधे execute करने से refuse करता है और 'Permission denied' report करता है, भले ही file की contents बिल्कुल valid Bash हों।

उदाहरण: Making a Script Executable

bash
#!/bin/bash
cat <<'EOF' > demo.sh
#!/bin/bash
echo "hello from demo.sh"
EOF
chmod +x demo.sh
ls -l demo.sh
./demo.sh

./script.sh बनाम bash script.sh

./script.sh run करना kernel से file को directly execute करने के लिए कहता है, जिसके लिए execute bit चाहिए और interpreter चुनने के लिए shebang पर depend करता है। इसके बजाय bash script.sh run करना explicitly Bash start करता है और इसे file को एक argument के रूप में देता है, इसलिए strictly कोई execute permission या shebang ज़रूरी नहीं, लेकिन consistency के लिए shebang अब भी good practice है।

उदाहरण: ./script.sh vs bash script.sh

bash
#!/bin/bash
cat <<'EOF' > greet.sh
echo "hi, no shebang needed for this method"
EOF
bash greet.sh
chmod +x greet.sh
echo '#!/bin/bash' | cat - greet.sh > greet2.sh
chmod +x greet2.sh
./greet2.sh

यह Check करना कि कौन सा Bash उपयोग हो रहा है

#!/usr/bin/env bash bash को PATH environment variable उपयोग करके lookup करता है, /bin/bash hard-code करने के बजाय, जो उन systems पर मायने रखता है जहां Bash कहीं और रहता है (जैसे कुछ macOS या BSD setups)।

which bash या type bash exactly दिखाता है कि कौन सा binary उपयोग होगा।

Warning: #!/bin/bash hard-code करना ज़्यादातर Linux systems पर ठीक है, लेकिन #!/usr/bin/env bash अलग Unix-like systems में ज़्यादा portable है।

उदाहरण: Checking Which Bash Is Used

bash
#!/bin/bash
which bash
type bash
echo "Running under: $BASH"
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. यह भूल जाना कि shebang बिल्कुल पहली line होनी चाहिए, पहले दो characters (#!), इससे पहले कुछ भी नहीं -- इसके ऊपर एक blank line भी इसे तोड़ देती है।
  2. पहले chmod +x से executable बनाए बिना ./script.sh run करने की कोशिश करना, फिर एक 'Permission denied' error से confused होना।
  3. यह मान लेना कि ./script.sh और bash script.sh हमेशा identically behave करते हैं; अगर shebang missing या गलत हो, ./script.sh आपकी उम्मीद से अलग interpreter उपयोग कर सकता है, जबकि bash script.sh shebang को देखे बिना हमेशा Bash force करता है।
चैप्टर सारांश
  • Shebang line का exact form #!/bin/bash या #!/usr/bin/env bash file की बिल्कुल पहली line के रूप में होता है।
  • chmod +x script.sh execute permission देता है ताकि file को ./script.sh के रूप में directly run किया जा सके।
  • ./script.sh interpreter चुनने के लिए shebang उपयोग करता है; bash script.sh हमेशा Bash उपयोग करता है और execute permission या shebang की ज़रूरत नहीं।
  • #!/usr/bin/env bash उन systems में ज़्यादा portable है जहां Bash exactly /bin/bash पर installed न हो।

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.