1. Starts with the fourth string, intercepts the string to the end of the string
[Email protected] ~]# myname= "Hello myworld" [[email protected] ~]# Substr=${myname:3}[[email protected] ~]# echo $substr Lo MyWorld
2. Starting with the 6th string, intercept 8 character-length strings
[Email protected] ~]# filename= "/root/test.txt" [[email protected] ~]# Substr=${filename:6:8}[[email protected] ~]# echo $substrtest. txt
3. Take a partial position variable
#! /bin/bashecho $0echo ${@:1} starts with the first positional variable and goes to all positional variables to execute the result: [[email protected] ~]#./1.SH 77 88 99
4. Calculating string Lengths
[Email protected] ~]# filename= "/root/test.txt" [[email protected] ~]# echo ${#filename}14
By the front of the string, delete the matching person, delete the shortest
[Email protected] ~]# filename= "/etc/sysconfig/network-scripts/ifcfg-eth0" [[email protected] ~]# r=${filename#/*/}[ [Email protected] ~]# echo $rsysconfig/network-scripts/ifcfg-eth0
2. Delete the match from the front of the string, delete the longest
[Email protected] ~]# filename= "/etc/sysconfig/network-scripts/ifcfg-eth0" [[email protected] ~]# r=${filename##/*/} [Email protected] ~]# echo $rifcfg-eth0
3. Delete the match from the back of the string, delete the shortest
[Email protected] ~]# filename= "/etc/sysconfig/network-scripts/ifcfg-eth0" [[email protected] ~]# r=${filename%/*}[[ Email protected] ~]# echo $r/etc/sysconfig/network-scripts
4. After the string, delete the match, delete the longest
[Email protected] ~]# url= "www.baidu.com" [[email protected] ~]# R=${url%%.*}[[email protected] ~]# echo $rwww
Syntax: ${variable/style/replacement string}
Replace only the first conforming string
[Email protected] ~]# act= "Root:x:0:0:root:/root:/bin/bash" [[email protected] ~]# R=${act/:/#}[[email protected] ~]# Echo $rroot #x:0:0:root:/root:/bin/bash
2. Replace all matching strings
[Email protected] ~]# act= "Root:x:0:0:root:/root:/bin/bash" [[email protected] ~]# R=${act//:/#}[[email protected] ~]# Echo $rroot #x#0#0#root#/root#/bin/bash
Syntax: ${variable/style}
Delete only the first matching string
[Email protected] ~]# act= "Root:x:0:0:root:/root:/bin/bash" [[email protected] ~]# R=${act/:/}[[email protected] ~]# echo $rrootx: 0:0:root:/root:/bin/bash
2. Delete all strings that match
[Email protected] ~]# act= "Root:x:0:0:root:/root:/bin/bash" [[email protected] ~]# R=${act//:/}[[email protected] ~]# Echo $rrootx 00root/root/bin/bash
Take a list of variable names
[Email protected] ~]# filename= "Ifcfg-eth0" [[email protected] ~]# dir= "/etc/sysconfig/network-scripts/" [[Email Protected] ~]# dir_file= "$dir/$filename" [[email protected] ~]# echo ${[email protected]}dir dir_file
2. Take an array index list
[[email protected] ~]# ar= (a b c xy z)
[[email protected] ~]# r=${!ar[@]}
[Email protected] ~]# echo $r
0 1 2) 3 4
Using compound commands (arithmetic operations)
Use bash keywords [[]] to make a formula: [[judging]]
[[Email protected] ~]# if [[str > XYZ]];then> echo ' string str larger "> else> echo" string str relatively small "> fi string str is smaller
3. Using built-in commands: test-judged
This article is from the "Linux Revolution" blog, so be sure to keep this source http://kaibinyuan.blog.51cto.com/7304008/1619672
Shell theory Study (iv)