Basic syntax and variables of shell programming


I. basic format of shell script writing

Take the simplesthello wordGive an example

echo 'hello world'
  • . / bin / bash: tell the computer to use the bash interpreter to execute the code
  • Echo: printing

II. Running shell script

Method 1(recommended)

chmod 777

			#Give file permission


			#Running files

Method 2


Three. Notes

Single-Line Comments
			The content of the content

			multiline comment

IV. defining variables

Basic grammar

1. Define variable: variable name = variable value
			2. Undo variable: unset variable name
			3. Declare static variable: readonly variable name ා static variable cannot be unset

			Return command execution result to variable
			1. A = ` LS - La ` back quote
			2. A = $(LS - LA) is equivalent to backquotes

			System variable:

V. setting environment variables

Basic grammar

Export variable name = variable value output shell variable to environment variable
			Source profile name makes the modified configuration information take effect immediately
			Echo $variable value view the value of environment variable

			Give an example
			export a = 111
			echo $a

Vi. position parameter variable

Mainly pass parameters to shell script

$n (function description: n is a number, $0 represents the command itself, $1 - $9 represents the first to ninth parameters, more than ten parameters, more than ten parameters need to be enclosed in braces, such as )

			$* (function description: this variable represents all parameters in the command line, and $* takes all parameters as a whole)

			[email protected] (function description: this variable also represents all parameters in the command line, but [email protected] treats each parameter differently)

$(function description: this variable represents the number of all parameters in the command line)

Give an example

Content in script

			echo $2   
			#Show the second parameter
			echo $*   
			#This whole
			echo [email protected]   
			#Only in this separate for loop can cash be withdrawn
			echo $#   
#Number of parameters passed


[[email protected] ~]# sh 1 2 3 line 1: .#!/bin/bash: No such file or directory
			1 2 3
			1 2 3

7. Predefined variables

It is a variable that has been defined in advance by the shell designer and can be used directly in the shell script

$$(function description: process number (PID) of the current process)
			$! (function description: process number (PID) of the last process running in the background)
$? (function description: the return status of the last executed command. If the value of this variable is 0, it indicates that the last command executed correctly; if the value of this variable is non-zero (which number is determined by the command itself), it indicates that the last command failed to execute.)

Basic operation of variables

Operation expression

1. $((expression))  
			#$((1 + 1)) no spaces
			2. $[expression]   
			#$[1 + 1] no spaces
			The above two operation symbols are common, do not \ comment out

			This is special
			3. ` expr m + n ', ` expr 1 + 1' operators must have spaces between them 
			Jia Jia
			To eliminate
			Remove the surplus
			\(  \)