shell-Script-string slices

Source: Internet
Author: User
Tags lowercase
string Slices${#var}: Returns the length of the string variable var
[Root@centos6 ~]# alpha= ' echo {A. Z} |tr-d ""  \ Create a variable to assign 26 letters to it, and no spaces
[Root@centos6 ~]# echo $alpha
abcdefghijklmnopqrstuvwxyz
[ ROOT@CENTOS6 ~]# echo ${#alpha}  \ \ See how many characters of a variable are
26
${var:offset}: Returns the string variable var starting with the character in the offset character end-of-file (excluding the offset characters), and to the last part, the value of offset is between 0 and ${#var}-1 (after bash4.2, negative values are allowed)
[Root@centos6 ~]# Echo ${alpha:3}//Skip the top 3 display after all
defghijklmnopqrstuvwxyz
${var:offset:number}: Returns the part of the string variable var starting with the character in the offset character end-of-file (excluding the offset characters), the length of the number
[Root@centos6 ~]# echo ${alpha:3:4}   \ skip 3 4
DEFG
${var:-length}: Take the rightmost few characters of a string
Note: A colon must have a blank character after it
[Root@centos6 ~]# Echo ${alpha:-3}
xyz
${var:offset:-length}: Skips the offset character from the left, always to the right until the rightmost length character
[Root@centos6 ~]# Echo ${alpha:3:-4}   //CENTOS6 does not support this notation
-bash: -4:substring expression < 0
[Root@centos7 ~]# Echo ${alpha:3:-4}  //7 is OK, take the first 3 to the last 4th between the
DEFGHIJKLMNOPQRSTUV
${var:-length:-offset}: First from the rightmost left to the length of the character start, to the right to the distance between the rightmost offset character content
Note: The space before the-length
[Root@centos7 ~]# echo ${alpha: -3:-2}  \ \ Take the penultimate 3 but the last 2 not
x
[Root@centos7 ~]# echo ${alpha: -5:-2}  \ \ The number on the front must be bigger than the number behind it
vwx
String HandlingSUBSTRING based on pattern
${var#*word}: Where word can be specified as any character function: From left to right, find the first occurrence of word in the string stored by the var variable, delete the beginning of the string to the first occurrence of all characters between word characters
Bash
[Root@centos7 ~]# Echo ${alpha#*v}
Wxyz
[Root@centos7 ~]# Echo ${alpha#*w}
Xyz
[Root@centos7 ~]# line= ' getent passwd root '
[Root@centos7 ~]# Echo $line
Root:x:0:0:root:/root:/bin/bash
[Root@centos7 ~]# Echo ${line#*root}
: X:0:0:root:/root:/bin/bash
${var##*word}: Ditto, greedy mode, and the difference is that all content between the beginning of the string and the last character specified by work is deleted
Bash
[Root@centos7 ~]# Echo ${line##*root}
:/bin/bash

An example column
File= "Var/log/messages"
${file#*/}: Log/messages
${file##*/}: Messages
${var%word*}: Where word can be any of the specified characters;
Function: From right to left, find the string in which the var variable is stored, the first occurrence of word, delete the last character of the string left to the first occurrence of all characters between word characters
Bash
[Root@centos7 ~]# Echo ${line%root*}
root:x:0:0:root:/

${var%%word*}: Ditto, except to remove the rightmost character of a string to the left until the last occurrence of all characters between word characters

[Root@centos7 ~]# Echo ${line%%root*}

An example column

url=http://www.magedu.com:80
${url##*: {
${url%%:*} http
Find Replacement${VAR/PATTERN/SUBSTR}: Finds the string in the string represented by Var that was first matched to by pattern, replaced by substr
[Root@centos7 ~]# Echo ${line/root/admin}
Admin:x:0:0:root:/root:/bin/bash
${VAR//PATTERN/SUBSTR}: Finds all strings that can be matched by pattern in the string represented by Var and replaces it with SUBSTR
[Root@centos7 ~]# Echo ${line//root/admin}
Admin:x:0:0:admin:/admin:/bin/bash
${var/#pattern/substr}: Find a string in the string represented by Var that matches the beginning of the line by pattern, replacing it with a substr
[Root@centos7 ~]# echo ${line/#root/admin}
Admin:x:0:0:root:/root:/bin/bash
${VAR/%PATTERN/SUBSTR}: Finds a string in the string represented by Var that matches the end of the line by pattern, replacing it with substr
[Root@centos7 ~]# Echo ${line/%bash/admin}
root:x:0:0:root:/root:/bin/admin
Find and delete${var/pattern}: Deletes the string in the string represented by Var for the first time matched by pattern
[Root@centos7 ~]# Echo ${line/root}
: X:0:0:root:/root:/bin/bash
[Root@centos7 ~]# Echo ${line/root/}   // Find substitution can also be found with empty instead of
: X:0:0:root:/root:/bin/bash
${var//pattern}: Deletes all strings matched by pattern in the string represented by Var
[Root@centos7 ~]# Echo ${line//root}
: X:0:0::/:/bin/bash
${var/#pattern}: Deletes all strings in the string represented by Var that match the line at the beginning of the line
[Root@centos7 ~]# echo ${line/#root}
: X:0:0:root:/root:/bin/bash
${var/%pattern}: Delete the string in the string represented by Var to match the line at the end of
[Root@centos7 ~]# echo ${line/#root}
: X:0:0:root:/root:/bin/bash
character case Conversion${var^^}: Converts all lowercase letters in VAR to uppercase
[Root@centos7 ~]# Echo ${line^^}
root:x:0:0:root:/root:/bin/bash
[Root@centos7 ~]# Echo ${line}   // But the variables are unchanged, but the display is changed
Root:x:0:0:root:/root:/bin/bash
${var,,}: Converts all uppercase letters in VAR to lowercase variable Assignmentvar is a variable, and STR is also a variable, and expr is an expression that can be a string, meaning that when STR is not defined or defined as null or defined, the effect on the VAR variable
[Root@centos7 ~]# var=${line-"haha"}  ///When line has a value Var is the value of line
[Root@centos7 ~]# echo $var
root:x:0:0: Root:/root:/bin/bash
[Root@centos7 ~]# line= "[Root@centos7 ~]#] var=${line-" haha "
}  // When line's value is null, the value of Var is line value
[Root@centos7 ~]# echo $var         

[Root@centos7 ~]# unset line
[Root@centos7 ~]# var= ${line-"haha"}   //When line is undefined the value of var is haha
[Root@centos7 ~]# echo $var         
haha
Advanced Variable Usage-there are type variables


Shell variables are generally untyped, but the bash shell provides declare and typeset (will be eliminated) two commands to specify the type of variable, two commands are equivalent bash does not support floating-point numbers is the decimal point

Declare [option] variable name
-R declaring or displaying read-only variables
-I defines a variable as an integral number
-a variable is defined as an array
-A to define a variable as an associative array
-F displays all defined function names and their contents
-F displays only the names of all the functions that have been defined
-X declaration or display of environment variables and functions
-l declares variable to lowercase letter declare-l var=uuper

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.