Asked By – KayleL
I have noticed this in a couple of scripting languages, but in this example, I am using python. In many tutorials, they would start with
#!/usr/bin/python3 on the first line. I don’t understand why we have this.
- Shouldn’t the operating system know it’s a python script (obviously it’s installed since you are making a reference to it)
- What if the user is using a operating system that isn’t unix based
- The language is installed in a different folder for whatever reason
- The user has a different version. Especially when it’s not a full version number(Like Python3 vs Python32)
If anything, I could see this breaking the python script because of the listed reasons above.
Now we will see solution for issue: Purpose of #!/usr/bin/python3 shebang
#!/usr/bin/python3 is a shebang line.
A shebang line defines where the interpreter is located. In this case, the
python3 interpreter is located in
/usr/bin/python3. A shebang line could also be a
perl or any other scripting languages’ interpreter, for example:
Without the shebang line, the operating system does not know it’s a python script, even if you set the execution flag (
chmod +x script.py) on the script and run it like
./script.py. To make the script run by default in python3, either invoke it as
python3 script.py or set the shebang line.
You can use
#!/usr/bin/env python3 for portability across different systems in case they have the language interpreter installed in different locations.