View system variables:
1.env command
[root@localhost ~]# env
2.set command
[root@localhost ~]# set
*set can display user-defined variables
Custom variables:
1. Define variables:
[root@localhost ~]# a=test [root@localhost ~]# echo $a test
2. Naming rules for variables: can include upper and lower case letters, numbers, underscores (cannot start with numbers)
[root@localhost ~]# a=1 [root@localhost ~]# echo $a 1 [root@localhost ~]# a_1=2 [root@localhost ~]# echo $a_1 2 [root@localhost ~]# a1=3 [root@localhost ~]# echo $a1 3 [root@localhost ~]# 1a=4 -bash: 1a=4: Command not found
3. Add single quotes when variable values contain special characters ($/ \ #spaces, and so on):
[root@localhost ~]# a=abc [root@localhost ~]# echo $a abc [root@localhost ~]# a='a b c' [root@localhost ~]# echo $a a b c [root@localhost ~]# a=a b c -bash: b: Command not found
4. Accumulation of variables: When a variable value contains a variable name, double quotation marks are required to read the value of the variable. Single quotation mark variable names are recognized as strings
[root@localhost ~]# a=1 [root@localhost ~]# b=2 [root@localhost ~]# echo $a$b 12 [root@localhost ~]# c='$a$b' [root@localhost ~]# echo $c $a$b [root@localhost ~]# c="$a$b" [root@localhost ~]# echo $c 12
5. Global variables: Variables defined in the current shell (terminal) only work in the current shell (terminal). Global variables can be used to make variables work in the child shells of the current shell, but global variables defined in the child shell no longer work in the parent shell (global variables can only work in the current shell and the child shells of the current shell)Active in child shells)
Define global variable command:export
[root@localhost ~]# a=test [root@localhost ~]# echo $a test [root@localhost ~]# bash [root@localhost ~]# echo $a [root@localhost ~]# exit exit [root@localhost ~]# export a=test [root@localhost ~]# bash [root@localhost ~]# echo $a test
6. Delete variable: unset variable name
[root@localhost ~]# a=1 [root@localhost ~]# echo $a 1 [root@localhost ~]# unset a
Environment variables:
1. Variable Profile:
a. System level: /etc/profile, /etc/bashrc
b. User level: ~/.bash_profile, \~/.bashrc, \~/.bash_history, \~/.bash_logout
* System-level profiles are usually loaded at login, and user-level profiles are only valid for a single user
2.PS1 variable: Represents the content at the forefront of each line of command ([root@localhost ~]#)
[root@localhost ~]# echo $PS1 [\u@\h \W]\$
*u for user, h for hostname, W for current directory
Display absolute path after changing uppercase W to lowercase w:
[root@localhost ~]#cd /etc/sysconfig/ [root@localhost sysconfig]#PS1='[\u@\h \w]\$' [root@localhost /etc/sysconfig]#
3.PS2 variable: (used in another mode, such as after logging in to mysql)
[root@localhost ~]#echo $PS2 >
cut split command:
-d parameter: specify the split symbol, -f parameter: specify the number of segments, -c parameter: specify the number of characters
[root@localhost ~]#cat 1.txt root:x:0:0:root:/root:/bin/bash [root@localhost ~]#cat 1.txt |cut -d ":" -f 1 root [root@localhost ~]#cat 1.txt |cut -d ":" -f 1,2 root:x [root@localhost ~]#cat 1.txt |cut -d ":" -f 1-5 root:x:0:0:root [root@localhost ~]#cat 1.txt |cut -c 4 t
sort command: sort the contents of each line from smallest to largest ASCII codes
[root@localhost ~]# [root@localhost ~]#cat 1.txt abc aa_1 a abbop #test <zxcv 404 2018 5 [root@localhost ~]#sort 1.txt 2018 404 5 a aa_1 abbop abc #test <zxcv
-n parameter: sort numbers from smallest to largest (lines starting with letters and special symbols default to 0)
[root@localhost ~]#cat 1.txt abc aa_1 a abbop #test <zxcv 404 2018 5 [root@localhost ~]#sort -n 1.txt a aa_1 abbop abc #test <zxcv 5 404 2018
-r parameter: reverse order
[root@localhost ~]#sort -n 1.txt a aa_1 abbop abc #test <zxcv 5 404 2018 [root@localhost ~]#sort -nr 1.txt 2018 404 5 <zxcv #test abc abbop aa_1 a
wc statistics command:
-l parameter: counting rows
[root@localhost ~]#cat test.txt abc aa_1 a abbop #test <zxcv 404 2018 5 [root@localhost ~]#wc -l test.txt 9 test.txt
-m parameter: counts the number of characters
[root@localhost ~]#cat a.txt 2019 0917 [root@localhost ~]#wc -m a.txt 10 a.txt
*cat view file content shows only 8 characters, but wc-m shows 10 because there are hidden line breaks'$'
[root@localhost ~]#cat -A a.txt 2019$ 0917$
-w parameter: count of words (separated by spaces)
[root@localhost ~]#cat a.txt 2019 0917 hello,world test [root@localhost ~]#wc -w a.txt 4 a.txt
uniq Remove Command:
Uniq can only be weighted between adjacent rows, so uniq is generally used with sort, sorting before weighting:
[root@localhost ~]#cat a.txt 2019 hello world test test 1 1 2019 [root@localhost ~]#uniq a.txt 2019 hello world test 1 2019 #The line was not duplicated
Used in conjunction with the sort sort command:
[root@localhost ~]#cat a.txt 2019 hello world test test 1 1 2019 [root@localhost ~]#sort a.txt | uniq 1 2019 hello world test
-c parameter: counting the number of de-duplicates
[root@localhost ~]#sort a.txt | uniq -c 2 1 2 2019 1 hello world 2 test
Tee redirection command: (similar to > redirection, except that the tee command prints the redirected content when redirected)
[root@localhost ~]#sort a.txt | uniq -c 2 1 2 2019 1 hello world 2 test [root@localhost ~]#sort a.txt |uniq -c |tee b.txt 2 1 2 2019 1 hello world 2 test [root@localhost ~]#cat b.txt 2 1 2 2019 1 hello world 2 test
-a parameter: orientation of appending
[root@localhost ~]#cat b.txt 2 1 2 2019 1 hello world 2 test [root@localhost ~]#sort a.txt |uniq -c |tee -a b.txt 2 1 2 2019 1 hello world 2 test [root@localhost ~]#cat b.txt 2 1 2 2019 1 hello world 2 test 2 1 2 2019 1 hello world 2 test
tr replacement command: (can replace single, multiple, and all characters)
[root@localhost ~]#echo "hello world" hello world [root@localhost ~]#echo "hello world" |tr 'h' 'H' Hello world [root@localhost ~]#echo "hello world" |tr '[hw]' '[HW]' Hello World [root@localhost ~]#echo "hello world" |tr '[a-z]' '[A-Z]' HELLO WORLD [root@localhost ~]#echo "hello world" |tr '[a-z]' '0' 00000 00000
split cut command: (usually used to cut large log files)
-b parameter: specify the cut size (bytes by default if no unit is specified)
[root@localhost ~]#find /etc/ -type f -exec cat {} > log.txt \; [root@localhost ~]#ls -lh //Total usage 27M -rw-r--r--. 1 root root 27M 9 January 1722:44 log.txt [root@localhost ~]#split -b 10M log.txt [root@localhost ~]#du -sh * 27M log.txt 10M xaa 10M xab 6.1M xac
The file prefix can be specified while cutting:
[root@localhost ~]#split -b 10M log.txt testlog. [root@localhost ~]#ls log.txt testlog.aa testlog.ab testlog.ac
-l parameter: cut by line number
[root@localhost ~]#split -l 60000 log.txt [root@localhost ~]#wc -l * 170640 log.txt 60000 xaa 60000 xab 50640 xac 341280 Total dosage