Bash variables for Shell

variable call echo

 

Variable view set

View all variables (local variables and environment variables), if you only query environment variables, use the env command

 

variable unset

 

One: User-defined variables

 

 

Two: environment variables

 

export declares environment variables

Syntax: export variable name = variable value

You can also set a declared local variable as an environment variable

eg: name=zhangsan

export name

 

env   view environment variables

 

pstree determines the process tree

 

Common environment variables

PATH

The path where the system finds the command, and multiple paths are separated by colons ":"

PATH="$PATH":/root/sh #Method of superimposing environment variables

 

PS1  (system reserved user operating system environment variable)

Variables that define the system prompt



 

 

The default is: PS1='[\u@\h \W] ' echo PS1 to view the value of the variable

 

 

 

Three: position parameter variable

 

 

eg:sum.sh

#!/bin/bash

sum=$(( $1+$2 ));

echo "sum is: $sum"

Execute the command: sh sum.sh 10 22 Be careful not to forget the last two numeric parameters.

 

 eg: canshu.sh

#!/bin/bash

for i in $*

do

echo '$* = '+$i;

done

 

for i in $@

do

echo '$@ = '+$i;

done

 


 

 

Four: Predefined variables


 

 read receives keyboard input

Syntax: read [options] [variables]

Options:

-p "prompt information": output prompt information while waiting for read input. prompt abbr

-t seconds The number of seconds to wait for input. timeout abbreviation

-n Accepts the number of characters to wait. If this parameter is not defined, the input of this parameter will not be ended until a carriage return. nchars abbr

-s hides the entered data.

 

eg:

read.sh

 

#!/bin/bash

read -t 30 -p "please input your name " name

echo -e "\n"

echo $name

 

read -t 30 -s -p "please input your age" age

echo -e "\n"

echo $age

 

read -t 30 -n 1 -p "please input your gender[F/M]" gender

echo -e "\n"

echo $gender

 

数字运算

a=3

b=5

method1:d=$(($a + $b));   #注意:”=”左右两边不能有空格,

Method2:f=$[$a+$b];

method3:declare -i c=$a+$b;

method4:e=$(expr $a+$b);  #注意:”=”左右两边不能有空格,”+”左右两边必须要有空格

 

declare

 

 

运算符优先级



 
 变量替换


 

环境变量配置文件

 

 /etc/目录下的对所有用户生效,~/目录下的对当前用户生效

 

source 命令   

修改配置文件后强制让指定配置文件生效。

语法:source 配置文件

或者. 配置文件 

 

环境变量配置文件读取顺序图



 

/etc/profile的作用


 

/etc/bashrc的作用

 

 

~/bash_logout  注销时需要做的操作

 

~/bash_history   所有敲过的历史命令

 

shell登录信息

 

 

 

 

 

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326262229&siteId=291194637