libStrings
Functions to process shell strings.
Overview
Functions to process and manipulate shell strings.
Contains the following:
ksl::strlen()
ksl::strlenR()
ksl::isEmpty()
ksl::isEmptyR()
ksl::startsWith()
ksl::endsWith()
ksl::trimRight()
ksl::trimLeft()
ksl::trimWhitespace()
ksl::contains()
ksl::toLower()
ksl::toUpper()
ksl::capitalize()
ksl::isAlphNum()
ksl::isAlpha()
ksl::# isAscii()
ksl::isBlank()
ksl::isCntrl()
ksl::isDigit()
ksl::isGraph()
ksl::isLower()
ksl::isInteger()
ksl::isPrint()
ksl::isPunct()
ksl::isSpace()
ksl::isUpper()
ksl::# isWord()
ksl::isXdigit()
Index
ksl::strlen
Returns the number of characters in string.
Passes string by value. See also strlenR() next.
Example
x=$(ksl::strlen "dinosaur")
Arguments
$1 (…): the string of interest
Exit codes
0: in all cases
Output on stdout
the string length

ksl::strlenR
Returns the number of characters held in string variable.
Passes string by reference. See also strlen() previous.
Example
ANIMAL=dinosaur
x=$(ksl::strlenR ANIMAL)
Arguments
$1 (…): the variable name holding a string
Exit codes
0: in all cases
Output on stdout
the string length

ksl::isEmpty
Returns true if string is empty, otherwise false.
Passes string by value. See also isEmptyR() next.
Example
ANIMAL=dinosaur
if ksl::isEmpty $ANIMAL; then echo "yes"; fi
Arguments
$1 (…): the string of interest
Exit codes
0: if string is empty (true)
1: if string is non-empty (false)

ksl::isEmptyR
Returns true if string variable holds a non-zero string.
Passes string by reference. See also isEmpty() previous.
Example
ANIMAL=dinosaur
if ksl::isEmptyR ANIMAL; then echo "yes"; fi
Arguments
$1 (…): the variable name holding a string
Exit codes
0: if string variable is empty (true)
1: if string variable is non-empty (false)

ksl::startsWith
Returns true if $1 string starts with $2 string.
Example
ANIMAL="old dinosaur"
if ksl::startsWith $ANIMAL "old"; then echo "yes"; fi
Arguments
$1 (…): the major string to test
$2 (…): the minor string to look for
Exit codes
0: the major string starts with the minor string (true)
1: the major string does not start with the minor string (false)

ksl::endsWith
Returns true if $1 string ends with $2 string.
Example
ANIMAL="old dinosaur"
if ksl::endsWith $ANIMAL "dinosaur"; then echo "yes"; fi
Arguments
$1 (…): the major string to test
$2 (…): the minor string to look for
Exit codes
0: the major string ends with the minor string (true)
1: the major string does not end with the minor string (false)

ksl::contains
Returns true if $1 string contains the string in $2.
Example
ANIMAL="old dinosaur"
if ksl::contains $ANIMAL "dinosaur"; then echo "yes"; fi
Arguments
$1 (…): the major string to test
$2 (…): the minor string to look for
Exit codes
0: the major string contains the minor string (true)
1: the major string does not contain the minor string (false)

ksl::trimLeft
Returns a copy of $1 with the matching string in $2 removed from the front of $1.
If $2 is not given then defaults to removing whitespace. $2 argument is a prefix.
Example
ANIMAL="old dinosaur"
echo ksl::trimLeft $ANIMAL "old "
outputs: "dinosaur"
Arguments
$1 (…): the major string to operate on
$2 (…): the minor string to look for
Exit codes
0: in all cases
Output on stdout
the resulting string

ksl::trimRight
Returns a copy of $1 with the matching string in $2 removed from the end of $1.
If $2 is not given then defaults to removing whitespace. $2 argument is a prefix.
Example
ANIMAL="old dinosaur"
echo ksl::trimRight $ANIMAL "dinosaur"
outputs: "old "
Arguments
$1 (…): the major string to operate on
$2 (…): the minor string to look for
Exit codes
0: in all cases
Output on stdout
the resulting string

ksl::trimWhitespace
Returns a copy of $1 with the whitspace removed from both the start and end of $1.
Example
ANIMAL=" old dinosaur\t"
echo ksl::trimWhitespace $ANIMAL
outputs: "old dinosaur"
Arguments
$1 (…): the string to operate on
Exit codes
0: in all cases
Output on stdout
the resulting string

ksl::toLower
Returns a copy of $1 string converted to lower case.
Example
ANIMAL="Old Dinosaur"
echo ksl::toLower $ANIMAL
outputs: "old dinosaur"
Arguments
$1 (…): the string to operate on
Exit codes
0: in all cases
Output on stdout
the resulting string

ksl::toUpper
Returns a copy of $1 string converted to upper case.
Example
ANIMAL="Old Dinosaur"
echo ksl::toUpper $ANIMAL
outputs: "OLD DINOSAUR"
Arguments
$1 (…): the string to operate on
Exit codes
0: in all cases
Output on stdout
the resulting string

ksl::capitalize
Returns a copy of $1 string with first character capitalized and the rest left alone.
Example
ANIMAL="old dinosaur"
echo ksl::capitalize $ANIMAL
outputs: "Old dinosaur"
Arguments
$1 (…): the string to operate on
Exit codes
0: in all cases
Output on stdout
the resulting string

ksl::isAlphNum
Returns true if all characters in $1 are alphanumeric as defined by the POSIX standard.
Example
ANIMAL="dinosaur"
if ksl::isAlphaNum $ANIMAL; then echo "yes"; fi
Arguments
$1 (…): the string to operate on
Exit codes
0: the string contains only alpha numeric characters (true)
1: the string contains more than just alpha numeric characters (false)

ksl::isAlpha
Returns true if all characters in $1 are alpha as defined by the POSIX standard.
Example
ANIMAL="dinosaur"
if ksl::isAlpha $ANIMAL; then echo "yes"; fi
Arguments
$1 (…): the string to operate on
Exit codes
0: the string contains only alpha characters (true)
1: the string contains more than just alpha characters (false)

ksl::isAscii
Returns true if all characters in $1 are ASCII characters as defined by the POSIX standard.
ASCII is not currently working. Need to investigate.
Example
ANIMAL="dinosaur"
if ksl::isAscii $ANIMAL; then echo "yes"; fi
Arguments
$1 (…): the string to operate on
Exit codes
0: the string contains only ASCII characters (true)
1: the string contains more than just ASCII characters (false)

ksl::isBlank
Returns true if all characters in $1 are blank as defined by the POSIX standard.
Example
ANIMAL="dinosaur"
if ksl::isBlank $ANIMAL; then echo "yes"; fi
Arguments
$1 (…): the string to operate on
Exit codes
0: the string contains only blank characters (true)
1: the string contains more than just blank characters (false)

ksl::isCntrl
Returns true if all characters in $1 are control characters as defined by the POSIX standard.
Example
ANIMAL="dinosaur"
if ksl::isCntrl $ANIMAL; then echo "yes"; fi
Arguments
$1 (…): the string to operate on
Exit codes
0: the string contains only control characters (true)
1: the string contains more than just control characters (false)

ksl::isPrint
Returns true if $1 contains only characters that are printable as defined by POSIX standard.
Example
ANIMAL="dinosaur"
if ksl::isPrint $ANIMAL; then echo "yes"; fi
Arguments
$1 (…): the string to operate on
Exit codes
0: the string contains only printable characters (true)
1: the string contains more than just printable characters (false)

ksl::isGraph
Returns true if all characters in $1 are in the graph class (displayable on a screen) as defined by the POSIX standard.
Example
ANIMAL="dinosaur"
if ksl::isGraph $ANIMAL; then echo "yes"; fi
Arguments
$1 (…): the string to operate on
Exit codes
0: the string contains only digit characters (true)
1: the string contains more than just digit characters (false)

ksl::isLower
Returns true if all characters in $1 are lower case as defined by the POSIX standard.
Example
ANIMAL="dinosaur"
if ksl::isLower $ANIMAL; then echo "yes"; fi
Arguments
$1 (…): the string to operate on
Exit codes
0: the string contains only lowercase characters (true)
1: the string contains more than just lowercase characters (false)

ksl::isUpper
Returns true if all characters in $1 are upper case as defined by the POSIX standard.
Example
ANIMAL="dinosaur"
if ksl::isUpper $ANIMAL; then echo "yes"; fi
Arguments
$1 (…): the string to operate on
Exit codes
0: the string contains only upper case characters (true)
1: the string contains more than just upper case characters (false)

ksl::isPunct
Returns true if $1 contains only characters that are punctuations as defined by the POSIX standard.
Example
ANIMAL="dinosaur"
if ksl::isPunct $ANIMAL; then echo "yes"; fi
Arguments
$1 (…): the string to operate on
Exit codes
0: the string contains only punctuation characters (true)
1: the string contains more than just punctuation characters (false)

ksl::isSpace
Returns true if $1 contains only characters that are spaces as defined by the POSIX standard.
Example
ANIMAL="dinosaur"
if ksl::isSpace $ANIMAL; then echo "yes"; fi
Arguments
$1 (…): the string to operate on
Exit codes
0: the string contains only space characters (true)
1: the string contains more than just space characters (false)

ksl::isWord
Returns true if all characters in $1 are considered a word - containing only letters, digits, and the character _.
isWord is not currently working. Need to investigate.
Example
ANIMAL="dinosaur"
if ksl::isWord $ANIMAL; then echo "yes"; fi
Arguments
$1 (…): the string to operate on
Exit codes
0: the string contains a valid word (true)
1: the string contains more than a word (false)

ksl::isDigit
Returns true if all characters in $1 are digits as defined by the POSIX standard.
Note that “+” “-” “.” are not digits.
Example
ANIMAL="dinosaur"
if ksl::isDigit $ANIMAL; then echo "yes"; fi
Arguments
$1 (…): the string to operate on
Exit codes
0: the string contains only digit characters (true)
1: the string contains more than just digit characters (false)

ksl::isInteger
Returns true if $1 forms a valid integer meaning all digits with an optional preceding +/-.
Does not check for length.
Example
ANIMAL="dinosaur"
if ksl::isInteger $ANIMAL; then echo "yes"; fi
Arguments
$1 (…): the string to operate on
Exit codes
0: the string contains a valid integer (true)
1: the string contains more than just an integer (false)

ksl::isNumber
Returns true if $1 forms a valid number.
Example
ANIMAL="dinosaur"
if ksl::isNumber $ANIMAL; then echo "yes"; fi
Arguments
$1 (…): the string to operate on
Exit codes
0: the string contains a valid number (true)
1: the string contains more than just a number (false)

ksl::isFloat
Returns true if $1 forms a valid floating point number.
Example
ANIMAL="dinosaur"
if ksl::isFloat $ANIMAL; then echo "yes"; fi
Arguments
$1 (…): the string to operate on
Exit codes
0: the string contains a valid floating point number (true)
1: the string contains more than just a floating point number (false)

ksl::isXdigit
Returns true if all characters in $1 are valid hexadecimal digits.
Example
ANIMAL="dinosaur"
if ksl::isXdigit $ANIMAL; then echo "yes"; fi
Arguments
$1 (…): the string to operate on
Exit codes
0: the string contains only hexadecimal characters (true)
1: the string contains more than just hexadecimal characters (false)
