What Are #! Shebangs For in Scripts?

NeuralNine · Beginner ·💻 AI-Assisted Coding ·2y ago

Key Takeaways

Explains the role of shebangs in script files

Full Transcript

what is going on guys welcome back in this video today we're going to talk about shebang which is the Linux way of determining which interpreter should run a certain executable file so let's get right into it [Music] alright so chances are you've already encountered a shebang before maybe in a python script maybe in a bash script and you didn't really know what it was doing what it was there for a shebang is basically this top line that uses a hashtag and an exclamation mark followed by a file path and maybe an argument to specify which interpreter which application should be used to run this specific file so for example I might have a main.py script and you might see at the top of the script this hashtag exclamation mark and then something like user bin and then Python 3 for example this is one example of how this could look or in a bash file you might see user bin bash for example now the question is why do we even need that line to specify which application should be used for this particular file if it's already having the python file extension so it's already called main.py why do I need to specify this shebang that specifies Python 3. and the thing is you don't need to do that if you run this with python3 so if you go ahead and you to run your script with um Python 3 main.py you don't need a shebang for this because you're specifying what application you want to use to run this particular script however in Linux you can make files executable you can give them the executable Flag by saying change mode so chmod and then plus X and then Main py in my case now main py doesn't exist because I didn't save the file but this would make this file an executable which means that I can run it without specifying python before it I can run it by saying Point slash main.py and I can do that with any file it can also have no file extension so I can say run my file provided that my file is an executable file it has this x flag I can run this as an executable and I can do that with shell shell scripts I can do that with python files it doesn't really matter but the thing is I need to specify this shebang because this shebang if the file is not a binary file specifies which application should be used to load this file now again back to the question why do I need this if I already name the file main.py and the answer to that is Linux doesn't really care about file extensions the idea that you determine a files file type by looking at the file extension comes from Windows so Windows does that Windows looks at dot py dot docx dot txt and so on and then specifies or or handles default as this specific file type in Linux we don't do that and Linux files are handled by shebangs by certain signatures for example jpegs are handled by the pattern you can save a file as a DOT PNG if the file itself has the jpeg structure Linux is going to recognize it as a JPEG file not as a PNG file whereas windows will tell you couldn't parse this PNG file because Windows cares about the file extension Linux does not which means that I can go ahead and I can create a python script here and I can say print uh hello world for example and of course I can just go ahead now and run this so I can say python3 main.py and this works but I can also go ahead now and say that actually even though it's a py file Linux doesn't care that it's a py file I can specify Asia bang that says uh Slash user slash bin slash bash and this will use bash then now of course if I run this with Python 3 it should still work as you can see but if I now say okay my main py file should be an executable file so I'm going to give it the X flag um now I can go ahead and run this file and you will see it says syntax error it doesn't know what this is because this is now treated as a bash file whereas if I go ahead and I change this to user bin and then python3 you will see that this works as a python file so I don't need to specify Python 3 it already uses Python 3 because of the shebang and even more interesting if I change this to bash and I change the code to Echo hello world so this is now bash code then I get hello world because I'm running this as a bash script even though it's a py file because the file extension again doesn't matter on Linux Linux cares about the content Linux cares about the structure and about the shebang in case of scripts um so yeah this is how you can specify this so so you can have a shell file you can have a docx file it doesn't matter what I call this file I can call this file uh main dot xlsx which is now an Excel file if I go into that file and I say that this is a python script and I print even though Bim here doesn't recognize this as a python script anymore as you can see there's no syntax highlighting it doesn't matter because that is still a python script for Linux because of the shebang now of course if I remove the shebang it's not going to recognize this as a python script anymore by default it's going to assume it's a shell script so A bash script um and it's going to try to run it with bash but you can see it doesn't matter what the file extension is now that's basically what a shebang is for now there's a caveat here because you can also specify an indirect path if you want to have for example a very long path or if you have a very long path or if you want to keep it Dynamic you don't want to specify a fixed path which you do oftentimes with python is you read it from the environment so what you can do is let's move this back here to a a py file main.py let's add a ship bang here now instead of saying user bin Python 3 which is the exact path to python 3. what I can do especially because when my system python might be here but on other systems it might be somewhere else so maybe I don't want to do it just for my system I want Python 3 to be used wherever it is at maybe it's not at user bin python so what I can do is I can go user bin and then environment so NF and I can read Python 3 from the environment so it's going to give me the path to Python 3 uh using this nth command and then I can go ahead again and say hello world and then I can just say Point slash mean p y and there you go and I think actually we should even be able to just run this and see what this puts out okay yeah this basically takes from the environment the executable as you can see so it just runs this uh yeah so this is what you can do maybe one thing that's interesting to know is that the limit for the shebang is 127 characters as far as I know so if you have a very long path you maybe want to use it from the environment so you maybe want to specify uh the executable or the name here and then you want it load it from the environment because you cannot fit more than 127 characters into the shebang and yeah that's basically what a shebang is for it doesn't really make sense to use it on Windows it's a Linux thing because Linux again doesn't care about the file extension and I've seen this many times throughout my coding journey and I always ask myself okay what do I need this for I mean it's already a python script I already run it with Python 3 even on Linux I can just run it with Python 3 why do I need to specify this now you know if you've had the same question you just have to specify this because if you run this as an executable of course if you always use Python 3 it doesn't matter you can leave out the shebang because Python 3 already specifies uh or you already specify that you're going to use Python 3 but chances are you don't do that you want to run this as an executable and then it's important to specify the shebang because otherwise it's going to assume that this is a script so that's it for today's video I hope you enjoyed it and hope you learned something if so let me know by hitting the like button and leaving a comment in the comment section down below and of course don't forget to subscribe to this Channel and hit the notification Bell to not miss a single future video for free other than that thank you much for watching see you next video and bye

Original Description

Today we learn about shebangs and what their role in script files is. ◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾ 📚 Programming Books & Merch 📚 🐍 The Python Bible Book: https://www.neuralnine.com/books/ 💻 The Algorithm Bible Book: https://www.neuralnine.com/books/ 👕 Programming Merch: https://www.neuralnine.com/shop 💼 Services 💼 💻 Freelancing & Tutoring: https://www.neuralnine.com/services 🌐 Social Media & Contact 🌐 📱 Website: https://www.neuralnine.com/ 📷 Instagram: https://www.instagram.com/neuralnine 🐦 Twitter: https://twitter.com/neuralnine 🤵 LinkedIn: https://www.linkedin.com/company/neuralnine/ 📁 GitHub: https://github.com/NeuralNine 🎙 Discord: https://discord.gg/JU4xr8U3dm
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from NeuralNine · NeuralNine · 0 of 60

← Previous Next →
1 Visualizing Stock Data With Candlestick Charts in Python
Visualizing Stock Data With Candlestick Charts in Python
NeuralNine
2 Python Beginner Tutorial #1 - Installation and First Program
Python Beginner Tutorial #1 - Installation and First Program
NeuralNine
3 Python Beginner Tutorial #2 - Variables and Data Types
Python Beginner Tutorial #2 - Variables and Data Types
NeuralNine
4 Python Beginner Tutorial #3 - Operators and User Input
Python Beginner Tutorial #3 - Operators and User Input
NeuralNine
5 Python Beginner Tutorial #4 - If Statements and Conditions
Python Beginner Tutorial #4 - If Statements and Conditions
NeuralNine
6 Python Beginner Tutorial #5 - Loops
Python Beginner Tutorial #5 - Loops
NeuralNine
7 Python Beginner Tutorial #6 - Sequences and Collections
Python Beginner Tutorial #6 - Sequences and Collections
NeuralNine
8 Python Beginner Tutorial #7 - Functions
Python Beginner Tutorial #7 - Functions
NeuralNine
9 Python Beginner Tutorial #8 - Exception Handling
Python Beginner Tutorial #8 - Exception Handling
NeuralNine
10 Python Beginner Tutorial #9 - File Operations
Python Beginner Tutorial #9 - File Operations
NeuralNine
11 Python Beginner Tutorial #10 - String Functions
Python Beginner Tutorial #10 - String Functions
NeuralNine
12 Python Intermediate Tutorial #1 - Classes and Objects
Python Intermediate Tutorial #1 - Classes and Objects
NeuralNine
13 Python Intermediate Tutorial #2 - Inheritance
Python Intermediate Tutorial #2 - Inheritance
NeuralNine
14 Python Intermediate Tutorial #3 - Multithreading
Python Intermediate Tutorial #3 - Multithreading
NeuralNine
15 Python Intermediate Tutorial #4 - Synchronizing Threads
Python Intermediate Tutorial #4 - Synchronizing Threads
NeuralNine
16 Python Intermediate Tutorial #5 - Events and Daemon Threads
Python Intermediate Tutorial #5 - Events and Daemon Threads
NeuralNine
17 Python Intermediate Tutorial #6 - Queues
Python Intermediate Tutorial #6 - Queues
NeuralNine
18 Python Intermediate Tutorial #7 - Sockets and Network Programming
Python Intermediate Tutorial #7 - Sockets and Network Programming
NeuralNine
19 Python Intermediate Tutorial #8 - Database Programming
Python Intermediate Tutorial #8 - Database Programming
NeuralNine
20 Python Intermediate Tutorial #9 - Recursion
Python Intermediate Tutorial #9 - Recursion
NeuralNine
21 Python Intermediate Tutorial #10 - XML Processing
Python Intermediate Tutorial #10 - XML Processing
NeuralNine
22 Python Intermediate Tutorial #11 - Logging
Python Intermediate Tutorial #11 - Logging
NeuralNine
23 Python Data Science Tutorial #1 - Anaconda and PyCharm Setup
Python Data Science Tutorial #1 - Anaconda and PyCharm Setup
NeuralNine
24 Python Data Science Tutorial #2 - NumPy Arrays
Python Data Science Tutorial #2 - NumPy Arrays
NeuralNine
25 Python Data Science Tutorial #3 - Numpy Functions
Python Data Science Tutorial #3 - Numpy Functions
NeuralNine
26 Python Data Science Tutorial #4 - Plotting Functions With Matplotlib
Python Data Science Tutorial #4 - Plotting Functions With Matplotlib
NeuralNine
27 Python Data Science Tutorial #5 - Subplots and Multiple Windows
Python Data Science Tutorial #5 - Subplots and Multiple Windows
NeuralNine
28 Python Data Science Tutorial #6 - Matplotlib Styling
Python Data Science Tutorial #6 - Matplotlib Styling
NeuralNine
29 Python Data Science Tutorial #7 - Bar Charts with Matplotlib
Python Data Science Tutorial #7 - Bar Charts with Matplotlib
NeuralNine
30 Python Data Science Tutorial #8 - Pie Charts with Matplotlib
Python Data Science Tutorial #8 - Pie Charts with Matplotlib
NeuralNine
31 Python Data Science Tutorial #9 - Plotting Histograms with Matplotlib
Python Data Science Tutorial #9 - Plotting Histograms with Matplotlib
NeuralNine
32 Python Data Science Tutorial #10 - Scatter Plots with Matplotlib
Python Data Science Tutorial #10 - Scatter Plots with Matplotlib
NeuralNine
33 Python Data Science Tutorial #11 - 3D Plotting with Matplotlib
Python Data Science Tutorial #11 - 3D Plotting with Matplotlib
NeuralNine
34 Python Data Science Tutorial #12 - Pandas Series
Python Data Science Tutorial #12 - Pandas Series
NeuralNine
35 Python Data Science Tutorial #13 - Pandas Data Frames
Python Data Science Tutorial #13 - Pandas Data Frames
NeuralNine
36 Python Data Science Tutorial #14 - Pandas Statistics
Python Data Science Tutorial #14 - Pandas Statistics
NeuralNine
37 Python Data Science Tutorial #15 - Pandas Sorting and Functions
Python Data Science Tutorial #15 - Pandas Sorting and Functions
NeuralNine
38 Python Data Science Tutorial #16 - Pandas Merging Data Frames
Python Data Science Tutorial #16 - Pandas Merging Data Frames
NeuralNine
39 Python Data Science Tutorial #17 - Pandas Queries
Python Data Science Tutorial #17 - Pandas Queries
NeuralNine
40 Python Machine Learning Tutorial #1 - What is Machine Learning?
Python Machine Learning Tutorial #1 - What is Machine Learning?
NeuralNine
41 Python Machine Learning Tutorial #2 - Linear Regression
Python Machine Learning Tutorial #2 - Linear Regression
NeuralNine
42 Python Machine Learning Tutorial #3 - K-Nearest Neighbors Classification
Python Machine Learning Tutorial #3 - K-Nearest Neighbors Classification
NeuralNine
43 Python Machine Learning #4 - Support Vector Machines
Python Machine Learning #4 - Support Vector Machines
NeuralNine
44 Python Machine Learning Tutorial #5 - Decision Trees and Random Forest Classification
Python Machine Learning Tutorial #5 - Decision Trees and Random Forest Classification
NeuralNine
45 Python Machine Learning Tutorial #6 - K-Means Clustering
Python Machine Learning Tutorial #6 - K-Means Clustering
NeuralNine
46 Python Machine Learning Tutorial #7 - Neural Networks
Python Machine Learning Tutorial #7 - Neural Networks
NeuralNine
47 Python Machine Learning Tutorial #8 - Handwritten Digit Recognition with Tensorflow
Python Machine Learning Tutorial #8 - Handwritten Digit Recognition with Tensorflow
NeuralNine
48 Generating Poetic Texts with Recurrent Neural Networks in Python
Generating Poetic Texts with Recurrent Neural Networks in Python
NeuralNine
49 Stock Portfolio Visualization with Matplotlib in Python
Stock Portfolio Visualization with Matplotlib in Python
NeuralNine
50 Analyzing Coronavirus with Python (COVID-19)
Analyzing Coronavirus with Python (COVID-19)
NeuralNine
51 Making Text Images Readable Again with Python and OpenCV
Making Text Images Readable Again with Python and OpenCV
NeuralNine
52 Neural Networks Simply Explained (Theory)
Neural Networks Simply Explained (Theory)
NeuralNine
53 Motion Filtering with OpenCV in Python
Motion Filtering with OpenCV in Python
NeuralNine
54 Top 5 Programming Languages To Learn in 2020
Top 5 Programming Languages To Learn in 2020
NeuralNine
55 Simple TCP Chat Room in Python
Simple TCP Chat Room in Python
NeuralNine
56 Image Classification with Neural Networks in Python
Image Classification with Neural Networks in Python
NeuralNine
57 Edge Detection with OpenCV in Python
Edge Detection with OpenCV in Python
NeuralNine
58 S&P 500 Web Scraping with Python
S&P 500 Web Scraping with Python
NeuralNine
59 Simple Sentiment Text Analysis in Python
Simple Sentiment Text Analysis in Python
NeuralNine
60 Introduction - Algorithms & Data Structures #1
Introduction - Algorithms & Data Structures #1
NeuralNine

Related Reads

📰
Something I've Been Building Is Almost Ready. 🚀
Learn how building an AI application can challenge and teach you new skills, and why it matters for career growth
Dev.to · AGUNWA CHIDIEBELE
📰
Building a Vibe-Based Music Recommender with MongoDB and Voyage AI
Learn to build a music recommender system using MongoDB and Voyage AI that suggests songs based on vibe, not just artist or genre
Dev.to · Jordan Smith
📰
AI Wrote Your Code. Did It Ship a Vulnerability Too?
AI-generated code often fails basic security tests, highlighting the need for careful review and testing
Dev.to · Stanley A.
📰
Beyond Vibe Coding: Embracing Agentic Engineering for Sustainable AI Development
Learn to move beyond vibe coding and adopt agentic engineering for sustainable AI development
Medium · AI
Up next
We Studied 10,000 Devs Using AI. This Is Where They Fail.
SCALER
Watch →