The basic syntax of linux shell programming

The basic syntax of linux shell programming

  Shell is an interface for interactive operation between users and the kernel. Currently, the most popular Shell is called bash Shell. Shell is also a programming language <interpreted programming language> , that is, shell script <is programming with Linux shell commands>. A system can have multiple shells. You can use the cat/etc/shells command to view the shells installed in the system. Different shells may support different command syntax.

Basic format

  The code is written in a normal text file, usually with .sh as the suffix


#!/bin/bash ## Indicates which shell parser is used to parse and execute our script program
echo "hello world" ## Comments can also be written here
## This is a line of comment

Execute script


Or add x permission to the script and execute it directly, otherwise it can only be executed through sh

chmod 755


Basic syntax

System variable

Variables in Linux Shell are divided into "system variables" and "user-defined variables". You can view system variables through the set command.

[hadoop@centos-Reall-131 shtest]$ set
LESSOPEN='|/usr/bin/ %s'

Access system variables: $HOME, $PWD, $SHELL, $JAVA_HOME, etc.

Custom variable

  1. Grammar

  Variable = value (for example, STR=abc)

  No spaces on both sides of the equal sign

  Use variable: $arg

   There is a difference between double quotation marks and single quotation marks: double quotation marks only escape spaces in the string; single quotation marks will escape variables in the string, such as $param.

  2. Example

[hadoop@centos-Reall-131 ~]$ STR=hello world 
-bash: world: command not found
[hadoop@centos-Reall-131 ~]$ STR="hello world"
[hadoop@centos-Reall-131 ~]$ A=9
[hadoop@centos-Reall-131 ~]$ echo $A
[hadoop@centos-Reall-131 ~]$ echo $STR
hello world

What if I want to print hello worlds is greater?

Is echo $STRs is greate OK?

No, the correct way of writing is:

echo ${ STR}s is greate

[hadoop@centos-Reall-131 ~]$ echo ${STR}s is greate
hello worlds is greate
[hadoop@centos-Reall-131 ~]$ echo'${STR}s is greate'
${STR}s is greate
[hadoop@centos-Reall-131 ~]$ echo "please$STR" 
pleasehello world
[hadoop@centos-Reall-131 ~]$ echo'please$STR'

unset A Unset variable A

readonly B=2 declares the static variable B=2 and cannot be unset

export A #The    variable can be promoted to the global environment variable in the current shell process, which can be used by other subshell programs


a="a in"
echo $a


b="b in"
echo $b
echo $a

Then execute ./ and you will find that the a variable defined in the a script is not printed out in the b script.

If you want to print the variable a of the a script in b, you need to export the variable a in the a script. At this time, the a variable becomes the global variable of the bash process where the script is located, and all the child processes of the process All have access to variable a.

another way:

If you call in the script as follows

. ./     ## Note: Pay attention to the first "."


source ./ ##

Then, runs in the bash process space where is located, instead of running in a child process of the a process.


1. Invoking directly from will make execute in the "child process" space of the bash process where a is located

2. The child process space can only access the variables defined by export in the parent process

3. A shell process cannot promote variables defined by itself to the parent process space

4. When the "." sign executes the script, the script will be executed in the shell process space where the caller is located

3. Backquote assignment

A=`ls -la` ## Backquote, run the command inside, and return the result to the variable A

A=$(ls -la) ## Equivalent to backticks

4. Special variables

$? indicates the status code of the previous command exit

$$ represents the current process number

$0 represents the current script name

$n represents the input parameter at position n (n represents a number, n>=1)

$# indicates the number of parameters, often used in loops

Both $* and $@ represent the parameter list

Note: The difference between $* and $@

Both $* and $@ represent all parameters passed to the function or script

ü When not enclosed by double quotes ""——

Both $* and $@ form a parameter list in the form of $1 $2… $n

ü When they are enclosed in double quotes ""——

"$*" will take all the parameters as a whole, forming a whole string in the form of " $1 $2… $n " ;

"$@" will separate each parameter and form a parameter list in the form of "$1" "$2"… "$n"