Most basic: Remember the previous:#!/bin/bash, and the file suffix name is generally. sh
VI HelloWorld. SH #!/bin/bashecho"helloworld"
Then remember to add permissions to the script, because the default is read and write permissions, default to all people add permissions:
chmod +x HelloWorld. SH
In the current directory, both methods execute the script
./helloworld. SH sh HelloWorld. SH
If not the current directory, such as the previous level of the directory, then: Remember to note the space OH:
sh scripts/helloworld. SH . /scripts/helloworld. SH
--------------------------------------Set--------------------------------------------
Gets all the current variables. For example:
grep homehome=/rootjava_home=/usr/local/jdk1. 8. 0_181
In addition: note pwd; the output value is not the same for different paths, the output value is the current path
----------------------------------------variable---------------------------------------------------
Single or double quotes just to make the space a whole
[Email protected] scripts]# param=echo paramparam[[email protected] scripts]# param02= " Xiao Feng02 ";p aram03='xaio feng03'echo $parma 02; Echo $param 03xaio feng03
The difference between single and double quotes: Here the double quotes are off, the single quotes are not, and the crisp quotes are more powerful.
Echo " $param "; Echo ' $param ' ; Xiao Feng02$param02
example, know str= "Hello World" wants to print "Hello Worlds is greater" notice there's a s. Should:
[Email protected] scripts]# str="helloworld"echo ${ Str}s is Greathelloworlds great
Enclose in curly braces
-----------------------------------Export and Shell-----------------------------------------------------------------------
A and B are in different processes, so B cannot share A's variable
[Email protected] scripts]#CatA.SH#!/bin/Basha=hahahaEcho "In a-----"$a [[email protected] scripts]#CatB.SH#!/bin/bash./A.SHEcho "In b-----"$a [[email protected] scripts]#./b.SHinchA-----hahahainchb-----
This way: No, because B is the father. A is a child, a defines export, just makes a child process useful
[Email protected] scripts]#CatA.SH#!/bin/Bashexport a=hahahaEcho "In a-----"$a [[email protected] scripts]#CatB.SH#!/bin/bash./A.SHEcho "In b-----"$a [[email protected] scripts]#./b.SHinchA-----hahahainchb-----
It should be.
[Email protected] scripts]#CatA.SH#!/bin/Bashexport a=hahahaEcho "In a-----"$a./b.SH[email protected] scripts]#CatB.SH#!/bin/BashEcho "In b-----"$a [[email protected] scripts]#./A.SHinchA-----hahahainchb-----hahaha
Conclusion:Export A # can promote the variable to the Global environment variable in the current shell process, which can be used by other child shell programs
Or: source means that b.sh is also in the process of a.sh.
[Email protected] scripts]#CatA.SH#!/bin/Basha=hahahaEcho "In a-----"$asource/home/xiaofeng/scripts/b.SH[email protected] scripts]#CatB.SH#!/bin/BashEcho "In b-----"$a [[email protected] scripts]#./A.SHinchA-----hahahainchb-----hahaha
Note:source can also use "." Replace
. /home/xiaofeng/scripts/b.SH
Assign a command value to a variable, in reverse quotation marks: '
[email protected] scripts]# lltotal A-rwxr-xr-x.1Root root theJul in -: toA.SH-rwxr-xr-x.1Root root toJul - the: ,B.SH-rwxr-xr-x.1Root root +Jul - -: -HelloWorld.SH[[email protected] scripts]# a=' ll ' [[email protected] scripts]#Echo$atotal A-rwxr-xr-x.1Root root theJul in -: toA.SH-rwxr-xr-x.1Root root toJul - the: ,B.SH-rwxr-xr-x.1Root root +Jul - -: -HelloWorld.SH[[email protected] scripts]# a=`Date' [[email protected] scripts]#Echo$aSun Jul in -: -: +Cst2018[email protected] scripts]#
or dollar brackets
[Email protected] scripts]# a=$ (dateecho: :2018
4. Special Variables
$? indicates the status code of the last command exit
$$ Indicates the current process number
$ A indicates the current script name
$n represents the n position input parameter (n for number,n>=1)
$# represents the number of parameters, often used in loops
$* and [email protected] all represent parameter lists
[Email protected] scripts]#DateSun Jul in -: -: -Cst2018[email protected] scripts]#Echo$?0[[Email protected] scripts]# AD-Bash:ad:command not found[[email protected] scripts]#Echo$?127[email protected] scripts]#true[email protected] scripts]#Echo$?0[email protected] scripts]#false[email protected] scripts]#Echo$?1
$n
cat C.shecho $1 $2 $3[[email protected] scripts]#. /C.sh AAA BBB cccaaa BBB CCC
end!
Shell BASIC Programming