Processing of five strings of Linux shell programming

Source: Internet
Author: User

String handling of Shell programming


In the shell, because it is a weakly typed language, all variables are strings without declaring the variable type beforehand. So the processing of strings is very important.

The following are common operations on string manipulation.


One, string slices

${string_var:offest:length}, similar to the extraction of array elements.

# Here the String_var is a variable, the offest is the start offset of the string, length is a tangent string var=dhcpd:x:177:177:dhcp server:/:/sbin/nologin[[email Protected] ~]# echo ${var:6:3}x:1

${string_var:offest}

# do not specify length, default display to the last [[email protected] ~]# echo ${var:6}x:177:177:dhcp server:/:/sbin/nologin

${string_var:-offest}

# Gets the trailing forward specified length of the character [[email protected] ~]# echo ${var: -7}nologin


Second, get the sub-string

1, ${string_var#*word}

# in a substring stored in the String_var variable, from left to right, look for the first occurrence of word, and delete the character starting at the beginning of everything in Word var=/etc/sysconfig/network-scripts/sysconfig/etc/cmd[ [Email protected] ~]# echo ${var#*etc}/sysconfig/network-scripts/sysconfig/etc/cmd

2, ${string_var#*word}

# in the string_var variable stored in the substring, from left to right, look for the first occurrence of word, delete the character beginning at this word all content [[email protected] ~]# echo ${var##*etc}/cmd## Typically, this method can be used to obtain the base name of a directory [[email protected] ~]# echo ${var##*/}cmd

3, ${variable%word*}

# in variable, on the store string, from right to left, look for the first occurrence of word, removing all the content from this word to the end of the string var=/etc/sysconfig/network-scripts/sysconfig/etc/cmd[[ Email protected] ~]# echo ${var%etc*}/etc/sysconfig/network-scripts/sysconfig/

4, ${variable%%world*}

# on the variable store string, from right to left, look for the last occurrence of word, delete all the contents of this word at the end of the string; [email protected] ~]# echo ${var%%etc*}/


Iii. Find and replace

1, ${string_var/pattern/new_string}

# in the String_var string variable, from left to right, replace the first substring matched by pattern to New_stringvar=bash:x:4403:4403::/home/bash:/bin/bash[[email Protected] ~]# echo ${var/bash/zsh/}zsh/:x:4403:4403::/home/bash:/bin/bash# Here you can use file wildcard characters like? * etc [[email protected] ~]# echo ${var/b?? h/zsh/}zsh/:x:4403:4403::/home/bash:/bin/bash# use # to anchor variable value header [[email protected] ~]# echo ${var/%b?? h/zsh/}bash:x:4403:4403::/home/bash:/bin/zsh/# use% to anchor variable value tail [[email protected] ~]# echo ${var/#b?? H/zsh/}zsh/:x:4403:4403::/home/bash:/bin/bash


2, ${string_var//pattern/new_string}

# in the String_var string variable, from left to right, replaced by pattern matched to a substring of new_string, matching to a few times replaced [[email protected] ~]# echo ${var//b?? h/zsh/}zsh/:x:4403:4403::/home/zsh/:/bin/zsh/


Iv. Find and delete

# The variables used are as follows Var=bash:x:4403:4403::/home/bash:/bin/bash

1, ${string_var/pattern}

# in the String_var string variable, from left to right, replace the substring that was first matched to the pattern to delete it [[email protected] ~]# echo ${var/b?? H/}:x:4403:4403::/home/bash:/bin/bash# can also use the # and% anchor variable values header and tail [[email protected] ~]# echo ${var/%b?? h/}bash:x:4403:4403::/home/bash:/bin/

2, ${string_var//pattern}

# in the String_var string variable, from left to right, replaced by pattern matched to a substring of new_string, matched to a few times deleted [[email protected] ~]# echo ${var//b?? h/}:x:4403:4403::/home/:/bin/


V. Uppercase and lowercase conversions

1, ${string_var^^}

var=var=bash:x:4403:4403::/home/bash:/bin/bash# convert all lowercase letters in the STRING_VAR variable to uppercase letters [[email protected] ~]# echo ${var^^} bash:x:4403:4403::/home/bash:/bin/bash# can also specify that character to replace uppercase and only support a single character [[email protected] ~]# echo ${var^^a}bash:x:4403:4403: :/home/bash:/bin/bash

2, ${string_var,,}

var1=bash:x:4403:4403::/home/bash:/bin/bash# convert all uppercase letters in the String_var variable to lowercase [[email protected] ~]# echo ${var1,,}bash: X:4403:4403::/home/bash:/bin/bash[[email protected] ~]# echo ${var1,,a}bash:x:4403:4403::/home/bash:/bin/bash


Six, variable copy operation

1, ${string_var:-value}

# If the String_var variable is not set or the value of the variable is empty, set the value of the String_var variable to value. # But this doesn't change the value of the original variable [[email protected] ~]# Name=centos[[email protected] ~]# echo ${name:-redhat}centos[[email protected] ~] # unset Name[[email protected] ~]# echo ${name:-redhat}redhat[[email protected] ~]# name=[[email protected] ~]# echo ${nam E:-redhat}redhat

${string_var-value}

# If the String_var variable is not set, the value of the String_var variable is set to value. [[email protected] ~]# Name=[[email protected] ~]# echo ${name-redhat}[[email protected] ~]# unset name[[email protected] The difference between ~]# echo ${name-redhat}redhat# ${string_var:-value} and ${string_var-value} is as follows: # when the variable value is empty, ${string_var-value} Will not be set to value, or the original null value


2, ${string_var:=value} ${string_var=value}

# ${string_var:=value}  and  ${string_var=value}  If the original is not  set  or empty #  the value of  value will be assigned to the  string_var variable # ${string_var:=value}  and The  ${string_var=value}  difference is as follows: [[email protected] ~]# name=[[email protected] ~] # echo ${name=redhat}[[email protected] ~]# echo  $name [[email protected]  ~]# name=[[email protected] ~]# echo ${name:=redhat}redhat[[email  protected] ~]# echo  $nameredhat #  When the original variable value is empty, there is a difference, the other is the same, the difference is as follows:#  if the original variable value is empty: # ${ When name=redhat}  is processed, the variable value is set to  redhat 
when the null value of the original variable is # ${name:=redhat}  processed.
# ${string_var:=value} and ${string_var:-value} represent the same meaning, the difference is as follows: [[email protected] ~]# unset name[[email protected] ~]# echo ${n Ame:-redhat}redhat[[email protected] ~]# echo $name [[email protected] ~]# echo ${name:=redhat}redhat[[email protected] ~ ]# echo $nameredhat # ${string_var:-value} does not change the value of the original variable # ${string_var:=value} changes the value of the original variable after performing the assignment operation


3, ${string_var:+value}

# If the String_var variable is set or the value of the variable is not empty, then the value of the String_var variable is setting to value. [Email protected] ~]# Name=centos[[email protected] ~]# echo ${name:+redhat}redhat

${string_var+value}

# If the String_var variable is set, the value of the String_var variable is set to value. [Email protected] ~]# Name=[[email protected] ~]# echo ${name+redhat}redhat


4, ${string_var:?err_msg} ${string_var?err_msg}

# ${string_var?err_msg}, ${string_var:?err_msg} If String_var is set, that is the value of set, otherwise print err_msg.# These two methods are used the same way most of the time, only in $s When Tring_var is declared and set to empty, there will be a difference #
[[email protected] ~]# Debug=[[email protected] ~]# echo ${debug:?err_type1}-bash:debug:err_type1[[email protected] ~]# Debug=error1[[email protected] ~]# echo ${debug:?err_type1}error1


Vii. Supplementary

1, ${#var_name}

# Get the length of the var_name variable [[email protected] ~]# name= "Boy" [email protected] ~]# echo ${#name}3

Summary: This article mainly introduces the related processing of string. This includes operations such as length, find and replace, find delete, case conversion, assignment, and so on. Of course, these operations can also be done with text processing tools like Grep,sed,awk, but these actions are better than text processing tools, such as the built-in functionality of the shell and the speed of execution.

This article is from the "Upstream Cold" blog, please be sure to keep this source http://guoting.blog.51cto.com/8886857/1534127

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.