Shell Scripting - Here Strings
Last Updated :
01 Dec, 2022
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 string and a heredoc are extremely similar, but the former is a lot more simplified version of the latter.
Because of this, a delimiter token is not required in this text. Whenever we need a quick technique to transform some strings into a command, it is typically preferred. It is also known as an ordinary string which is used for the purpose of input redirection, It's not a special kind of string. The execution contains only a few steps.
Basic Syntax:
To construct a here-string, we use the “<<<” operator to redirect a string into a command. Concretely, the syntax is:
$ command <<< $DATA
What it essentially does is expand the variable DATA and redirect the output string to the COMMAND.
Usage of Here Strings
For input redirection from text or a variable, the here-string is utilized. On the same line, input is mentioned in single quotations (").
Example 1: Basic Usage
#!/bin/bash
wc -w <<< 'Hello GeeksforGeeks\n'
In this example, the string It is "Hello GeeksforGeeks\n" is called the here-string.
Output:
2
There are 2 words, so the output is been retrieved as 2.
Example 2: String with more words
#!/bin/bash
wc -w <<< 'Welcome to GFG a helping platform for CS Students'
In this example, the string "Welcome to GFG a helping platform for CS Students" is called the here-string.
Output:
9
There are 9 words, so the output is been retrieved as 9.
Example 3: Here String with Parameter
if grep -q "txt" <<< "$VAR"
then
echo "$VAR contains the substring sequence \"txt\""
fi
In the above example, the here-string is present with a parameter $VAR which is working as a parameter.
Example 4: Escape Special Characters in Here String
word=$'HELLO\ngfg'
echo "$WORD"
It's a kind of special character which is sometimes used in the here-string
The above example is here-string with Escape Special Characters and it's Escaping some characters.
Similar Reads
String Operators | Shell Script Pre-Requisite: Conditional Statement in Shell Script There are many operators in Shell Script some of them are discussed based on string. Equal operator (=): This operator is used to check whether two strings are equal. Syntax:Operands1 = Operand2Example:Â php #!/bin/sh str1="GeeksforGeeks"; str2="ge
2 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 - 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
How to Use Heredoc in Shell Scripting The Heredoc or Here Document is an input literal used by many programming and scripting languages. The Heredoc was originally used in UNIX Shells and is in fashion till date and, is supported by all popular Linux shells such as zsh, bash, tsh, etc. The symbol for a Heredoc is '<<' two left che
6 min read
Shell Scripting - Substitution There are certain expressions that convey special meanings. In other words, they are not what they look like. A shell carries out substitution whenever it encounters such expressions. Hence, substitution is defined as a mechanism carried out by a shell in which it substitutes the value of an express
3 min read
String Manipulation in Shell Scripting String Manipulation is defined as performing several operations on a string resulting change in its contents. In Shell Scripting, this can be done in two ways: pure bash string manipulation, and string manipulation via external commands. Basics of pure bash string manipulation: 1. Assigning content
4 min read