search
HomeBackend DevelopmentPython Tutorial#/bin/python是运行什么命令的?

#/bin/python是运行什么命令的?求大牛详解。。。
初学Python,练习的时候遇到这个,被鄙视,呜呜~~~~~~~~~~~~~~恨没姿势,求涨~~

回复内容:

首先,楼主,你打漏了一个字符,正确的是

#!/bin/python

要了解这行是干啥用的,得先了解一个“魔数(magic number)”的概念。

每一种文件,在文件开头,都有一个标识文件类型的字节序列,标识这个文件是什么类型的。这个字节序列叫““魔数”。

像unix下的脚本,这个魔数就是"#!"。这个魔数的英文名叫“Shebang”。


操作系统的文件解释器在加载文件时,先要判断文件是什么类型的,然后再加载特定的文件解释器,来解读这个文件。操作系统的文件解释器判断的依据,就是看这个魔数。

所以,当看到"#!"时,操作系统的文件解释器知道这是一个“脚本”。

那具体用哪一种脚本解释器呢?它继续读。

结果发现/bin/python。

也就是要用/bin/python 这个路径下的一个叫“python”的解释器来解释这个文件。

于是操作系统的文件解释器就加载了这个python解释器。 然后操作系统的文件解释器任务就完成了。

当python解释器被加载运行后,它自己来解释这个文件。

这时,它看到第一行: #!/bin/python

它认得一开头的#是标记注释行开始的记号,所以它认为这是一行注释,就直接跳过。
然后开始执行后续的真正的python指令。

综上,这一行,被两个层次的解释器以不同的形式解释。 这就是一行注释!
在#后面加上!才是解释器定义。 这是你在linux/unix系统下运行一个脚本的时候,告诉系统用什么解释器来运行脚本用的
比如你执行一个脚本
$ ./script
#!/bin/python 不是命令,是定义Python解释器位置的。就是告诉系统在什么地方可以找到用于这个脚本的Python解释器。
比如,#!/bin/python3 是定义Python3位置的。
我一般这么写,#!/use/bin/env python
这样可以让系统协助寻找Python解释器,可以在多个系统里面通用。 你用的什么教材?换一本吧,没解释这句的书就不用看了 提醒 UNIX系统 用 /bin/python 来解释执行这个脚本


head -1 abc.sh
#! /bin/bash 
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Python's Hybrid Approach: Compilation and Interpretation CombinedPython's Hybrid Approach: Compilation and Interpretation CombinedMay 08, 2025 am 12:16 AM

Pythonusesahybridapproach,combiningcompilationtobytecodeandinterpretation.1)Codeiscompiledtoplatform-independentbytecode.2)BytecodeisinterpretedbythePythonVirtualMachine,enhancingefficiencyandportability.

Learn the Differences Between Python's 'for' and 'while' LoopsLearn the Differences Between Python's 'for' and 'while' LoopsMay 08, 2025 am 12:11 AM

ThekeydifferencesbetweenPython's"for"and"while"loopsare:1)"For"loopsareidealforiteratingoversequencesorknowniterations,while2)"while"loopsarebetterforcontinuinguntilaconditionismetwithoutpredefinediterations.Un

Python concatenate lists with duplicatesPython concatenate lists with duplicatesMay 08, 2025 am 12:09 AM

In Python, you can connect lists and manage duplicate elements through a variety of methods: 1) Use operators or extend() to retain all duplicate elements; 2) Convert to sets and then return to lists to remove all duplicate elements, but the original order will be lost; 3) Use loops or list comprehensions to combine sets to remove duplicate elements and maintain the original order.

Python List Concatenation Performance: Speed ComparisonPython List Concatenation Performance: Speed ComparisonMay 08, 2025 am 12:09 AM

ThefastestmethodforlistconcatenationinPythondependsonlistsize:1)Forsmalllists,the operatorisefficient.2)Forlargerlists,list.extend()orlistcomprehensionisfaster,withextend()beingmorememory-efficientbymodifyinglistsin-place.

How do you insert elements into a Python list?How do you insert elements into a Python list?May 08, 2025 am 12:07 AM

ToinsertelementsintoaPythonlist,useappend()toaddtotheend,insert()foraspecificposition,andextend()formultipleelements.1)Useappend()foraddingsingleitemstotheend.2)Useinsert()toaddataspecificindex,thoughit'sslowerforlargelists.3)Useextend()toaddmultiple

Are Python lists dynamic arrays or linked lists under the hood?Are Python lists dynamic arrays or linked lists under the hood?May 07, 2025 am 12:16 AM

Pythonlistsareimplementedasdynamicarrays,notlinkedlists.1)Theyarestoredincontiguousmemoryblocks,whichmayrequirereallocationwhenappendingitems,impactingperformance.2)Linkedlistswouldofferefficientinsertions/deletionsbutslowerindexedaccess,leadingPytho

How do you remove elements from a Python list?How do you remove elements from a Python list?May 07, 2025 am 12:15 AM

Pythonoffersfourmainmethodstoremoveelementsfromalist:1)remove(value)removesthefirstoccurrenceofavalue,2)pop(index)removesandreturnsanelementataspecifiedindex,3)delstatementremoveselementsbyindexorslice,and4)clear()removesallitemsfromthelist.Eachmetho

What should you check if you get a 'Permission denied' error when trying to run a script?What should you check if you get a 'Permission denied' error when trying to run a script?May 07, 2025 am 12:12 AM

Toresolvea"Permissiondenied"errorwhenrunningascript,followthesesteps:1)Checkandadjustthescript'spermissionsusingchmod xmyscript.shtomakeitexecutable.2)Ensurethescriptislocatedinadirectorywhereyouhavewritepermissions,suchasyourhomedirectory.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools