Bash Scripting - How to Initialize a String Last Updated : 28 Nov, 2021 Comments Improve Suggest changes Like Article Like Report Strings are quite crucial aspects of programming and scripting. We need to store the characters in some variables. In this article, we'll see how to initialize strings in BASH. Basic String Initialization To initialize a string, you directly start with the name of the variable followed by the assignment operator(=) and the actual value of the string enclosed in single or double quotes. Code: #!/bin/usr/env bash a="Hello" echo $a Output: This simple example initializes a string and prints the value using the "$" operator. The "#!/bin/usr/env bash" is the shebang, which makes the kernel realize which interpreter to be run when executing, in our case, it's the BASH interpreter. We have assigned the string "a" a value of "Hello". Also, it must be noted that there should not be any whitespace around the assignment operator otherwise it generates errors in the script. Usage of Double and single Quotes in Strings The double quotes or single quotes might not be significant but when it comes to using variables inside the string, we make use of double quotes to expand the value of the variable. Code: #!/bin/usr/env bash name='Kevin' greet="Welcome $name, to GeeksforGeeks!" echo $greet Output: In the above example, if we don't use double quotes in the "greet" string, the value of the variable "name" would not expand and the string will be displayed as it is. As we can see the variable "name" is not expanded to the value and it is displayed as it is when we use single quotes when we have initialized the string "greet". Comment More infoAdvertise with us Next Article Bash Scripting - How to Initialize a String M meetgor Follow Improve Article Tags : Linux-Unix Bash-Script Similar Reads Bash Scripting - How to Run Bash Scripting in Terminal In this article, we are going to see how to run bash script in terminal. For example, we create a bash file set of instructions or commands ( known as bash script ), and through command, we can run this file in terminal. Let see how to run bash script in terminal. Example 1 : In this example we prin 2 min read How to Find Length of String in Bash Script? In this article, we are going to see how to get string length in Bash scripting. Here are a few ways from which we can get the length of the string in BASH: Using the # operatorUsing while loopUsing expr commandUsing awk commandUsing wc command Using these mentioned tools and commands, we will be ab 5 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 Bash Scripting - Introduction to Bash and Bash Scripting Bash is a command-line interpreter or Unix Shell and it is widely used in GNU/Linux Operating System. It is written by Brian Jhan Fox. It is used as a default login shell for most Linux distributions. Scripting is used to automate the execution of the tasks so that humans do not need to perform them 12 min read 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 Shell Script to Join a String There are different ways to concatenate strings in the shell. Some of them are listed below. String concatenation is used in shell scripting to make it much easier for the user to understand and use the program. Appending strings also allow the programmer to learn the working of concatenation on the 3 min read Bash Scripting - Case Statement A case statement in bash scripts is used when a decision has to be made against multiple choices. In other words, it is useful when an expression has the possibility to have multiple values. This methodology can be seen as a replacement for multiple if-statements in a script. Case statements have an 8 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 Bash Scripting - How to check If variable is Set In BASH, we have the ability to check if a variable is set or not. We can use a couple of arguments in the conditional statements to check whether the variable has a value. Â In this article, we will see how to check if the variable has a set/empty value using certain options like -z, -v, and -n. Usi 10 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 Like