Shebang Line और Scripts चलाना
In this page:
#!/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 होती है।
$0 script का अपना filename/path रखता है, यही कारण है कि head -n 1 "$0" shebang line वापस print करता है।उदाहरण: Anatomy of the Shebang
#!/bin/bash
echo "This script's interpreter is chosen by the first line"
head -n 1 "$0"
Login to try C/C++/Java/PHP code in the editor
एक 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
#!/bin/bash
cat <<'EOF' > demo.sh
#!/bin/bash
echo "hello from demo.sh"
EOF
chmod +x demo.sh
ls -l demo.sh
./demo.sh
Login to try C/C++/Java/PHP code in the editor
./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
#!/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
Login to try C/C++/Java/PHP code in the editor
यह 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 उपयोग होगा।
#!/bin/bash hard-code करना ज़्यादातर Linux systems पर ठीक है, लेकिन #!/usr/bin/env bash अलग Unix-like systems में ज़्यादा portable है।उदाहरण: Checking Which Bash Is Used
#!/bin/bash
which bash
type bash
echo "Running under: $BASH"
Login to try C/C++/Java/PHP code in the editor
- यह भूल जाना कि shebang बिल्कुल पहली line होनी चाहिए, पहले दो characters (
#!), इससे पहले कुछ भी नहीं -- इसके ऊपर एक blank line भी इसे तोड़ देती है। - पहले
chmod +xसे executable बनाए बिना./script.shrun करने की कोशिश करना, फिर एक 'Permission denied' error से confused होना। - यह मान लेना कि
./script.shऔरbash script.shहमेशा identically behave करते हैं; अगर shebang missing या गलत हो,./script.shआपकी उम्मीद से अलग interpreter उपयोग कर सकता है, जबकिbash script.shshebang को देखे बिना हमेशा Bash force करता है।
- Shebang line का exact form
#!/bin/bashया#!/usr/bin/env bashfile की बिल्कुल पहली line के रूप में होता है। chmod +x script.shexecute permission देता है ताकि file को./script.shके रूप में directly run किया जा सके।./script.shinterpreter चुनने के लिए shebang उपयोग करता है;bash script.shहमेशा Bash उपयोग करता है और execute permission या shebang की ज़रूरत नहीं।#!/usr/bin/env bashउन systems में ज़्यादा portable है जहां Bash exactly/bin/bashपर installed न हो।
Chapter Quiz — Complete all 12 topics to unlock
0/12 topics done
Complete these topics first: