Introduction to the basic features of the CentOS System Shell Programming Language Foundation Bash

Source: Internet
Author: User
Tags clear screen

The command history of Bash's underlying features:

Command history: The shell process saves commands that were previously executed by the user in its session
~] #history
Customizing the history function, which can be implemented by environment variables
Histsize:shell the number of commands history that the process can retain
Histfile: Persistent Save command history file, ". Bash_history"
Histfilesize: Size of the command history file

Command usage:

History [-c] [-D offset] [n]
or HISTORY-ANRW [filename]
or history-ps arg [arg ...]

Command common options:

-C: Empty command history
-D Offset: Delete the specified command history
-r: Read command history from file to History list
-W: Append commands from the history list to the history file

History + Numbers: Displays the most recent number bar command

To invoke a command in the command history list:

! + Number: Repeat the number Bar command in the history list
!! : Executes the previous command again
! String: Executes the most recent command in the command history list that starts with a string

Note: Repeated executions of commands sometimes need to depend on idempotent

Call the last parameter of the previous command:
Shortcut key: ESC,.
String:!$

Control the way the command history is logged:
Environment variable: Histcontrol
Ignoredups: Ignore duplicate commands
Ignorespace: Ignore commands that start with whitespace characters
Ignoreboth: Both of these take effect at the same time

Modify the value of the variable: name= ' value ', this modification is valid only for the current shell

Command completion for Bash's basic features:

Command completion: When the shell program receives a request from the user to execute the command, the left-most character is treated as a command after the analysis is completed

Command Lookup mechanism:
Find internal commands
According to the directory set in the PATH environment variable, the file name is searched from left to right under directory

The given beginning string, if it can uniquely identify a command program file, the direct completion;
Given a lead string, if you cannot uniquely identify a command program file and then click the TAB key once, the list is given

Path Completion:
Under the given starting path, match each file under the starting path with the beginning string under the corresponding path

Tab: If you can uniquely identify a command program file, the direct completion, if you cannot uniquely identify a command program file, and then click the TAB key once, will give the list

Command-line expansion of Bash's underlying features:

~: Automatically expands to the user's home directory, or specifies the user's home directory
{}: Can host a comma-delimited list of paths and be able to expand them to multiple paths
For example:/tmp/{a,b} is equivalent to/tmp/a/tmp/b

Execution status results of commands for Bash's underlying features

Status result of the command execution:
Bash outputs this result by a status return value
Success: 0
Failed: 1-255
After the command execution is complete, its status return value is saved in Bash's special variable "$?" Medium, ~] #echo $?

When the command is executed normally, there will also be command return values, depending on the command and its function, the results are different

The execution result of the reference command:
$ (COMMAND) orCOMMAND

References to the underlying features of bash:

Strong quote: '
Weak reference: ""
Command reference: "(~ location)

Shortcut keys for the basic features of bash:

CTRL + A: Jump to the beginning of the command line
Ctrl+e: Jump to the end of the command line
Ctrl+u: Delete all characters from the beginning of the line to the cursor position
Ctrl+k: Removes all characters from the cursor to the end of the line
Ctrl+l: Clear screen, equivalent to clear command

Globbing of Bash's underlying features (file name wildcard mechanism):

Globbing: File name wildcard mechanism (whole file name match, not part)

Match pattern: metacharacters
: matches any character of any length
PA
: All files beginning with PA
pa: File name contains PA files
*pa: Files ending with PA

? : Matches any single character
Pa?: Matches the next file that starts with PA for either P or a

[]: matches any single character within the specified range
There are several special formats:
[A-z],[a-z],[0-9],[a-z,0-9]

[^[;upper;]] : All non-capital letters

Pa[0-9][0-9],? 2[0-9]0-9

Exercise 1: Show all files or directories in the/var directory that end with an L, lowercase letter, and an arbitrary character in the middle
Ls-d/var/l? [; lower;]
Exercise 2: Display a file or directory that starts with any digit in the/etc directory and ends with a non-digit
ls-d/etc/[0-9][^0-9]
Exercise 3: Display a file or directory that starts with a non-letter, followed by a letter and any character of any length, in the/etc directory
Ls-d/etc/[^a-z][a-z]

Exercise 4: Copy all files or directories that start with M and that end with a non-number to the/tmp/magedu.com directory in the/etc directory
Cp-r/etc/m[^0-9]/tmp/magedu.com
Exercise 5: Copy the/usr/share/man directory, all files or directories that begin with man, followed by a number, to the/tmp/man directory
Cp-r/usr/share/man/man[0-9]/tmp/man
Exercise 6: Copy all files or directories that end with. conf and start with m,n,r,p in the/etc directory to the/TMP/CONF.D directory
Cp-r/ETC/[MNRP]
. conf/tmp/conf.d

Command hash of Bash features:

Cache the results of previous commands: Key-value
Key: Search key, Value: Values
?
Hash command
?? Hash: List
?? hash-d Command: Delete
?? Hash-r: Empty
Features of Bash variables:

Program: Instruction + data
directive: Provided by Program Files
Data: IO devices, files, pipelines, variables
?
Program: algorithm + data structure
?
Variable name + point to memory space
?
Variable assignment: Name=value
Variable types: Storing formats, representing data ranges, participating operations
? ? Programming Languages:
? ? ? ? ? ? Strongly typed variables
? ? ? ? ? ? Weakly typed variables:
And?????????? Bash all the variables as character types
The variables in bash do not have to be declared beforehand, equivalent to implementing both the Declaration and the assignment process
? ? ? ? ? ? ? ? ? ? ? ? ? Declaration: Declaring its type, defining its variable name
?
? ? ? ? ? ? Variable substitution: Replace the position where the variable name appears with the data in the memory space it points to
? ? ? ? ? ? Variable reference: ${var_name}, $var _name
? ? ? ? ? ? Variable name: Variable name can only contain numbers, letters, and underscores, and cannot start with a number
? ? ? ? ? ? Variable name: See the meaning of the name, the naming mechanism follows a certain rule, cannot use the reserved words of the program, such as If,else,then,while, etc.
?
? ? ? ? Bash Variable type:
??????? Local variables: Scope is only the current shell process
??????? environment variable: Scope is the current shell process and its child processes
??????? Local variables: Scope is only a code fragment (function context)
??????? positional parameter variables: parameters passed to the shell process executing the script
??????? Special variables: Shell-built variables with special functions
? ? ? ? ? ? ? ? ? ? ? $?: Used to perform the execution status result of the previous command
?
? ? ? ? ? ? ? ? Local variables:
? ? ? ? ? ? ? ? ? ? Variable assignment: Name=value
? ? ? ? ? ? ? ? ? ? Variable reference: ${var_name}, $var _name
? ? ? ? ? ? ? ? ? ? ? ? ?“” : The variable name is replaced with its value
? ? ? ? ? ? ? ? ? ? ? ? ?‘’ : variable name is not replaced with its value
? ? ? ? ? ? ? ? ? ? View variables: Set
? ? ? ? ? ? ? ? ? ? Undo Variable: unset name
? ? ? ? ? ? ? ? ? ? ? ? ? ? Note: Here the non-variable reference
?
? ? ? ? ? ? ? Environment variables:
? ? ? ? ? ? ? ? ? Variable assignment:
? ? ? ? ? ? ? ? ? ? ? (1) Export Name=value
? ? ? ? ? ? ? ? ? ? ? (2) Name=value
??????????????? exportname
? ? ? ? ? ? ? ? ? ? ? (3) Declare-x Name=value
? ? ? ? ? ? ? ? ? ? ? (4) Name=value
? ? ? ? ? ? ? ? ? ? ? ? ? Declare-x Name
??????????? variable reference: ${name}, $name
?
? ? ? ? ? ? ? Note: Bash has many environment variables (usually full uppercase characters) embedded in the work environment (PATH,HISTFILE,HISTSIZE,HISTFILESIZE,UID) that are set for bash
?
????????????????????????
????????? Undo Variable: unset name
?
? ? ? ? ? Read-only variables:
?????? (1) Declare-r name
?????? (2) readonly name
?
? ? ? ? ? ? ? ? Note: read-only variables cannot be re-assigned and do not support undo, inventory time is the life cycle of the current shell process, terminated with Shell process termination
?
?
Bash features multi-command execution:

~] #COMMAND1; COMMAND2; COMMAND3, .....
?
Logical operation:
? ? ? Number of operations: true (true,yes,on,1)
? ? ? ? ? ? ? ? ? ? ? Fake (false,no,off,0)
?
?????? With:
???????? 1 && 1 = 1
???????? 1 && 0 = 0
???????? 0 && 1 = 0
???????? 0 && 0 = 0
????? or:
? ? ? ? ? ? ? 1 | | 0 =1
? ? ? ? ? ? ? 1 | | 1 =1
? ? ? ? ? ? ? 0 | | 1 =1
? ? ? ? ? ? ? 0 | | 0 =0
????? Non:
? ? ? ? ? ? ? ! 1 = 0
? ? ? ? ? ? ? ! 0 = 1
?
?? Short Circuit rule:
????? ~] #COMMAND1 && COMMAND2
? ? ? ? ? ? ? ? COMMAND1 is "false", then COMMAND2 no longer executes
? ? ? ? ? ? ? ? Otherwise, COMMAND1 is true, then COMMAND2 must perform
?
? ? ? ? ? ? Example: [[email protected] ~]# touch/tmp/test.etc && ls/etc >/tmp/test.etc
? ? ? ? ? ? ? ? ? [Email protected] ~]# cat/tmp/test.etc
?
????? ~] #COMMAND1 | | COMMAND2
? ? ? ? ? ? ? ? COMMAND1 is true, COMMAND2 no longer executes
? ? ? ? ? ? ? ? Otherwise, COMMAND1 is "false", then COMMAND2 must perform
?
??????. example: [[email protected] ~]# ID User1 | | Useradd user1
????????? Id:user1: No such user
? ? ? ? ? ? ? ? ? [[email protected] ~]# ID user1
? ? ? ? ? ? ? ? ? uid=2003 (user1) gid=2003 (user1) group =2003 (user1)

Introduction to the basic features of the CentOS System Shell Programming Language Foundation Bash

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.