The shell foundation

  • The shell is a command-line interpreter that provides the user with an interface system-level program that sends requests to the Linux kernel to run the program

  • The shell can be used to start, suspend, stop, or write programs

  • The Shell is also a powerful transformation language

  • The Shell is a scripting language that interprets execution, and Linux commands can be invoked directly from the Shell

echo

  • The output command

  • -e: activates the escape character

echo hello
echo -e "a\tb"
Copy the code

Write the executionshell

  • hello.sh

All shell files start with #! /bin/bash indicates that this is a shell script and you need to use /bin/bash to execute the file.

#! /bin/bash
echo hello
Copy the code
Sh // Run the sh hello.sh script to grant executable permission to the file. Run chmode 755 hello.sh chmode u+x hello.sh./hello.shCopy the code

The alias

  • Commands can specify aliases, which are custom command shortcuts
  • useAlias [shortcut]=[original command]Specify, for examplealias cp="cp -i"It means to specifycpThe command is executed.cp -i
  • Write the shortcut to the environment variable configuration filevi ~/.bashrcTo permanently save the command shortcut.
  • source ~/.bashrcRefresh the file execution, after the execution can be called through the shortcut.
  • In accordance with the$PATHThe order of directory lookup defined by the environment variable executes the first command found
  • throughUnalias [Command shortcut]You can delete the command alias

Command execution sequence

Match the commands in the following order

  • Absolute path or relative path
  • The alias
  • bashInternal command
  • In accordance with the$PATHEnvironment variables define directory lookup order for command lookup

Command shortcut keys

The command meaning
ctrl+c Forcibly terminates the current command
ctrl+l Clear the screen
ctrl+a The cursor moves to the top of the command line
ctrl+e Cursor moves to end of command line
ctrl+u Delete from current cursor position to start of line (excluding current cursor position)

The history command

  • Hisotry [option] [History command save file]

  • options

    • -cClear History command
    • -wWrite cached history command to history command save file –~/.bash_history
  • The number of files saved by default is 1000. You can modify HISSIZE=10000 in the vi /etc/profile file to change the default number.

call

  • Use the up and down arrows to invoke the previous history command
  • use! nRepeat the NTH history command (n indicateshistoryNumber in command)
  • use!!!!!Repeat the previous command
  • use! characterRepeat the last command that begins with the string (similarjsIn thestartsWithTo match from back to frontcharacterStart the command to execute)

Output redirection

Standard input output

equipment Device file name File descriptor type
equipment Device file name File descriptor type
The keyboard /dev/stdin 0 With the input
display /dev/stdout 1 The standard output
display /dev/stderr 2 Standard error output
type symbol role
Standard output redirection Command > file To overwrite the correct input of a command to a specified file or device
Standard output redirection Command >> file To append the correct input result of a command to a specified file or device
Correct output and error output are saved simultaneously Command > file 2>&1 Overwrite the correct output and the wrong output in the same file (Command > fileInput the command execution result to a file. Normally, the file can be written successfully. But you can’t write it if there’s an error.2Represents error output,2 > &1Error output is input to correct output, and then unified output, so that it can be successfully output to the file.
Correct output and error output are saved simultaneously Command >> file 2>&1 To append correct output and incorrect output to the same file

Input redirection

The wc command is used to specify the number of lines, words, and bytes of a file and display the statistics result.

The < command can be redirected as output.

For example, if wc < 1.txt, input 1.txt to the WC command to obtain the result.

Pipe symbol

Multiple commands are executed in sequence

Multiple command executor format meaning case
; Command 1; Command 2; Command 3; Multiple commands are executed without any connection between commands echo 1; echo 2;
&& Command 1&& Command 2&& command 3 Indicates logical and. The next command is executed only when the first command is executed correctly echo 1 && echo 2;
|| Command 1 | | command 2 | | 3 command Indicates logic or that the next command is executed if the first command fails echo 1 || echo 2;

Pipe symbol

  • Pipe symbol | a command before the output will be input to the next command

  • Command 1 | 2

The ls/etc | more # said to the output of the ls \ etc content flow to more paging view.Copy the code

The wildcard

  • Matches the file and filename
The wildcard role
? Matches any character
* Matches 0 or any character
[] Matches any character in parentheses
[-] matching[]Any character in the-Stands for scope, for example[0-9]Any character from 0 to 9 is matched
(^) Matches characters that are not in parentheses (indicating not)

Other symbols

symbol role
' ' Single quotation marks: All special characters in single quotation marks have no special meaning. Ordinary string
"" Double quotation marks: All special characters in double quotation marks have no special meaning. but$, `.\In addition, it has the meaning of call variable values, reference commands, and escapes
` ` Backquotes, enclosed in system commands
The $() Same thing as back quotes
# In shell scripts, # denotes a comment
$ Call variable
\ Translate the characters
echo '$PATH' # $PATH
echo "$PATH" # /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
Copy the code