Basic String Operations

ShellShellBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In this lab, you will learn about the basic string operations in shell scripting. String operations are useful when manipulating and extracting data from strings. You will learn about string length, index, substring extraction, substring replacement, and more.

String Length

To get the length of a string, use the ${#STRING} syntax.

Here's an example:

STRING="this is a string"
echo ${#STRING} ## Outputs: 16

Create a new file and save it with ~/project/string-operations.sh.

cd ~/project
chmod +x string-operations.sh
./string-operations.sh
16

Index

To find the position of a character or substring in a string, use the expr index command. Here's an example:

STRING="this is a string"
SUBSTRING="hat"
expr index "$STRING" "$SUBSTRING" ## Outputs: 1 (position of the first 't' in $STRING)
1

Substring Extraction

To extract a substring from a string, use the ${STRING:$POS:$LEN} syntax. Here's an example:

STRING="this is a string"
POS=1
LEN=3
echo ${STRING:$POS:$LEN} ## Outputs: "his"

If you omit :$LEN, the substring will be extracted from $POS to the end of the string.

his

Simple Data Extraction Example

Here's an example that demonstrates data extraction using substring operations:

DATARECORD="last=Clifford,first=Johnny Boy,state=CA"
COMMA1=$(expr index "$DATARECORD" ',') ## 14 (position of first comma)
CHOP1FIELD=${DATARECORD:$COMMA1}       ## COMMA2=$(expr index "$CHOP1FIELD" ',')
LENGTH=$(expr $COMMA2 - 6 - 1)
FIRSTNAME=${CHOP1FIELD:6:$LENGTH} ## "Johnny Boy"
echo $FIRSTNAME

Execute the script:

./string-operations.sh

Substring Replacement

To replace substrings in a string, you can use various syntaxes. Here are some examples:

  • Replace the first occurrence of a substring with replacement:
STRING="to be or not to be"
echo ${STRING[@]/be/eat} ## Outputs: "to eat or not to be"
  • Replace all occurrences of a substring:
STRING="to be or not to be"
echo ${STRING[@]//be/eat} ## Outputs: "to eat or not to eat"
  • Delete all occurrences of a substring (replace with an empty string):
STRING="to be or not to be"
echo ${STRING[@]// not/} ## Outputs: "to be or to be"
  • Replace the occurrence of a substring if it's at the beginning of the string:
STRING="to be or not to be"
echo ${STRING[@]/#to be/eat now} ## Outputs: "eat now or not to be"
  • Replace the occurrence of a substring if it's at the end of the string:
STRING="to be or not to be"
echo ${STRING[@]/%be/eat} ## Outputs: "to be or not to eat"
  • Replace the occurrence of a substring with shell command output:
STRING="to be or not to be"
echo ${STRING[@]/%be/be on $(date +%Y-%m-%d)} ## Outputs: "to be or not to be on 2022-05-10"

Summary

In this lab, you learned about various string operations in shell scripting. You learned how to get the length of a string, find the index of a character or substring, extract substrings, and replace substrings. Understanding these operations will help you manipulate and extract information from strings efficiently in your shell scripts.

Other Shell Tutorials you may like