Home > Article > Operation and Maintenance > What does linux executable file refer to?
In Linux, an executable file means that the file can be run and the file has "x" permissions; the executable file can be a code file (script file) or A binary file. There are two ways to run a file: 1. Use the interpreter to run the file directly; 2. Use "./" to run the file, and the syntax is "./ file name".
An executable file refers to a file that can be run. This file can be a code file or a Binary files
There are only files and directories in the Linux file system (everything is a file).
In Linux, there are two main ways to run a file: bash test.sh
or ./test.sh
.
This test.sh
is called an executable file.
The executable file is divided into two Two types: one is a common binary file, and the other is a script file. The former, such as the commonly used ls
, mkdir
commands, etc. are binary files, while the latter is a common shell script. , or Python scripts that rely on interpreter execution.
You can check the type of a file through the command file
. For example, a binary file will display:
The script file outputs:
can be found in the above two script files. You can see that the output contents are different. One is recognized as shell script
and the other is considered to be an ordinary text file. But both files are executable:
As shown in the figure: After running both files, they will output hello world!
.
Two different running methods are used here. The first one directly specifies an available Execute file ./hello.sh
. If run in this way, the script must meet the following two requirements:
The file has "x" permissions
If the file is not a binary type file, then its interpreter must be specified in the first line at the beginning
The first one refers to the Linux file "rwx" in the system r means read-only, w means writable, and "x" means that the file is executable.
Determining whether the file is executable here is somewhat similar to "duck type" , that is, as long as an animal can quack, I think it is a duck. At this time, even if a dog walks over and quacks twice, then I think it is a duck.
After conversion, no matter what the file type is, as long as this file With "X" permission, the system considers it to be an executable file.
Example: Create a new text type file with the content hello world!
, and then add "x "Execution permission.
You can see that when viewing the file type, it is consistent with the content printed by the Python script above, thinking that this is a txt file.
Then Add execution permission to it, and then run './', prompting "Command not found".
Here, the system treats it as a shell script to execute, reads the content from it in turn, and then submits Give the "/bin/bash" interpreter to execute.
Then follow the same method and turn the Python script into an executable file recognized by the system
At this point, you can also use ./
to run it. What does
There are two ways to run a file , the first is to use his interpreter to run it directly, such as:
Each interpreter can only run scripts corresponding to its own syntax. If you use If other interpreters are used, an error will be thrown. For example, the Python interpreter cannot run the shell script. Directory", and "/" is an ordinary path separator. When combined together, it is nothing more than a relative path, such as:
, so when executing an executable recognized by the system When it comes to files,
is not the most important part, the file itself is. For example, I can run an executable file with an absolute path:
If you remove the
in front, and then delete the .py
at the end, that is, just enter "hello" and print ahello world! , then this file seems to be no different from a command in the Linux system.
Next, turn it into a "command" that is the same as ls
.
First Renamed the executable file
Then put it in the /usr/bin directory
Input hello directly and print directly hello world!
This also explains from the side, as long as you turn a script into an executable file recognized by the system and put it in /usr /bin directory, it is no different from commonly used commands.
In fact, there is The variable is called PATH. This variable is a set of paths. When you enter a command (such as ls), it will look for the corresponding directory in the content of this variable. name of the file, and then run it.
So, if I can add the current directory to this variable, then the files in the current directory can be run directly by the system without specifying a path.
If a file does not have an "x" attribute line, it cannot be executed even if it is dropped into the /usr/bin directory.
Generally, the PATH variable is rarely declared directly. When operating on this variable, it will also be written to the ~/.bashrc
directory or the two environment variable files of /etc/profile
, in this way, after the session is connected, the content inside will be automatically run, and this directory will be added to the PATH variable.
The above is the detailed content of What does linux executable file refer to?. For more information, please follow other related articles on the PHP Chinese website!