Basics of using Shell commands
Getting Started with Shell Prompt
If the Linux system you are using does not have a graphical user interface (or is currently unavailable), you will see a Shell prompt after logging in. At this time, entering commands through the Shell will be the main way to use the Linux system.
For ordinary users, the default prompt is a simple dollar sign: $
, and for root users, the default prompt is #
.
On most Linux systems, the $
and #
prompts follow the user name, system name, and current directory name. For example, set /usr/share/ on the computer named aliyun as the current working directory of user jackma, and its login prompt is as follows:
[ jackma@aliyun share]$
At this time, [ jackma@aliyun share]$
the meaning of each part is as follows:
jackma
Is the username of the currently logged in user.aliyun
is the name of the computer.share
is the name of the current working directory.
Understand Shell command syntax
Shell commands are the basic tools for operating and controlling computers in Linux systems. The following is the general syntax of the Shell command:
command [options] [arguments]
command
is the name of the command to be executed.options
Are optional command options used to modify the behavior of the command or provide additional functionality.arguments
Is the parameter or operation object of the command.
Optional commands [options]
In Shell command syntax, [options]
the and [arguments]
parts are optional.
[options]
Is a flag used to modify command behavior or provide additional functionality. Usually begins with a single dash-
or double dash , followed by one or more letters, numbers, or other specific characters.--
Different commands have different options, and multiple options can be combined as needed.
For example, ls
the command has some commonly used options:
-l
: Display file and directory details in long format.-a
: Includes showing hidden files and directories.-r
: Sort in reverse order.
To use [options]
, you can place it after the command name like this:
ls -l -a
Another common practice is to group multiple options together to reduce typing. You can use a single dash to connect all options together:
ls -la
The above two ways of writing are equivalent. In addition to a single dash -
, a double dash --
is usually followed by a complete word, such as:
ls --help
Optional parameters [arguments]
[arguments]
Is the parameter or operation object of the command. They are input to a command and specify the files, directories, or other data that the command operates on.
For example, cp
the command requires two parameters to specify the paths to the source and destination files:
cp source_file destination_file
In this example, source_file
it is the path to the source file to be copied, destination_file
but the path to the destination file, i.e. the location to which the source file is copied.
The specific usage of parameters depends on the different commands and operations.
It's worth noting that not all commands require options or parameters. Some commands may have no options and only require a simple command name to complete the operation. For example, the date command is used to display the current date and time:
date
View all shell commands
In a shell, to find a command you enter, the shell looks in what's called a "path." If the command is not on the path, you need to enter the complete identifier of the command location.
One way to run a command is to enter the full path or absolute path to execute it. For example, the way to run the date command in the /bin directory is as follows:
$ /bin/date
This method is not convenient for commands located in directories with long paths. A better approach is to store commands in known directories and add those directories to the shell's PATH
environment variables. PATH
An environment variable consists of a string containing a list of directories. When a command is entered, the shell checks the list to find the corresponding command.
To view the current path, enter the following command:
echo $PATH
The output will show a common default path for ordinary Linux users. Directories in the path list are separated by colons (:). Most user commands provided by Linux systems are stored in the /bin
, /usr/bin
or /usr/local/bin
directory. The /sbin
and /usr/sbin
directories contain administrative commands (some Linux systems may not place these directories in the normal user path).
By adding the directory where the command is located to the PATH environment variable, you can enter the command name directly on the command line without entering the full path to execute the command. This can improve the convenience of using the command.
Next, view all commands under a folder:
ls /usr/bin
Command history
In the Shell, you can use the history command to view a list of previously executed commands. This command will display a list of historical commands with line numbers, each command corresponding to its corresponding line number.
history
To repeat a previous command, you can use one of the following methods:
Use !!
symbols to execute the last command. For example, enter !!
and press Enter to repeat the previous command.
Use !n
to execute a command at a specific line number, where n is the line number of the command in the history. For example, enter !5
and press the Enter key to execute the fifth command.
Or press the up and down keys to select the most recent command.
Metacharacters
In the shell, metacharacters are characters with special meaning that control how the command line is parsed and executed. They are used in the shell for pattern matching, redirecting input and output, pipe operations, etc. Here are some common shell metacharacters:
-
Wildcard:
*
: Matches any number of characters (including zero characters).?
: Matches a single character.
-
Redirect:
>
: Redirect the output of the command to a file, overwriting the existing content.>>
:Append the output of the command to the end of the file.<
: Use the file contents as input to the command.
-
Pipe:
|
: Use the output of one command as the input of another command.
-
Escape Character:
\
: Used to escape the next character to prevent it from being interpreted as a special character.
-
quotation marks:
'text'
: Single quotation marks, used to completely retain the text within the quotation marks, and special characters in them will not be interpreted."text"
: Double quotes, used to preserve the text within the quotes, but some special characters (such as variables) will be interpreted.
-
Command separator:
;
: Used to separate multiple commands so that they can be executed sequentially.
These metacharacters play an important role in Shell commands and can perform operations such as pattern matching, file redirection, and input and output control. When using these metacharacters, you need to pay attention to the escaping of special characters and the use of quotation marks to ensure the correct execution of the command and the expected results.