Environment variables
Some commands
Various environment variables often appear in shell scripts, so to write a script, you must first understand the environment variables. Common commands for handling environment variables are listed below
- Set is used to display local variables
- env to display environment variables
- Export to display and set environment variables
- Source followed by a file, you can directly execute the script command in the file and update
Common environment variables
Equivalent to the C language, the main function of the argv array, which is represented here in $num, where Num refers to a number. See the program example below
# Foot.sh#!/bin/shecho "#0" echo "#1" echo "#2 $" echo "#3 $" echo "#4 $4" echo "#5 $" echo "#10 ${10}" # Note the curly braces here
List some common variables that can be viewed directly through ECHO
PWD current path, bash's pwd command is the output of the path oldpwd the previous directory Pathshell the command path home user's home directory Shell current shelluser username UID User ID can refer to/etc/ PASSWDPPID Create the process number of the current process, which is the parent process number PS1 prompt variable
Reference
General strings can be used without quotes, with quotation marks (single or double quotes, to block special characters, such as asterisks)
"1 is $" |
Double quotes |
A string that references a backslash (\) In addition to the dollar sign ($) backslash (\) can output single quotation marks |
' 1 is $ ' |
Single quotation marks |
The strings inside the single quotation mark are output as is, including double quotes, but cannot output single quotes |
Vara= ' pwd ' |
Anti-Quote |
The contents of the inverted quotation mark are interpreted as shell commands. |
Note: ${pwd} and ' pwd ' mean the same, the returned result is a string, and the newline character is deleted
Comparison
Place the expression in brackets, such as ["$num 1"-eq 20] and return 0 if the condition satisfies.
Comparison operators
-eq |
Euqal |
== |
-ge |
Greater or equal |
>= |
-gt |
Greater |
> |
-le |
Less or equal |
<= |
-lt |
Less than |
< |
-ne |
Not equal |
!= |
File operators
-D File |
Directory |
is a directory |
-E File |
Exist |
Whether there is |
-F File |
File |
is a normal file |
-R File |
Readable |
Whether it is readable |
-S file |
|
Whether the file length is not zero |
-W File |
Writable |
Whether it can be written |
-X File |
Executive |
is executable |
-L File |
Linkable |
Whether it is a symbolic link |
logical operators
!expression |
Logical Non- |
Expression1-a expression2 |
Logic and |
Expression1-o expression2 |
Logical OR OR |
Getting Started with Linux shell script programming (i)