Linux based shell variables

Posted by gurroa on Fri, 18 Feb 2022 06:06:47 +0100

1, shell variables and other related introduction

1. Compiled language and interpreted language
Compiled language: compiled language is written before program execution. It needs a special compilation process to compile the program into machine language. For example, the exe format package of Windows.
Interpretive language: interpretive language does not need to be compiled. It needs to be translated when running programs, such as / bin/bash in Linux shell.
2. Variables: in Bash shell, the value of each variable is a string. Whether you use quotation marks when assigning values to variables or not, the values will be stored in the form of strings. The name of a Shell variable starts with a letter or underscore, followed by any length of letters, numbers or underscores.
3. Variable assignment: the method of variable assignment is variable name = value, where "=" cannot be left blank on both sides.

2, shell variable type

1. Local variable: the variable name and value defined by the user. Also known as local variable, it is only valid in the current shell and will not inherit to the child shell.
2. Global variables: global variables are visible in the global scope. When declaring global variables, there is no need to add any modifiers. They are only valid in the current shell and sub shell.

3, shell variable usage

1. Local variable usage
① Define local variables

[root@control ~]# with_sapce="this is a test variable"
[root@control ~]# echo $with_sapce
this is a test variable


② View defined variables

[root@control ~]# set |grep with
with_sapce='this is a test variable'

③ Undefine local variables

[root@control ~]# 
[root@control ~]# unset with_sapce 
[root@control ~]# echo $with_sapce

[root@control ~]# 

2. Global variable usage
① Define global variables

[root@control ~]# export ATEST=12345
[root@control ~]# echo $ATEST
12345

② View global variables

[root@control ~]# env |grep ATEST
ATEST=12345

③ Cancel global variable

[root@control ~]# unset ATEST
[root@control ~]# echo #ATEST

[root@control ~]# 

3. Some global variables of the system

[root@control ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/nginx/sbin
[root@control ~]# echo $HISTSIZE
20000
[root@control ~]# echo $PS1
[\u@\h \W]\$
[root@control ~]# echo $HOME
/root
[root@control ~]# echo $UID
0

4, Define command aliases

1. Definition method

[root@control ~]# alias test='ls -laF'
[root@control ~]# test /data
total 1152044
drwxr-xrwx.  9 root root       4096 May  5 12:16 ./
dr-xr-xr-x. 31 root root       4096 May  6 15:20 ../
-rw-r--r--.  1 root root 1073741824 Feb 22 21:46 1G.txt
-rw-r--r--.  1 root root    1048576 Feb 22 21:45 1M.txt
drwx------.  2 root root         99 Apr 19 13:33 audit/
-rw-r--r--.  1 root root        715 Jan 30 23:42 block.txt
drwxr-xr-x.  2 root root          6 Jan 24 15:58 dir1/
drwxr-xr-x.  2 root root          6 Jan 24 15:58 dir2/
-rwSrwxr--+  1 root root          0 Jan 24 15:59 file1*
-rw-r--r--.  1 root root          0 Jan 24 15:59 file2
-rw-r--r--.  1 root root          0 Jan 24 15:59 file3
-rw-r--r--.  1 root root          0 Apr 30 13:58 file{o..20}
-rw-r--r--.  1 root root        160 Jan  4 15:41 hello.txt
drwxr-xr-x.  2 root root         21 May  5 12:17 log/
-rw-r--r--.  1 root root       3095 Apr 26 18:50 passwd
-rw-r--r--.  1 root root       3128 Apr 26 18:48 passwd.bak
-rw-r--r--.  1 root root        122 Jan 27 17:34 person.txt
drwxr-xr-x.  2 root root          6 Jan 24 16:39 redhat/
drwxr-xr-x.  2 root root       4096 Jan 24 22:29 scripts/
-rw-r--r--.  1 root root  104857801 Apr 29 01:56 test.txt
drwxr-xr-x.  2 root root       4096 May  1 19:36 tmp/
-rw-r--r--.  1 root root        403 Apr 29 18:31 weblog.txt

2. View the defined alias

[root@control ~]# alias test
alias test='ls -laF'

3. Undefined command alias

[root@control ~]# unalias test
[root@control ~]# alias test
-bash: alias: test: not found

3. Permanent alias

[root@control ~]# echo "alias test='ls -laF'" >> ~/.bashrc 
[root@control ~]# alias test
-bash: alias: test: not found
[root@control ~]# source ~/.bashrc
[root@control ~]# alias test
alias test='ls -laF'

5, System variable file definition description

/etc/profile: global environment variable
~/. bash_profile: user environment variable
/etc/bashrc: global environment variable
~/. bashrc: user environment variable

Topics: Programming Linux Operation & Maintenance shell string