Shell Variables - Details of linux Videos

Posted by jd307 on Tue, 04 Jun 2019 03:18:42 +0200

Basic concepts of variables

Variable classification

  • User-defined variables
  • Environmental variables: These variables mainly store data related to the operating environment of the system.
  • Location parameter variable: This variable is mainly used to transfer parameters or data to scripts. The variable name can not be customized and the function of the variable is fixed.
  • Predefined variables: variables that have been defined in Bash, variable names can not be customized, and the role of variables is fixed.

Variable Setting Rules

  • Variable names can be composed of letters, numbers, and underscores, but cannot begin with numbers.
  • In Bash, the default types of variables are string types. If you want to perform numerical operations, you must specify the variable type as numeric.
  • Variables are joined by equal signs, and no spaces are allowed on the left and right sides of the equal sign.
  • If the value of a variable has spaces, use single or double quotation marks to include
  • In the value of a variable, you can use the "" escape character.
  • If the value of the variable needs to be increased, then the variable can be superimposed. However, variables need to be enclosed in double quotes, "$variable name" or ${variable name}.
  • If you assign the result of a command to a variable as a variable value, you need to include the command using inversion marks or $().
  • Environmentally variable names are recommended in capitals for easy distinction

Example


# Variable type defaults to string
[root@localhost ~]# var1=1
[root@localhost ~]# var2=1
[root@localhost ~]# var3=$var1+$var2
[root@localhost ~]# echo $var3
1+1
[root@localhost ~]# 

# No spaces on the left and right sides of the equal sign
[root@localhost ~]# var4 = 2
-bash: var4: No command found
[root@localhost ~]# var4=2
[root@localhost ~]# 

# Assign the result of a command to a variable as the value of a variable
[root@localhost ~]# now_time=$(date)
[root@localhost ~]# echo $now_time
2017 Tuesday, 08, 2008:09:33 CST
[root@localhost ~]# old_time=`date`
[root@localhost ~]# echo $old_time
2017 Tuesday, 08, 2008:10:02 CST
[root@localhost ~]# list=`ls -al ./student.txt`
[root@localhost ~]# echo $list
-rw-r--r--. 1 root root 55 8 July 17:56 ./student.txt
[root@localhost ~]# 

User-defined variable definition

Definition of variables

[root@localhost ~]# os='centos 7'

Variable superposition


[root@localhost ~]# aa=123
[root@localhost ~]# aa="$aa"456
[root@localhost ~]# aa="$aa"789
[root@localhost ~]# echo $aa
123456789
[root@localhost ~]# 

Variable View


# View all variables defined in the system
[root@localhost ~]# set

[root@localhost ~]# set | grep "var[0-9]"
var1=1
var2=1
var3=1+1
var4=2

Variable deletion

Format: unset variable name


[root@localhost ~]# BB='Don't BB!'
[root@localhost ~]# echo $bb
//Don't BB!!
[root@localhost ~]# unset bb
[root@localhost ~]# echo $bb

[root@localhost ~]# 

environment variable

What are the environmental variables?

User-defined variables only work in the current shell, while environment variables work in the current shell and all of its sub-shells. If the environment variable is written to the corresponding configuration file, the environment variable will take effect in all shells.

Setting environment variables

# Declare variables
# export variable name = variable value

[root@localhost ~]# export env_aa1=123
[root@localhost ~]# echo $env_aa1
123

# Query environment variables
[root@localhost ~]# env


[root@localhost ~]# env | grep 'env_aa1'
env_aa1=123
[root@localhost ~]#

# Delete environment variables
[root@localhost ~]# env | grep 'env_aa1'
env_aa1=123
[root@localhost ~]# unset env_aa1
[root@localhost ~]# env | grep 'env_aa1'
[root@localhost ~]# 

How to define sub-Shell

[root@localhost ~]# export name=yh
[root@localhost ~]# age=18
[root@localhost ~]# export sex=m
[root@localhost ~]# echo $name
yh
[root@localhost ~]# echo $age
18
[root@localhost ~]# echo $sex
m
[root@localhost ~]# bash    # Enter child shell
[root@localhost ~]# echo $name # Environment variables can be invoked in sub-shell s
yh
[root@localhost ~]# echo $age

[root@localhost ~]# echo $sex
m
[root@localhost ~]# exit # Exit child shell
exit
[root@localhost ~]# 

Common System Environment Variables

  • PATH: Path of System Find Command

    [root@localhost ~]# echo $PATH
    /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
    [root@localhost ~]# PATH="$PATH":/root/sh #PATH variable superposition
    
  • PS1: Variables defining system prompts

    • d: Display date in "Sunday, Month and Sunday"
    • h: Show abbreviated hostname. For example, the default host name "localhost"
    • t: 24-hour time in the format "HH:MM:SS"
    • T: Display 12-hour time in the format "HH:MM:SS"
    • A: Display 24-hour time in "HH:MM"
    • u: Display the current user name
    • w: Displays the full name of the current directory
    • W: Displays the last directory of the current directory
    • # The number of orders executed
    • S: Prompt. If the user is root, the prompt will be displayed as "#" and the normal user prompt "$"
Example
[root@localhost sh]# echo $PS1
[\u@\h \W]\$
[root@localhost sh]# PS1='[\u@\t \W]\$'
[root@20:02:54 sh]#cd ..
[root@20:03:00 ~]#

Location parameter variable

Location parameter variable Effect
$n n is the number, $0 is the command itself, $1 - $9 is the first to ninth parameter, and more than ten parameters need braces, such as .
$* This variable represents all the parameters on the command line, $* takes all the parameters as a whole
$@ This variable also represents all the parameters on the command line, but $@ treats each parameter differently
$# This variable represents the number of parameters on the command line

Example

# $n
[root@localhost sh]# cat chanshu1.sh 
#!/bin/bash
echo $0
echo $1
echo $2
echo $3
[root@localhost sh]# ./chanshu1.sh 11 22 33 44
./chanshu1.sh
11
22
33
[root@localhost sh]# 

[root@localhost sh]# cat params.sh 
#!/bin/bash
# Use $# Number representing all parameters
echo "A total of $# parameters"
# Use $* to represent all parameters
echo "The parameters is:$*"
# Using $@ also represents all parameters
echo "The parameters is:$@"
[root@localhost sh]# params.sh 1 2 3 4 5 6 7 8 9
-bash: /root/sh/params.sh: insufficient privilege
[root@localhost sh]# chmod 755 params.sh 
[root@localhost sh]# params.sh 1 2 3 4 5 6 7 8 9
A total of 9 parameters
The parameters is:1 2 3 4 5 6 7 8 9
The parameters is:1 2 3 4 5 6 7 8 9
[root@localhost sh]# 

# The difference between $* and $@
[root@localhost sh]# cat paramsDemo.sh 
#!/bin/bash
for i in "$*"

    do
        echo "The parameters is:$i"
    done
x=1
for y in "$@"

    do
        echo "The parameter$x is:$y"
        x=$(($x+1))
    done
[root@localhost sh]# chmod 755 paramsDemo.sh 
[root@localhost sh]# paramsDemo.sh 1 2 3 4 5 6 7 8 9
The parameters is:1 2 3 4 5 6 7 8 9
The parameter1 is:1
The parameter2 is:2
The parameter3 is:3
The parameter4 is:4
The parameter5 is:5
The parameter6 is:6
The parameter7 is:7
The parameter8 is:8
The parameter9 is:9
[root@localhost sh]# 

Predefined variables

Predefined variables Effect
$? The return status of the last execution of the command. If the value of this variable is 0, it proves that the previous command is executed correctly; if the value of this variable is not 0 (the specific number is determined by the command itself), it proves that the previous command is executed incorrectly.
$$ Current process number (PID)
$! The process number (PID) of the last process running in the background

Example

[root@localhost sh]# ls
add.sh  chanshu1.sh  paramsDemo.sh  params.sh   
[root@localhost sh]# echo $?
0
[root@localhost sh]# la
-bash: la: No command found
[root@localhost sh]# echo $?
127
[root@localhost sh]# 

[root@localhost sh]# cat process.sh 
#!/bin/bash
# Output current process PID
echo "The current proccess is $$"

find /root -name params.sh &

echo "The last one Daemon proccess is $!"
[root@localhost sh]# process.sh 
The current proccess is 9244
The last one Daemon proccess is 9245
[root@localhost sh]# /root/sh/params.sh

Receive keyboard input

Command format

read [option] [variable name]

Options:
    - p "Tip Information": Output Tip Information while Waiting for read Input
    - t Description: The read command waits for the user's input all the time. Use this option to specify the waiting time
    - n Character Number: The read command accepts only the specified number of characters and executes
    - s: Hidden input data, suitable for confidential information input

Example

[root@localhost sh]# cat readDemo.sh 
#!/bin/bash
 Read-t 30-p: "Enter your name:" name
 echo: "Your name is: $name"
echo -e "\n"
Read-s-t 30-p: "Enter your age, we will keep it secret for you:" age
echo -e "\n"
echo: "Your age is: $age"
echo -e "\n"
Read-n 1-T 30-p "Enter your gender [M/F]:" gender
echo -e "\n"
echo: "Your gender is: $gender"
[root@localhost sh]# readDemo.sh 
Enter your name: yh
 Your name is yh


Enter your age and we will keep it secret for you:

Your age is: 18


Enter your gender [M/F]:m

Your gender is:m
[root@localhost sh]# 

Topics: Linux shell CentOS