Go back to Alan's home page


  1. Pattern match

    x='abc\123\'

    # matched
    if [[ $x =~ .*[0-9]+.* ]]; then ...; fi
    if [[ $x =~ .*\\[0-9]+\\ ]]; then ...; fi

    #Incorrect/not matched:
    if [[ $x =~ '.*[0-9]+.*' ]]; then ...; fi # extra single quotes
    if [[ $x =~ ".*[0-9]+.*" ]]; then ...; fi # extra double quotes
    if [[ $x =~ .*[0-9]\+.* ]]; then ...; fi # \+ instead of +
    if [[ $x =~ .*[0-9]+\ ]]; then ...; fi # \ instead of \\

    y='abc 123'

    # matched
    if [[ $y =~ [a-z]*\ [0-9]* ]]; then ...; fi

    #incorrect/not matched
    if [[ $y =~ [a-z]* [0-9]* ]]; then ...; fi # space without backslash

  2. Substring extraction

    x='abc\123\'

    # extracted
    echo `expr match "$x" '[^0-9]*\([0-9]\+\).*'`
    echo `expr match "$x" '[^0-9]*\([0-9]\+\)\\\\'`
    echo `expr match "$x" '.*\\\\\([0-9]\+\).*'`

    #Incorrect/not extracted:
    echo `expr match "$x" .*\\\\\([0-9]\+\).*` # missing single quotes
    echo `expr match "$x" ".*\\\\\([0-9]\+\).*"`# "..." instead of '...'
    echo `expr match "$x" '[^0-9]*\([0-9]+\).*'` # + instead of \+
    echo `expr match "$x" '[^0-9]*\([0-9]+\)\\'` # \\ instead of \\\\\


    y='abc 123'

    # extracted
    echo `expr match "$y" '[a-z]*\ \([0-9]*\)` # space with backslash is ok
    echo `expr match "$y" '[a-z]* \([0-9]\)'` # space without backslash also ok

  3. Read files line by line

    while read line
    do
      ... # variable 'line' stores the line
    done < "filename"

    To use the command read, the file must end with a line feed (LF, ASCII 10, c-q c-j in Emacs); otherwise the last line will not be read.

  4. Read files created from Windows
    Mostly, if not always, text files created from Windows use two characters, carriage return (CR, ASCII 13, c-q c-m in Emacs) and line feed (LF) to indicate a new line, while in Linux it's just LF. When using command read to read such files, CR will likely be the last character of a line. Strange things could happen when concatenating strings containing such control characters.
    The following is a simple example (^M is the character CR):

    x='123abc^M'
    y='456'
    echo $x$y # outputs 456abc

    See Newline (Wiki) for more information.