Start with a string:
$ a="hi, this is my beautifull string"
Positional substrings:
- Substring from the 4th character on:
$ echo ${a:3} this is my beautifull string - Substring of length 4 from the 10th character:
$ echo ${a:9:4} is m
Substring modification:
- Substitute the first instance of a substring:
$ echo ${a/full/ful} hi, this is my beautiful string - Same example:
$ echo ${a/h/H} Hi, this is my beautifull string - Substitute all the instances of a substring:
$ echo ${a//hi/HI} HI, tHIs is my beautifull string
Substring removal:
- Remove the shortest match of a substring from the start:
$ echo ${a#h*i} , this is my beautifull string - Same, starting at the end
$ echo ${a%i*g} hi, this is my beautifull str - Remove the longest match of a substring from the start:
$ echo ${a##h*i} ng - Same, starting at the end
$ echo ${a%%i*g} h
These are for bash-2, they probably work on bash-3 and they seem to work on ksh (under OS X at least).
Add your comment below, or trackback from your own site.
Subscribe to these comments.
Be nice. Keep it clean. Stay on topic. No spam.
You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>