Command Line
Это содержимое пока не доступно на вашем языке.
A command line interface is an interface that allows launching or executing commands (or programs) and communicating with them. On Windows, the most commonly used, simple CLI application (program) is the CMD (short for “command”).
To open CMD, you can hit the ⊞ Win button to pop open the search bar, type “cmd” to search for the CLI program and hit enter to run it.
How to run commands and programs?
Section titled “How to run commands and programs?”Commands (and programs) are run by typing their name (or path) and hitting Enter. Commands are directed at the CLI system (clear the screen, change the current working directory, etc.). Programs are run by the operating system. After starting a program, and until it completes, whatever you type into the CLI application can potentially be read by the program.
For example, let’s say you have a program named prog.exe.
In order to execute this program, you need to type its name into the CLI, and hit Enter.
This will execute the file named prog.exe found in the current directory (more on that later).
Typing in prog.exe and hitting Enter would issue a request to the operating system to run the program named prog.exe,
which would read the file and start executing the instructions from it.
.exeis optional to type on Windows, so just typingprogwould also start the same program..exeis one of few special extensions that work like this. To run programs with other extensions you need to type the name fully.
exestands for “executable”.
Current working directory
Section titled “Current working directory”CLI applications have the concept of the CWD (current working directory), which is a very important concept to understand when working with CLIs.
The current working directory refers to the folder that the program names will be resolved relative to.
So to start the file named prog.exe, it needs to be located in the current working directory.
If it’s not, the CLI won’t be able to locate it, and will give you an error that it couldn’t execute the program.
There can only be a single current working directory at any given time, and it’s typically always visible in the CLI application while you’re using it. It refers to any single folder on your computer, and it can be changed at will while using the CLI application, by typing in special commands (described later). Think of the CWD as a variable or a piece of state that the CLI application always keeps track of.
The CWD can also be viewed and interpreted by the programs you run on the command line. It can be used to, for example, be able to read files in the same folder you run the program from.
CWD navigation
Section titled “CWD navigation”You can change the working directory by using the cd command.
- To set the CWD (navigate) to a subfolder named “A”, you do
cd A. - To set the CWD to the parent directory, you do
cd ... The double dot can be used multiple times and even accompanied with normal child directory names, for examplecd ../../Awould mean “go back, go back, go to directory A”, so “go to directory A in the grandparent”. - Finally, if you want to change the working directory to some absolute path,
like
C:/Users/Anton/Documents/projectsorD:/Downloads/movies, you just typecdfollowed by that absolute path. - If you’re trying to switch e.g. from disk
Cto diskD, you also need to input the disk name you’re trying to switch to as a commandD:and hit Enter.
Environment variables
Section titled “Environment variables”The last concept related to CLIs described in this document is the idea of environment variables. These might sound scary, but they are just some named strings of characters that can be used by the CLI or the programs that you run from it.
The CLI keeps track of the values of all these environment variables at any given time.
It also allows the user to change them by using the set command, or “expand” them,
by typing %variable_name% in the CLI.
Just like the CWD, the environment variables can be read by the programs that you run.
The PATH Environment Variable
Section titled “The PATH Environment Variable”The most important is the PATH variable, which contains the folders
into which the operating system should look to resolve a program name.
For example, say the CWD is D:/Downloads, but your program is in C:/Programs and is named prog.exe.
You could go to the C:/Programs directory by changing the CWD and run prog.exe,
or you can run the program by typing its absolute path, C:/Programs/prog.exe,
but it’s not as convenient as just typing prog.exe and having it just work.
That’s what PATH is for.
If you set PATH to C:/Programs (where prog.exe is located),
then you’ll be able to run prog.exe from anywhere by typing its file name (prog.exe).
The operating system will try to find prog.exe by looking into
the directory specified in the PATH variable, aka the path C:/Programs, where it will find your program.
But if it didn’t find it in PATH, it would then look for it in the current directory.
PATH can be set to a ”;“-delimited list of paths,
which means it can include multiple such directory paths in which it should look for programs,
you just have to separate them with ”;”.
For example, C:/Folder1;C:/Folder2.
When you try to run a program, it will try and look into all of these in order, until it finds the program.
It will always try and find the program in the order of paths in PATH,
and only then look in the current directory.
However, there’s a workaround which allows you to run a program from the current directory only.
You do this by specifying the program’s relative path from the current directory.
In this case it will skip looking in the PATH directories and just look in the current directory.
You do this by prepending the program name with ./, for example ./prog.exe
(. means “the current directory”).
PATH Examples
Section titled “PATH Examples”For example, if PATH is set to C:/Folder1;C:/Folder2, with the folders containing:
C:/Folder1:prog1.exeprog2.exe
C:/Folder2:prog2.exeprog3.exeThen runningprog1.exein any directory will run the programprog1.exefromC:/Folder1, runningprog2.exewill run the programprog2.exefromC:/Folder1(because it will find that first), runningprog3.exewill run the programprog3.exefromC:/Folder2, and runningprog4.exewill fail to find it.
Exporting environment variables
Section titled “Exporting environment variables”The cool thing about environment variables is that they can be overridden for all programs on startup. This is called exporting the variable.
You can either do that in windows by using their GUI, or by using the
setx command in the CLI.
It can also be modified by programs e.g. on installation.
If you find that you can’t access some command on the command line, even though you know you’ve installed it, it probably means that the directory that this program is inside is not present in the
PATHvariable. You just need to find the program, and add the directory it’s in to thePATHvariable. It will work everywhere for sure after a system reload.