Variables in shell scripts

Definition of variables

  • A variable is the address of an area of memory (a string representing the address of memory)
  • Significance of variables: Commands cannot operate on constantly changing targets, and using a fixed string of characters to represent an unstable target can solve this problem

2. Definition of variables in shell scripts

1. Variable Name

  • The characters a variable name can contain: letters, underscores_, number
  • Variable name definition rule: cannot start with a number
  • Suggestions: Short variable names are all capitals, long variable names are _ Distinguish subclasses

Temporarily set a1 and write script
Can't run script with sh, can run script with.
Because sh runs as a new child shell in the current shell, the value of the variable is not passed to the child shell, so the result cannot be obtained and the variable must be placed in a public area.

2. Environmental Level

Variable invalidation after environment shutdown

export a=1

When the environment is shut down, all the data in the shell's memory is recycled by the system. Opening a new shell is a new memory address and the value of a cannot be addressed.

3. User Level

Environment is still valid after shutdown, invalid after switching users

vim ~/.bash_profile

export a=1

source ~/.bash_profile

4. System Level

Not affected by environment and user

cd /etc/profile.d
vim skk.sh
export a=1

env looks at built-in variables to show environment variables that already exist in the system

5. Delete the set variable

Delete the set system, user, and environment variables in turn

rm -fr /etc/profile.d/skk.sh
vim ~/.bash_profile
unset a

3. Translation of Variables

1. Translations

\Translate a single character
""Weak references, batch translation of characters, not'\'''$'!'
''Strong Reference
$$id of the current process

2. Declaration

Variable declaration for a

a=1
echo $ab
echo ${a}b

3. Array of variables

a=(1 2 3 4 5 6)
a$[a[@]:Starting Element id:Number of elements]

echo ${a[0]}         ##The first element in the array
echo ${a[3]}	     ##The fourth element in the array
echo ${a[-1]}        ##The last element in the array
echo ${a[*]}         ##All elements in the array
echo ${a[@]}         ##All elements in the array
echo ${a[@]:0:3}     ##The first to third elements in the array
echo ${a[@]:3:3}	 ##Three elements after the third element
echo ${#a[@]}        ##Number of elements in the array

unset a[n]           ##Delete the n+1 element from the array
unset a              ##Delete the array a


4. Alias setting of commands in Linux

alias xie='vim'  ##Temporary settings
vim ~/.bashrc
alias xie='vim'  ##Effective only for the current user
vim /etc/bashrc
alias xie='vim'  ##Effective for all users of the system
vim /etc/skel/.bashrc
alias xie='vim'  ##Effective for new users
alias  ##See
unalias xie  ##Delete alias in the current environment

5. Change of user environment variables

Set the way:

export PATH=$PATH:/mnt  ##Environment Level
vim ~/.bash_profile  ##User level
vim /etc/bash_profile  ##System level

Once the script under /mnt has execute permissions, it can be executed without adding absolute paths

Passwords in scripts

1. Non-interactive mode

$0File name of shell script
$1The first string entered after the script
$2The second string of characters entered after the script
$*All characters "1 2 3" entered after the script
$@All characters "1", "2", "3" entered after the script
$#Number of strings entered after the script
vim test.sh

echo '$0' is $0
echo '$1' is $1
echo '$2' is $2
echo '$*' is $*
echo '$@' is $@
echo '$#' is $#

More parameters are detailed: https://blog.csdn.net/S_K15/article/details/121488610

2. Interactive mode

read: assign, -p: output prompt
-s: Hide input, echo'': Line break

vim test2.sh

read -p "Please input name:" -s NAME
echo ""
echo $NAME

6. Script Functions

Definition: Alias of the program
Setup: WORD represents action1, action2 in scripting

WORD()
{
	action1
	action2
}

Example: Exit script when input is exit

Write a function inside a function to make it loop

vim test3.sh

WORD(){  ##Define WORD function
  read -p "Please input word:" -s NAME
  echo ""
  echo $NAME
  [ "$NAME" == "exit" ] && {
    echo bye !!!
    exit
  } || {
  WORD
  }
}
WORD  ##Use WORD function

Tags: Linux Operation & Maintenance shell

Posted on Thu, 02 Dec 2021 12:11:28 -0500 by koopmaster