Linux_ Terminal and Shell

1. What is a terminal

  • In short, it is something that can accept input and display (output) results. Terminal is the most important man-machine interface of computer system.
  • Corresponding program file of desktop terminal (pseudo terminal) of CentOS / usr / bin / Gnome terminal

2. console and Terminal

  • In the early days, the physical I / O device directly connected to the computer was called the console
  • The input and output devices remotely connected to the computer through the network cable are called terminals

3. Virtual terminal (TTY: teletypewriter)

  • In the Linux operating system, multiple users can log in to the system for operation, or the same user can open multiple windows to perform multiple tasks. The Linux operating system sets up multiple virtual terminal devices, corresponding to / dev/tty1~/dev/tty6 respectively.
  • By default, only 6 of them are enabled for users in CentOS, namely tty1~tty6, which can be switched through the shortcut key: Ctrl + Alt + < F1 ~ F6 >.

4. What is a shell (command line)

  • Simply put, a shell is a shell program that receives commands entered by users and then submits them to the Linux kernel for processing.
  • When the terminal is opened, the shell program will be started automatically. There are many kinds of shell programs, and Linux mostly makes bash(GNU) by default
    Bourne again shell), which is a superset of Bourne shell(bsh), that is, an extension.
  • bash corresponding program file: / usr/bin/bash

5. Reasons for using shell

1. High efficiency
2. Stability
3. Development saves worry and money
4. It is indispensable in software testing, deployment and operation and maintenance
5. It is indispensable in software testing, deployment and operation and maintenance

6. command prompt

Generally speaking, after opening the terminal window, the character displayed in front of the cursor is the so-called command prompt. The command prompt of root user is' # ', and that of other users is' $'.

7. Execute orders

Start to input the command at the cursor behind the command prompt. After completing the command input, press enter to start executing the command.

8. Basic format of command

command [option] [target]
#Command: command name
#Options: command options
#target: operable objects such as files, devices and users

9. Options

Different options can be used to instruct commands to complete specific operations. Generally, options are divided into short format (-) and long format (–). Short format options are usually represented by a single character, and multiple short format options can be used together.

# Example:
ls -l -d /home/zc
# Equivalent to
ls -l --directory /home/zc
# Equivalent to
ls -ld /home/zc

10. Standard input (stdin)

The simple understanding is the keyboard. In Linux, the standard input is connected to the device file / dev/stdin. The standard input file descriptor is 0.

11. Standard output (stdout)

The simple understanding is the screen. In Linux, the standard output is connected to the device file / dev/stdout. The file descriptor for standard output is 1.

12. Standard error output (stderr)

The standard error output is the output location when the command executes an error. It is also the screen by default and is connected to the device file / dev/stderr. The file descriptor for standard error output is 2.

12. I / O redirection

Redirection is to change the original input or output position to another place.

13. Output redirection (> or > >)

#The content originally output to the screen is redirected and overwritten to the file file
command 1> file
#The content originally output to the screen is redirected and appended to the file file
command 1>> file

#File descriptor 1 can be omitted
command > file
command >> file

13. Error output redirection

#When the command is wrong, the error message is output to the screen
#Output the error to the black hole device, that is, discard all error messages
command 2> /dev/null

14. Standard input redirection

#Note that not all commands can receive data from standard input
#Redirect the file to the standard input of the command
command 0< file

#File descriptor 0 can be omitted
command < file

15. Pipeline (|)

Pipes are used to connect the standard output of one command to the standard input of another command. What is actually done is that the output of the previous command is redirected, and the standard input of the latter command is redirected.

# Basic syntax:
command1 | command2 | ...

16. Command multiline input

By default, when you enter a command, the shell will execute the command automatically after you press enter. If the command to be input is too long and you want to input in multiple lines, you can't press enter directly. You need to press a backslash '' first, and then press enter. The special meaning of the sample enter will be escaped to the ordinary line feed meaning.

17. Order completion

When entering a command on the command line, you can complete the command, file name, directory name, etc. by pressing tab. If you press the tab key twice in a row, all possible complements will be output according to the currently entered characters.

18. Historical orders

bash has the function of recording commands executed in the past. You can view all commands entered by the current user through the history command.

[zc@localhost /]$ history
	1 df
	2 man fdisk
	3 fdisk /dev/sda
	4 sudo fdisk /dev/sda
	5 man gdisk
	...

Use exclamation marks! Add the historical command number to execute the historical command again

[zc@localhost /]$ !1
df
Filesystem	 1K-blocks	 Used Available Use% Mounted on
devtmpfs		 885628 	0 	885628 	  0%  /dev
tmpfs 			 916220	    0   916220    0%  /dev/shm
...

You can also press the ↑ direction key in the terminal window to view the previous historical commands in turn. When you turn to the command to be re executed, press the enter key.

19. Command line help

19.1 -- help option of command

If you only want to view the usage and common options of the command, you can use the -- help option. Most Linux commands provide this option.

ls --help
[zc@localhost /]$ ls --help
Usage: ls [OPTION]... [FILE]...
List information about the FILEs (the current directory by default).
Sort entries alphabetically if none of -cftuvSUX nor --sort is specified.

Mandatory arguments to long options are mandatory for short options too.
-a, --all do not ignore entries starting with .
-A, --almost-all do not list implied . and ..
	--author with -l, print the author of each file
-b, --escape print C-style escapes for nongraphic characters
--block-size=SIZE with -l, scale sizes by SIZE when printing them;	
...

19.2 man page

The full name of man page is Manual page, which is the help document for commands and functions in linux /unix environment.

# man page use the man command to view
# Command format:
man [option] [Command name]

19.3 contents of the manual

  • name: function brief description
  • SYNOPSIS: Syntax
  • DESCRIPTION: precautions
  • OPTIONS: description of OPTIONS
  • SEE ALSO: relevant contents

Section 19.4 man

chapterexplain
man1Provides a description of commands used by ordinary users
man2Description of system call and kernel function
man3Description of subroutines and library functions
man4/Device file description in dev directory
man5/Format description of configuration file in etc directory
man6Game manual
man7Protocol conversion manual
man8System management manuals available to root
man9Linux system routine manual

19.5 see which sections of the command help

whereis

# The where is command displays the path of the command, its source code, and all help files
# Command format:
whereis Command name
# Example:
whereis read
read: /usr/bin/read
/usr/share/man/man1/read.1.gz
/usr/share/man/man1p/read.1p.gz
/usr/share/man/man2/read.2.gz
/usr/share/man/man3p/read.3p.gz

19.6 whatis

# The whatis command is used to view the database. The database needs to be created by the root user through the mandb command in advance
# Command format:
whatis Command name
# Example:
mandb
whatis read
read (1) - bash built-in commands, see bash(1)
read (1p) - read a line from standard input
read (2) - read from a file descriptor
read (3p) - read from a file

19.7 man page operation command

Shortcut keyfunction
Space barTurn down one page
ctrl+fTurn down one page
ctrl+bTurn up one page
GGo to page 1
gTo the last page
/stringSearch down the string
?stringSearch up for string
nSearch next
NSearch previous
qsign out

20. info page

It is a proprietary help system for Linux system and has stronger interactivity.

# The info page can be viewed using the info command
# Command format:
info [option] [Command name]

20.1 info page operation command

Shortcut keyfunction
Space barTurn down one page
tabMove between nodes, and nodes are displayed with asterisks
enterWhen the cursor is on the node, it will enter the node
bMove to the first node of the current page
eMove to the last node of the current page
nGo to the next node
pGo to the previous node
uReturn to the previous level
s stringKeyword search
?List of commands
qsign out

Keywords: Linux

Added by Janco on Sat, 18 Dec 2021 09:47:16 +0200