1. What is bash?
Bash is a Unix shell written for the GNU program and is the default shell used by many Linux platforms.
The shell is a command interpreter, an insulating layer between the operating system kernel and the user. At the same time, the shell is an explanatory or scripting language. It builds applications, also known as glue language, by "gluing" system calls, public programs, tools, and compiled binaries.
2. Why learn bash?
For anyone who wants to be properly proficient in some system management, mastering shell scripts is the most basic.
3. How the Bash script works:
sh script. SH # Use Bash to execute the bash script. SH chmod u+rx script. SH # Executes the command, which will be executed using the shell specified in the first line of the script, and if the specified shell does not exist, the system default shell will be used to execute the $ . /script. SH
4. Empty the log file under/var/log:
#!/bin/bash# Initializes a variable log_dir=/var/logcd $LOG _dircat /dev/null > Messagescat /dev/null > wtmpecho"Logs cleaned up . " Exit
5. Special characters in Bash
1). # comments
Beginning with "#" (except "#!") Outside) is a comment. "#!", which specifies the interpreter for the current script (using an absolute path)
#!/bin/echo"The# here dose not begin a comment. " Echo ' The # here dose does not begin a comment. ' Echo echoecho ${path#*:} echo2# 101011 ) # System conversion (using binary notation), not a comment
2).; Command delimiter
Use ";" You can write multiple commands on the same line
Echo Echo World
3).;; Case Statement Option termination tag
Use ";;" You can terminate the case option
#!/bin/bash varname=case"$varname"in [a echo"Alphabet"
;; [0-9echo"number"
Esac
4).. The point number is equivalent to the source command
. Script. SH source script. SH
5). "Double quotation marks
Treats escape characters as normal characters, allowing shell variables, shell commands to be replaced
6). ' Single quotation mark
Escape characters are normal characters, shell variables, shell commands are not allowed to be swapped
7)./Slash
File name path delimiter, division operator
8). \ backslash
Special meaning of canceling escape characters
9). ' Anti-colon
Command substitution
10).: Colon
I. Empty command, same as the shell built-in command true
#!/bin/bash while : # while true do echo endless loop " done
#!/bin/bash Condition=5if condition>0 then : Else echo"$condition"fi
Ii. variable expansion/string substitution
file # files "filecat /dev/null file function The same # However, this does not result in a new process because": "is a built-in command
11).? Test operator
Equivalent to ternary operators in C language
#!/bin/Basha=ten(t=a<2? 8:9 ))echo $tEcho $ ((a<2? ) 8:9))
12). $ variable Substitution
13). $ () command replacement
14). () parentheses
I. Command group
The list of commands in parentheses will be executed as a child shell (the variables in the child shell are not visible in the parent shell):
#!/bin/123(a=321;) echo $a # 123
Ii. initializing an array
#!/bin/Basharray= (12345)Echo ${array}
15). {} curly Braces
I. File name extension
MV file {,. bak}
Note: In curly braces, white space is not allowed unless the whitespace is referenced or escaped!
II. code block
Equivalent to an anonymous function, but a variable declared inside, after which the code is still visible
#!/bin/Basha=123{A=321;} echo $a 321
16). [] Brackets
I. Condition test
[is part of the shell built-in test command, not a link to external commands in/usr/bin/test
#!/bin/Basha=5iftenthen echo $a Else Echo Ten fi
Note: [[]] double brackets can also be used for conditional testing:
2 1 Echo " false " 12echo"true"
II. Array elements
#!/bin/Basharray= (12345) array[3]= Echo ${array[3]}
). <, > Angle brackets
I. redirect
> Standard output redirection, &> standard output, standard error output redirection, >&2 standard output redirection to standard error output, >> append
18). | Vertical pipe
Cat file TR ' A- z ' ' A- z '
19).-Dash
I. Options, prefixes
-eq,-le,-ge, ...
II. For redirected stdin, stdout
#!/bin/Bashbackupfile=backup-$ (Date+%m-%d-%Y) # Embed the time in the backup file. Archive=${1:-$BACKUPFILE}# If you do not specify a file name for the backup file on the command line, #+ It will be used by default"backup-mm-dd-yyyy.tar.gz".TarCVF-'Find. -mtime-1-type f-print ' > $archive.Targzip$archive.TarEcho "Directory $PWD backed up in archive file \ "$archive. tar.gz\"."Exit0
20). ~ Wave number
I. Household directory
6. Variable substitution
Echo $HOME Echo " $HOME " Echo ' $HOME '
Note: If there is no quotation marks, the extra whitespace in the output will be deleted automatically!
7. Variable assignment =
Note: There can be no white space before and after the assignment operation!
8. Special variables
I. Local variables: Visible only in code blocks or functions
II. environment variables: variables defined in the shell
Iii. position parameters: $ $, $ $, ... $9, ${10}, ${11},...
$*, [email protected]
Shell introduction and Getting Started