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 - Strings
Next article icon

Bash Scripting – String

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

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 declaration and assignment of string

str1="GeeksforGeeks"  str2=geeksforgeeks

The quotes can be omitted, but there shouldn’t be any space before and after the equals operator

Example

#!/bin/bash    #Initializing the strings     string1="GeekForGeeks"  string2='Geeks for Geeks'  string3=2345656778    #Printing the strings    echo $string1  echo $string2  echo $string3

Output

 

Reading the string from the user

The input can be read from the user using the read command.

Example

#!/bin/bash    echo What is your name  read name  echo "Hello there, $name"

Output

 

Concatenation of Strings

Strings in bash can be easily concatenated by listing the strings in order.

Example

#!/bin/bash  s1="Geeks"  s2="for"  s3="Geeks"  s4=${s1}${s2}${s3};  echo ${s4};

Output

 

Length of the String

The length of a given string can be accessed using the # operator by placing it inside the parameter expansion (curly braces) before the variable name.

Example

#!/bin/bash  string="geeksforgeeks"  echo "The length of the string is : ${#string}"

Output

 

String Replacement

Parts of an existing string can be replaced using  /  inside the parameter expansion. The syntax is given below.

${<variable_name>/<string_to_replace>/<new_string>}

This will replace the specified string with the new string once.

Example

string="Hello, Geeks!"  echo "String before replacement ${string}"  echo "String after replacement ${string/Geeks/World}"

Output

 

To replace every single matched string from the existing string, two slashes are used ‘//’ after the variable name, so it becomes.

${<variable_name>//<string_to_replace>/<new_string>}

Let us replace all the instances of the word butter from the below phrase.

Betty brought a bit of butter  but the butter was bit of bitter  so she brought some better butter  to make the bitter butter better

#!/bin/bash

para=”Betty brought a bit of butter but the butter was bit of bitter so she brought some better butter to make the bitter butter better”

#This will replace every instance of butter in the phrase to BATTER

echo “${para//butter/BATTER}”

Output

 



Next Article
Batch Script - Strings
author
amruthkiran
Improve
Article Tags :
  • Linux-Unix
  • Bash-Script

Similar Reads

  • Bash Scripting - Substring
    sIn this article, we will discuss how to write a bash script to extract substring from a string. Extracting an Index-Based Substring There are various ways to obtain substring based on the index of characters in the string: Using cut commandUsing Bash substringUsing expr substr commandUsing awk comm
    4 min read
  • Bash Scripting - Split String
    In this article, we will discuss how to split strings in a bash script. Dividing a single string into multiple strings is called string splitting. Many programming languages have a built-in function to perform string splitting but there is no built-in function in bash to do so. There are various met
    4 min read
  • Batch Script - Strings
    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 u
    4 min read
  • 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
  • Shell Scripting - Here Strings
    Here String is a kind of string that is used for input redirection from text or a variable. In here string Input will be mentioned in the same line within the quotes. the string inside the quotes is known as the Here string and Its command contains "<<<" (3 less than the symbol). Here strin
    2 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
  • 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
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