Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Shell Scripting
  • Kali Linux
  • Ubuntu
  • Red Hat
  • CentOS
  • Docker in Linux
  • Kubernetes in Linux
  • Linux interview question
  • Python
  • R
  • Java
  • C
  • C++
  • JavaScript
  • DSA
Open In App
Next Article:
Batch Script - Empty String
Next article icon

Batch Script - Strings

Last Updated : 01 May, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

A Bash script is a plain text file. This file contains different commands for step-by-step execution. These commands can be written directly into the command line but from a re-usability perceptive it is useful to store all of the inter-related commands for a specific task in a single file. We can use that file for executing the set of commands one or more times as per our requirements. Here in this article, we are going to discuss the use of Strings within Bash Scripting. In computer programming, a string is traditionally a sequence of characters, either as a literal constant or as some kind of variable.

The simplest example of the use of Strings in Bash scripting can be given as -

name="Satyajit Ghosh"  echo $name

Output:

Satyajit Ghosh

The above example shows a string-type variable name that prints something when called. So, the basic syntax for using Strings within a Bash Script will be -

Basic Syntax:

VariableName='value'  echo $VariableName

Now, there are multiple ways present in Bash Scripting by which we can manipulate Strings. Let's discuss them.

String Length

If we want to get or print the length of a String, we can use the following syntax for the same -

Basic Syntax:

variableName=value  echo ${#variablename}

Below is an example of the same -

Example:

name="Satyajit Ghosh"  echo The size of the String : ${#name}

Output:

The size of the String : 14

Here the size of the string is displayed on the output. Below is the terminal shell depiction after executing the script -

 

Substring Extraction

We can extract a substring or part of a String. We can provide only starting positions, both starting and ending positions, or can use right-ended indexing. The basic syntax for the same will be -

Basic Syntax:

${string:startindex:endindex} #both starting and ending position

${string: right-ended-index} #using right-ended indexing

${string:startindex} #only starting position

Below is an example of the same -

Example:

name="Satyajit Ghosh"

# using starting and ending position

echo The First Name is : ${name:0:8}

# using right-ended indexing

echo The Last Name is  : ${name: -5} #added-space required

# using only starting position

echo The Last Name is  : ${name:8}

Output:

The First Name is : Satyajit  The Last Name is : Ghosh  The Last Name is : Ghosh

Below is the terminal shell depiction after executing the script -

 

Substring Removal

It deletes the shortest or longest match of a substring from the front of a string. The basic syntax for the same will be -

Basic Syntax:

# Deletes shortest match of   # $substring from front of $string.  ${string#substring}     # Deletes longest match of   # $substring from front of $string.  ${string##substring} 

Below is an example of the same -

Example:

name="GeeksforGeeks"  echo "Name is : ${name#G*k}"  echo "Name is : ${name##G*k}"

Output:

Name is : sforGeeks  Name is : s

In the above example, the shortest match between G and k is 'Geek' and the longest match is 'GeeksforGeek', so the matched portion is removed and we have the rest of the letters present in the output. Below is the terminal shell depiction after executing the script -

 

Substring Replacement

Substring Replacement means replacing either the first match or all matches of the substring with the replacement string. The basic syntax for the same will be -

Basic Syntax:

${string/substring/replacement} #Replace first match of $substring with $replacement. 

${string//substring/replacement} #Replace all matches of $substring with $replacement.

Below is an example of the same -

Example:

name="HeeksforHeeks"  echo "Name is : ${name/Heek/Geek}"  echo "Name is : ${name//Heek/Geek}"

Output:

Name is : GeeksforHeeks  Name is : GeeksforGeeks

In the above example, the first one only replaces the first occurrence of Heek with Geek but the next statement changes all the occurrences of Heek with Geek. Below is the terminal shell depiction after executing the script -

 

String Concatenation

In bash scripting, we can concatenate two or more strings together. The basic syntax for the same will be -

Basic Syntax:

string=${string1}${string2}${string3}  or  string=$string1$string2$string3  or  string="$string1""$string2""$string3"

Below is an example of the same -

Example:

s1="Satyajit"  s2=" "  s3="Ghosh"    #String concatenation  name=$s1$s2$s3    echo $name

Output:

Satyajit Ghosh

In the above example, we have concatenated first name, last name, and a space in between. Then displayed it. Below is the terminal shell depiction after executing the script -

 

Next Article
Batch Script - Empty String
author
satyajit1910
Improve
Article Tags :
  • Linux-Unix
  • Batch-script

Similar Reads

  • Batch Script - Mid String
    In this article , we are going to learn how to use the concept of Mid String using Batch Script. Using the concept of 'Mid String' we are extracting out a sub string between two indices of any given string. Batch Script :@echo off set str=GeeksforGeeks echo %str% set str=%str:~5,-5% echo %str% pause
    2 min read
  • Batch Script - Left String
    In this article, we are going to study Left string in Batch Script. In Batch Script, the Left string is used to extract the characters from the beginning of the string by giving position 0 and a length using:~ while expanding a variable content. Example 1: Batch script set str=GeeksForGeeks echo.%st
    1 min read
  • Batch Script - Right String
    In this article, we are going to learn how to use the concept of Right String using Batch Script. Right String uses the concept of negative indexing. We have to extract the characters using :~ followed by the negative index of the character from which we want to print a substring from the main strin
    2 min read
  • Batch Script - Empty String
    In this article, we are going to create an Empty String using Batch Script. Batch Script :@echo off set str1= set str2=Hello ::using if statement we will check that str1 is empty string or not. if [%str1%]==[] echo "str1 is an empty string" if [%str2%]==[] echo "str2 is an empty string" pauseExplana
    1 min read
  • Batch Script - toInt
    In the Batch script, every variable is considered as a string and optionally integers. We need numbers all the time especially when it comes to performing system operations. We might extract numbers from a string variable but performing operations on them are not the same and require some additional
    3 min read
  • Batch Script - Create String
    In this article, we are going to learn how to create a String using Batch Script. Batch Script :@echo off set str=Geeks for Geeks echo %str% pauseBy using ' set ' we are getting input of any string.Ex: set str=input stringIn the next line, we are using ' echo %str% ' for printing our input string.Th
    1 min read
  • Batch Script - String length
    In this article , we are going to learn how to find length of any String using Batch Script. Batch Script :@echo off set str=Geeks For Geeks call :strLen str strlen echo String is %strlen% characters long pause exit /b :strLen setlocal enabledelayedexpansion :strLen_Loop if not "!%1:~%len%!"=="" set
    2 min read
  • Bash Scripting - String
    Bash String is a data type similar to integer or boolean. It is generally used to represent text. It is a string of characters that may also contain numbers enclosed within double or single quotes. Example: "geeksforgeeks", "Geeks for Geeks" or "23690" are strings Creating a String A basic declarati
    2 min read
  • Batch Script - Replace a String
    In this article, we are going to Replace a substring with any given string. Batch Script :@echo off set str=GFG is the platform for geeks. echo %str% set str=%str:the=best% echo %str% pause In the above example, we are going to replace 'the' by substring 'best' using %str:the=best% statement. Explan
    1 min read
  • Batch Script - String Concatenation
    String Concatenation is combining two or more strings to create a new string. For instance, let a string "Good" and take another string "Morning" now by string concatenation i.e. "Good" + "Morning" and we got new string i.e. "Good Morning". This is string concatenation. Let see some examples of stri
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences