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.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux(("`Linux`")) -.-> linux/FileandDirectoryManagementGroup(["`File and Directory Management`"]) linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux(("`Linux`")) -.-> linux/SystemInformationandMonitoringGroup(["`System Information and Monitoring`"]) shell(("`Shell`")) -.-> shell/BasicSyntaxandStructureGroup(["`Basic Syntax and Structure`"]) shell(("`Shell`")) -.-> shell/VariableHandlingGroup(["`Variable Handling`"]) shell(("`Shell`")) -.-> shell/ControlFlowGroup(["`Control Flow`"]) shell(("`Shell`")) -.-> shell/AdvancedScriptingConceptsGroup(["`Advanced Scripting Concepts`"]) shell(("`Shell`")) -.-> shell/SystemInteractionandConfigurationGroup(["`System Interaction and Configuration`"]) linux/BasicSystemCommandsGroup -.-> linux/echo("`Text Display`") linux/FileandDirectoryManagementGroup -.-> linux/cd("`Directory Changing`") linux/BasicFileOperationsGroup -.-> linux/chmod("`Permission Modifying`") linux/SystemInformationandMonitoringGroup -.-> linux/date("`Date/Time Displaying`") shell/BasicSyntaxandStructureGroup -.-> shell/comments("`Comments`") shell/BasicSyntaxandStructureGroup -.-> shell/quoting("`Quoting Mechanisms`") shell/VariableHandlingGroup -.-> shell/variables_decl("`Variable Declaration`") shell/VariableHandlingGroup -.-> shell/variables_usage("`Variable Usage`") shell/VariableHandlingGroup -.-> shell/str_manipulation("`String Manipulation`") shell/VariableHandlingGroup -.-> shell/param_expansion("`Parameter Expansion`") shell/ControlFlowGroup -.-> shell/cond_expr("`Conditional Expressions`") shell/AdvancedScriptingConceptsGroup -.-> shell/arith_ops("`Arithmetic Operations`") shell/AdvancedScriptingConceptsGroup -.-> shell/cmd_substitution("`Command Substitution`") shell/AdvancedScriptingConceptsGroup -.-> shell/subshells("`Subshells and Command Groups`") shell/SystemInteractionandConfigurationGroup -.-> shell/globbing_expansion("`Globbing and Pathname Expansion`") subgraph Lab Skills linux/echo -.-> lab-153898{{"`Basic String Operations`"}} linux/cd -.-> lab-153898{{"`Basic String Operations`"}} linux/chmod -.-> lab-153898{{"`Basic String Operations`"}} linux/date -.-> lab-153898{{"`Basic String Operations`"}} shell/comments -.-> lab-153898{{"`Basic String Operations`"}} shell/quoting -.-> lab-153898{{"`Basic String Operations`"}} shell/variables_decl -.-> lab-153898{{"`Basic String Operations`"}} shell/variables_usage -.-> lab-153898{{"`Basic String Operations`"}} shell/str_manipulation -.-> lab-153898{{"`Basic String Operations`"}} shell/param_expansion -.-> lab-153898{{"`Basic String Operations`"}} shell/cond_expr -.-> lab-153898{{"`Basic String Operations`"}} shell/arith_ops -.-> lab-153898{{"`Basic String Operations`"}} shell/cmd_substitution -.-> lab-153898{{"`Basic String Operations`"}} shell/subshells -.-> lab-153898{{"`Basic String Operations`"}} shell/globbing_expansion -.-> lab-153898{{"`Basic String Operations`"}} end

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