Video: Korn Shell A Little About Arrays/Lists

Video by bjamesm70

Share Button

Intro To Korn Shell – Lesson 7g – Pattern Matching *( )

Video by bjamesm70

Share Button

HOWTO: Korn Shell / BASH: How to determine if a string is numeric or not

Occasionally we run across something that should be simple. Checking whether a string is a number or not for example.

special_CHAR ‘(‘ pattern ‘)’

The special_CHAR is a prefix that changes the number of characters expected:

‘*’ for zero or more matches
‘+’ at least one match
‘@’ for exactly one match
‘?’ for zero or one matches
‘!’ for negation

The following will match one or more digits:


See POSIX Character Classes regarding the use of [:digit:]

Below is an example function to determine if a string is numeric written in Korn Shell 93 but will also work in BASH.

The criteria we’re using is based on the US standard of numbers:
and so on

Obviously we could expand this out to handle commas or other separators without too much difficulty.


function is_numeric {
    typeset TMP_STR="$1"
    typeset -i TMP_IS_NUMERIC

    if [[ "$TMP_STR" == +([[:digit:]])?(.*([[:digit:]])) ]]; then
        echo "'$TMP_STR' is numeric"
        echo "'$TMP_STR' is not numeric"

for TMP_STRING in "TEST_VAR" "22" "TRUE1" "TRUE3TRUE" "12.345" ""; do
    is_numeric "$TMP_STRING"


$ ./test_regex.ksh
'TEST_VAR' is not numeric
'22' is numeric
'TRUE1' is not numeric
'TRUE3TRUE' is not numeric
'12.345' is numeric
'' is not numeric

See Finnbarr P. Murphy’s blog for more examples of using regular expressions in Korn Shell 93

Share Button

Korn Shell 93: A better if structure with many tests

Writing korn shell scripts you will often come across if structures that look something like the following. It works well but the if structure doesn’t lend itself for quick reading.

if [[ MYVAR != "potato" ]] && [[ MYVAR != "acorn" ]] && [[ MYVAR != "pizza" ]] && [[ MYVAR != "apple" ]]; then

We can make it far more readable without losing the functionality:

if [[ MYVAR != @(potato|acorn|pizza|apple) ]]; then

The “@(potato|acorn|pizza|apple)” is effectively a short cut to a case structure. So, let’s expand that to the full case statement:

case ${MYVAR} in
        # commands go here
Share Button