Posts

Showing posts from March, 2015

Bash String Manipulation

In bash shell, when you use a dollar sign followed by a variable name, shell expands the variable with its value. This feature of shell is called parameter expansion. But parameter expansion has numerous other forms which allow you to expand a parameter and modify the value or substitute other values in the expansion process. In this article, let us review how to use the parameter expansion concept for string manipulation operations. This article is part of the on-going bash tutorial series. Refer to our earlier article on bash { } expansion . 1. Identify String Length inside Bash Shell Script ${#string} The above format is used to get the length of the given bash variable. $ cat len.sh #! /bin/bash var="Welcome to the geekstuff" echo ${#var} $ ./len.sh 24 To understand more about bash variables, read 6 Practical Bash Global and Local Variable Examples . 2. Extract a Substring from a Variable inside Bash Shell Script Bash provides a way to extract a