User Tools

Site Tools


blog:bash_string_functions

This is an old revision of the document!


bash string functions

len() in bash

$ var='Hello, World!'
$ echo "${#var}"
13

left() in bash

$ var='Hello, World!'
#${string:position:length}
$ echo "${var:0:5}"
Hello

right() in bash

$ var='Hello, World!'
#${string:position:length}
$ echo "${var:7:${#var}}"
World!
#or a litte more dynamic.. (the 6 most right chars)
echo "${var:$((${#var}-6)):${#var}}"
World!

mid() in bash

$ var='Hello, World!'
#${string:position:length}
$ echo "${var:4:4}"
o, W

string replace first in bash (substitute)

$ var='Hello, World!'
#${string/substring/replacement}
$ echo "${var/o/a}"
Hella, World!

string replace all in bash (substitute)

$ var='Hello, World!'
#${string//substring/replacement}
$ echo "${var//o/a}"
Hella, Warld!

Reference: http://www.netmess.org/tag/bash/

~~LINKBACK~~

Discussion

Enter your comment. Wiki syntax is allowed:
  ____    __   _____   ___      __
 / __ \  / /  / ___/  / _ ) __ / /
/ /_/ / / /__/ /__   / _  |/ // / 
\____/ /____/\___/  /____/ \___/
 
blog/bash_string_functions.1398325461.txt.gz · Last modified: 2014-04-24 09:44 by brb