Variables in Shell

Keywords: vim shell

1. System variable

1. Common system variables

$HOME, $PWD, $SHELL, $USER, etc

2. Case practice

(1) View the value of the system variable

[zj@hadoop101 datas]$ echo $HOME

/home/zj

(2) Display all variables in the current Shell: set

[zj@hadoop101 datas]$ set

BASH=/bin/bash

BASH_ALIASES=()

BASH_ARGC=()

BASH_ARGV=()

 

2. Custom variable

1. Basic grammar

(1) Define variable: variable = value

(2) Undo variable: unset variable

(3) Declare static variable: readonly variable, note: cannot unset

2. Variable definition rules

(1) Variable names can consist of letters, numbers, and underscores, but they cannot start with numbers. It is recommended that environment variable names be capitalized.

(2) No spaces on both sides of equal sign

(3) In bash, the default type of variables is string type, which can not be directly used for numerical operation.

(4) If the value of a variable has spaces, you need to use double or single quotes.

3. Case practice

(1) Define variable A

[zj@hadoop101 datas]$ A=5

[zj@hadoop101 datas]$ echo $A

5

(2) Reassign variable A

[zj@hadoop101 datas]$ A=8

[zj@hadoop101 datas]$ echo $A

8

(3) Undo variable A

[zj@hadoop101 datas]$ unset A

[zj@hadoop101 datas]$ echo $A

(4) Declare static variable B=2, cannot unset

[zj@hadoop101 datas]$ readonly B=2

[zj@hadoop101 datas]$ echo $B

2

[atguigu@hadoop101 datas]$ B=9

-bash: B: readonly variable

(5) In bash, the default type of variables is string type, so it is impossible to perform numerical operation directly

[zj@hadoop102 ~]$ C=1+2

[zj@hadoop102 ~]$ echo $C

1+2

(6) If there is a space for the value of a variable, it needs to be enclosed in double quotation marks or single quotation marks

[zj@hadoop102 ~]$ D=I love banzhang

-bash: world: command not found

[zj@hadoop102 ~]$ D="I love banzhang"

[zj@hadoop102 ~]$ echo $A

I love banzhang

(7) It can promote variables to global environment variables, which can be used by other Shell programs

export variable name

[zj@hadoop101 datas]$ vim helloworld.sh



//Add echo $B to helloworld.sh

#!/bin/bash



echo "helloworld"

echo $B



[zj@hadoop101 datas]$ ./helloworld.sh

Helloworld

The value of the printout variable B was not found.

[zj@hadoop101 datas]$ export B

[zj@hadoop101 datas]$ ./helloworld.sh

helloworld

2

 

3. Special variables

$n

1. Basic grammar

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

2. Case practice

[zj@hadoop101 datas]$ touch parameter.sh 
[zj@hadoop101 datas]$ vim parameter.sh

#!/bin/bash
echo "$0  $1   $2"

[zj@hadoop101 datas]$ chmod 777 parameter.sh

[zj@hadoop101 datas]$ ./parameter.sh cls  xz
./parameter.sh  cls   xz

$#

1. Basic grammar

Function Description: get the number of all input parameters, commonly used in loops.

2. Case practice

(1) Get the number of input parameters

[zj@hadoop101 datas]$ vim parameter.sh

#!/bin/bash
echo "$0  $1   $2"
echo $#

[zj@hadoop101 datas]$ chmod 777 parameter.sh

[zj@hadoop101 datas]$ ./parameter.sh cls  xz
parameter.sh cls xz 
2

$* $@

1. Basic grammar

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

$@ (function description: this variable also represents all parameters in the command line, but $@ treats each parameter differently)

2. Case practice

(1) Print all parameters entered

[zj@hadoop101 datas]$ vim parameter.sh



#!/bin/bash

echo "$0  $1   $2"

echo $#

echo $*

echo $@



[zj@hadoop101 datas]$ bash parameter.sh 1 2 3

parameter.sh  1   2

3

1 2 3

1 2 3

 

 $?

1. Basic grammar

$? (function description: the return status of the last executed command. If the value of this variable is 0, the last command is executed correctly; if the value of this variable is non-zero (which number is determined by the command itself), the last command is executed incorrectly.)

2. Case practice

(1) Determine whether the helloworld.sh script executes correctly

[zj@hadoop101 datas]$ ./helloworld.sh

hello world

[zj@hadoop101 datas]$ echo $?

0

 

 

 

 

Published 51 original articles, won praise 5, visited 433
Private letter follow

Posted by derekm on Wed, 15 Jan 2020 00:00:49 -0800