What Are #! Shebangs For in Scripts?
Skills:
Agentic Coding60%
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Visualizing Stock Data With Candlestick Charts in Python
NeuralNine
Python Beginner Tutorial #1 - Installation and First Program
NeuralNine
Python Beginner Tutorial #2 - Variables and Data Types
NeuralNine
Python Beginner Tutorial #3 - Operators and User Input
NeuralNine
Python Beginner Tutorial #4 - If Statements and Conditions
NeuralNine
Python Beginner Tutorial #5 - Loops
NeuralNine
Python Beginner Tutorial #6 - Sequences and Collections
NeuralNine
Python Beginner Tutorial #7 - Functions
NeuralNine
Python Beginner Tutorial #8 - Exception Handling
NeuralNine
Python Beginner Tutorial #9 - File Operations
NeuralNine
Python Beginner Tutorial #10 - String Functions
NeuralNine
Python Intermediate Tutorial #1 - Classes and Objects
NeuralNine
Python Intermediate Tutorial #2 - Inheritance
NeuralNine
Python Intermediate Tutorial #3 - Multithreading
NeuralNine
Python Intermediate Tutorial #4 - Synchronizing Threads
NeuralNine
Python Intermediate Tutorial #5 - Events and Daemon Threads
NeuralNine
Python Intermediate Tutorial #6 - Queues
NeuralNine
Python Intermediate Tutorial #7 - Sockets and Network Programming
NeuralNine
Python Intermediate Tutorial #8 - Database Programming
NeuralNine
Python Intermediate Tutorial #9 - Recursion
NeuralNine
Python Intermediate Tutorial #10 - XML Processing
NeuralNine
Python Intermediate Tutorial #11 - Logging
NeuralNine
Python Data Science Tutorial #1 - Anaconda and PyCharm Setup
NeuralNine
Python Data Science Tutorial #2 - NumPy Arrays
NeuralNine
Python Data Science Tutorial #3 - Numpy Functions
NeuralNine
Python Data Science Tutorial #4 - Plotting Functions With Matplotlib
NeuralNine
Python Data Science Tutorial #5 - Subplots and Multiple Windows
NeuralNine
Python Data Science Tutorial #6 - Matplotlib Styling
NeuralNine
Python Data Science Tutorial #7 - Bar Charts with Matplotlib
NeuralNine
Python Data Science Tutorial #8 - Pie Charts with Matplotlib
NeuralNine
Python Data Science Tutorial #9 - Plotting Histograms with Matplotlib
NeuralNine
Python Data Science Tutorial #10 - Scatter Plots with Matplotlib
NeuralNine
Python Data Science Tutorial #11 - 3D Plotting with Matplotlib
NeuralNine
Python Data Science Tutorial #12 - Pandas Series
NeuralNine
Python Data Science Tutorial #13 - Pandas Data Frames
NeuralNine
Python Data Science Tutorial #14 - Pandas Statistics
NeuralNine
Python Data Science Tutorial #15 - Pandas Sorting and Functions
NeuralNine
Python Data Science Tutorial #16 - Pandas Merging Data Frames
NeuralNine
Python Data Science Tutorial #17 - Pandas Queries
NeuralNine
Python Machine Learning Tutorial #1 - What is Machine Learning?
NeuralNine
Python Machine Learning Tutorial #2 - Linear Regression
NeuralNine
Python Machine Learning Tutorial #3 - K-Nearest Neighbors Classification
NeuralNine
Python Machine Learning #4 - Support Vector Machines
NeuralNine
Python Machine Learning Tutorial #5 - Decision Trees and Random Forest Classification
NeuralNine
Python Machine Learning Tutorial #6 - K-Means Clustering
NeuralNine
Python Machine Learning Tutorial #7 - Neural Networks
NeuralNine
Python Machine Learning Tutorial #8 - Handwritten Digit Recognition with Tensorflow
NeuralNine
Generating Poetic Texts with Recurrent Neural Networks in Python
NeuralNine
Stock Portfolio Visualization with Matplotlib in Python
NeuralNine
Analyzing Coronavirus with Python (COVID-19)
NeuralNine
Making Text Images Readable Again with Python and OpenCV
NeuralNine
Neural Networks Simply Explained (Theory)
NeuralNine
Motion Filtering with OpenCV in Python
NeuralNine
Top 5 Programming Languages To Learn in 2020
NeuralNine
Simple TCP Chat Room in Python
NeuralNine
Image Classification with Neural Networks in Python
NeuralNine
Edge Detection with OpenCV in Python
NeuralNine
S&P 500 Web Scraping with Python
NeuralNine
Simple Sentiment Text Analysis in Python
NeuralNine
Introduction - Algorithms & Data Structures #1
NeuralNine
More on: Agentic Coding
View skill →Related Reads
📰
📰
📰
📰
Something I've Been Building Is Almost Ready. 🚀
Dev.to · AGUNWA CHIDIEBELE
Building a Vibe-Based Music Recommender with MongoDB and Voyage AI
Dev.to · Jordan Smith
AI Wrote Your Code. Did It Ship a Vulnerability Too?
Dev.to · Stanley A.
Beyond Vibe Coding: Embracing Agentic Engineering for Sustainable AI Development
Medium · AI
🎓
Tutor Explanation
DeepCamp AI