Cynical news

Links
Literature
Music
Programming
Software
Programming
Bash Strings
Bash Strings
Create / delete

Create string a=""
a="abcdef"
abcdef

Initialise

Fixed contents a=""
a="abcdef"
abcdef
Repeated value 'x' x 10 a=""
a=`printf %10s|tr \ x`
xxxxxxxxxx

Count

Length of string a="abc"
i=${#a}
3
Length of string a="abc"
i=`expr length $a`
3
Size of underlying storage n/a error n/a
Number of 'X' chars in string a="abcXdefXX"
n=`echo "$a" | tr -cd "X" | wc -c | awk '{print $1}'`
3
Number of 'X', 'Y' and 'Z' chars in string a="abcXdefYZ"
n=`echo "$a" | tr -cd "XYZ" | wc -c | awk '{print $1}'`
3

Retrieve by index

Retrieve 1st char a="abc"
c=${a:0:1}
a
Retrieve 1st char a="abc"
c=`expr substr $a 1 1`
a
Retrieve last char a="abc"
c=${a:${#a}-1:1}
c
Retrieve last char a="abc"
c=`expr substr $a ${#a} 1`
c
Retrieve char 'n' a="abcdef"
n=2
c=${a:$n:1}
c
Retrieve char 'n' a="abcdef"
n=2
let n1=n+1; c=`expr substr $a $n1 1`
c

Slice

Slice from index 'm' to 'n' (inclusive) a="abcdefghi"
m=3
n=6
s=${a:$m:$n-$m+1}
defg
Slice from index 'n' (inclusive) a="abcdefghi"
n=3
s=${a:$n}
defghi
Slice up to index 'n' (inclusive) a="abcdefghi"
n=3
s=${a:0:$n+1}
abcd

Compare

Test for equality (case sensitive) a="abcdef"
b="abc"
t="0"
if [[ "$a" == "$b" ]]; then t="1"; fi
0
Test for equality (case insensitive) a="abcdef"
b="ABCdef"
t="0"
if [[ "`echo $a | tr '[A-Z]' '[a-z]'`" == "`echo $b | tr '[A-Z]' '[a-z]'`" ]] ; then t="1" ; fi
1
Test for prefix a="abcdef"
b="abc"
t="0"
if [[ `expr $a : "$b.*"` != "0" ]]; then t="1"; fi
1
Test for suffix a="abcdef"
b="def"
if [[ `expr $a : ".*$b"` != "0" ]]; then t="1"; fi
1

Search

Test char 'is in' string a="abcdefghi"
c='c'
t="0"
if [[ "`expr index $a $c`" != "0" ]]; then t="1"; fi
1
Test substring 'is in' string a="abcdefghi"
b="abc"
t="0"
if [[ "`expr index $a $b`" != "0" ]]; then t="1"; fi
1
Find index of char (forward) a="abcdefccccc"
c='c'
let i=`expr index $a $c`-1
2
Find index of char (reverse) a="abcdefccccc"
c='c'
a=`echo $a | rev`
let i=${#a}-`expr index $a $c`
10

Split

Split on char a="one,two,three"
c=','
ifs=$IFS; IFS=$c sa=($a); IFS=$ifs
one two three

Evaluate

Evaluate empty string a=""
t="0"
if [[ "$a" ]]; then t="1"; fi
0
Evaluate non-empty string a="abcdef"
t="0"
if [[ "$a" ]]; then t="1"; fi
1

Insert

Prepend char a="bcdef"
c='a'
a=$c$a
abcdef
Append char a="abc"
c='d'
a=$a$c
abcd
Insert char at index 'n' a="abc"
c='X'
n=1
a=${a:0:$n}$c${a:$n}
aXbc
Prepend string a="def"
b="abc"
a=$b$a
abcdef
Append string a="abc"
b="def"
a=$a$b
abcdef
Insert string at index 'n' a="abcghi"
b="def"
n=3
a=${a:0:$n}$b${a:$n}
abcdefghi
Concatenate 'a' and 'b' a="abc"
b="def"
s=$a$b
abcdef

Overwrite

Overwrite char at index 'n' a="abc"
n=1
c='B'
a=${a:0:$n}$c${a:$n+1}
aBc
Overwrite substring at index 'n' a="abcdefghi"
b="DEF"
n=3
a=${a:0:$n}$b${a:$n +${#b}}
abcDEFghi

Replace

Replace 1st instance of char 'X' with 'Y' a="XXXYYYZZZ"
a=${a/X/Y}
YXXYYYZZZ
Replace all instances of char 'X' with 'Y' a="XXXYYYZZZ"
a=${a//X/Y}
YYYYYYZZZ
Replace 1st instance of substring 'abc' with 'XYZ' a="abcdefabc"
a=${a/abc/XYZ}
XYZdefabc
Replace all instances of substring 'abc' with 'XYZ' a="abcdefabc"
a=${a//abc/XYZ}
XYZdefXYZ
Replace all instances of 'X','Y','Z' with 'A','B','C' respectively a="XXXYYYZZZ"
a=`echo $a | tr '[XYZ]' '[ABC]'`
AAABBBCCC

Remove

Remove 1st instance of char 'X' a="XXXYYYZZZ"
a=${a/X}
XXYYYZZZ
Remove all instances of char 'X' a="XXXYYYZZZ"
a=${a//X}
YYYZZZ
Remove 1st instance of substring 'abc' a="abcdefabc"
a=${a/abc}
defabc
Remove all instances of substring 'abc' a="abcdefabc"
a=${a//abc}
def
Remove prefix 'abc.' a="abc.abc.def"
a=${a#abc.}
abc.def
Remove suffix '.def' a="abc.def.def"
a=${a%.def}
abc.def

Remove by index

Remove 1st char a="abcdef"
a=${a#?}
bcdef
Remove last char a="abcdefghi"
a=${a%?}
abcdefgh
Remove char at index 'n' a="abcdef"
n=3
a=${a:0:$n}${a:$n+1}
abcef
Remove chars in index range 'm' to 'n' (inclusive) a="abcdef"
m=2
n=4
a=${a:0:$m}${a:$n+1}
abf
Remove chars from index 'n' onwards (inclusive) a="abcdefghi"
n=3
a=${a:0:$n}
abc
Remove chars up to index 'n' (inclusive) a="abcdefghi"
n=3
a=${a:$n+1}
efghi

Trim

Trim leading white space a=" abc def "
a=$(echo "$a" | sed 's/^[ ]*//')
abc def
Trim trailing white space a=" abc def "
a=$(echo "$a" | sed 's/[ ]*$//')
abc def
Trim leading and trailing white space a=" abc def "
a=$(echo "$a" | sed 's/^[ ]*//' | sed 's/[ ]*$//')
abc def

Sort

Sort in ascending order a="cafdeb"
a=`echo $a | sed -e 's/./&=/g' | tr '=' '\n' | sort | tr -d '\n'`
abcdef
Sort in descending order a="cafdeb"
a=`echo $a | sed -e 's/./&=/g' | tr '=' '\n' | sort -r | tr -d '\n'`
fedcba
Reverse a="abcdefghi"
a=`echo $a | rev`
ihgfedcba

Convert case

Convert to lower case a="ABCDEFGHI"
a=`echo $a | tr '[A-Z]' '[a-z]'`
abcdefghi
Convert to upper case a="abcdefghi"
a=`echo $a | tr '[a-z]' '[A-Z]'`
ABCDEFGHI
Convert first char to lower case a="ABCDEFGHI"
a="`echo ${a:0:1} | tr '[A-Z]' '[a-z'`${a:1}"
aBCDEFGHI
Convert first char to upper case a="abcdefghi"
a="`echo ${a:0:1} | tr '[a-z]' '[A-Z]'`${a:1}"
Abcdefghi

www.cynicalsoftware.com
14-11-09@15:37:24