Rahul's Blog

Bash Shell Scripting: Strings

July 21, 2020

#!/bin/bash

# Strings and Parameter Expansions( ${} )

rand_str="A random string is a string"

# get string length
echo "string length: ${#rand_str}"

# get string slice starting at index (0 index)
echo "${rand_str:2}"

# get string with starting and ending index (0 index)
echo "${rand_str:2:7}"

# returns whats left after `a`
echo "${rand_str#*a }"

Written by Rahul Jain         
I write code like I make sandwich