1 # string.bash --- bash emulation of string(3) library routines
2 # Author: Noah Friedman <friedman@prep.ai.mit.edu>
4 # Last modified: 1993-09-29
7 # Conversion to bash v2 syntax done by Chet Ramey
15 # Strcat appends the value of variable s2 to variable s1.
31 s1_val
=${!1} # indirect variable expansion
33 eval "$1"=\'"${s1_val}${s2_val}"\'
37 # Usage: strncat s1 s2 $n
39 # Line strcat, but strncat appends a maximum of n characters from the value
40 # of variable s2. It copies fewer if the value of variabl s2 is shorter
41 # than n characters. Echoes result on stdout.
60 s1_val
=${!s1} # indirect variable expansion
63 if [ ${#s2_val} -gt ${n} ]; then
64 s2_val
=${s2_val:0:$n} # substring extraction
67 eval "$s1"=\'"${s1_val}${s2_val}"\'
71 # Usage: strcmp $s1 $s2
73 # Strcmp compares its arguments and returns an integer less than, equal to,
74 # or greater than zero, depending on whether string s1 is lexicographically
75 # less than, equal to, or greater than string s2.
81 [ "$1" = "$2" ] && return 0
83 [ "${1}" '<' "${2}" ] > /dev
/null
&& return -1
89 # Usage: strncmp $s1 $s2 $n
91 # Like strcmp, but makes the comparison by examining a maximum of n
92 # characters (n less than or equal to zero yields equality).
98 if [ -z "${3}" ] ||
[ "${3}" -le "0" ]; then
102 if [ ${3} -ge ${#1} ] && [ ${3} -ge ${#2} ]; then
116 # Strlen returns the number of characters in string literal s.
122 eval echo "\${#${1}}"
126 # Usage: strspn $s1 $s2
128 # Strspn returns the length of the maximum initial segment of string s1,
129 # which consists entirely of characters from string s2.
135 # Unsetting IFS allows whitespace to be handled as normal chars.
137 local result
="${1%%[!${2}]*}"
143 # Usage: strcspn $s1 $s2
145 # Strcspn returns the length of the maximum initial segment of string s1,
146 # which consists entirely of characters not from string s2.
152 # Unsetting IFS allows whitspace to be handled as normal chars.
154 local result
="${1%%[${2}]*}"
160 # Usage: strstr s1 s2
162 # Strstr echoes a substring starting at the first occurrence of string s2 in
163 # string s1, or nothing if s2 does not occur in the string. If s2 points to
164 # a string of zero length, strstr echoes s1.
170 # if s2 points to a string of zero length, strstr echoes s1
171 [ ${#2} -eq 0 ] && { echo "$1" ; return 0; }
173 # strstr echoes nothing if s2 does not occur in s1
179 # use the pattern matching code to strip off the match and everything
183 # then strip off the first unmatched portion of the string
188 # Usage: strtok s1 s2
190 # Strtok considers the string s1 to consist of a sequence of zero or more
191 # text tokens separated by spans of one or more characters from the
192 # separator string s2. The first call (with a non-empty string s1
193 # specified) echoes a string consisting of the first token on stdout. The
194 # function keeps track of its position in the string s1 between separate
195 # calls, so that subsequent calls made with the first argument an empty
196 # string will work through the string immediately following that token. In
197 # this way subsequent calls will work through the string s1 until no tokens
198 # remain. The separator string s2 may be different from call to call.
199 # When no token remains in s1, an empty value is echoed on stdout.
208 #:docstring strtrunc:
209 # Usage: strtrunc $n $s1 {$s2} {$...}
211 # Used by many functions like strncmp to truncate arguments for comparison.
212 # Echoes the first n characters of each string s1 s2 ... on stdout.
226 # string.bash ends here