Home > Bash Scripting Tutorial > Bash Variables > Types of Variables in Bash > What is “HereDoc” Variable in Bash? [5 Practical Cases]
What is “HereDoc” Variable in Bash? [5 Practical Cases]
Being a Linux user, while working with complex or lengthy content, it’s so usual to face a situation like when you need to redirect a block of text or code stream to an interactive command. In such a case, the HereDoc variable serves as the best option by embedding the multiline strings directly within the Bash scripts. However, let’s explore this article to have a concise look at different aspects of HereDoc variables in Bash.
Table of Contents
What is a “HereDoc” in Bash?
HereDoc (Here Document) is a feature that allows you to define multiple lines of text within your code in Bash. Generally, it starts with a delimiter and ends with the same delimiter at the beginning of a line. And inside the delimiter, you can include any kind of plain text or variable reference as HereDoc content.
Here’s a basic syntax of HereDoc in Bash:
What is Bash “HereDoc” Variable?
A Bash HereDoc variable is a powerful tool that uses the HereDoc construct, captures multiple content blocks, and assigns it to a variable. This variable is useful not only for storing configuration files, complex strings, and variable references but also for containing command substitutions in case of making dynamic content.
5 Cases for Bash “HereDoc” Variable
There are various ways to use the HereDoc variable in Bash . The following section showcases 5 cases of Bash HereDoc variable:
1. Create Multiline Message Using “HereDoc” Variable in Bash
Multiline messages can be created with the HereDoc variable in bash. Here document allows you to define a block of messages within a script or command. It can include multiline text without escaping any special characters.
Now, to create multiline messages using HereDoc variables in Bash, follow the script:
In this script, name="X" assigns the value X to the variable name . Then, the line multi_msg=$(cat <<EOF … EOF) specifies a HereDoc section. The text between the delimiter <<EOF and EOF is assigned to the variable multi_msg . Next, in the line echo "$multi_msg" , the echo command prints the text of the multi_msg variable.
2. Redirect Content Using “HereDoc” Variable in Bash
You can use the HereDoc variable to define text and then redirect the content to a file. This lets you overwrite a file with the stored text of the HereDoc variable.
Let’s see a bash script of how to redirect content using the HereDoc variable in bash:
Here, the line output_file="file.txt" assigns a file file.txt to the variable output_file . Next, text=$(cat <<EOF … EOF) specifies a HereDoc block. The text between the delimiter <<EOF and EOF is assigned to the variable text . Then, the line echo "$text" > "$output_file" redirects the content of the text variable to the file assigned to the output_file variable. Finally, in echo "Text has been redirected to $output_file" , the echo command prints Text has been redirected to $output_file to confirm the redirection of the HereDoc content.
3. Bash “HereDoc” Variable as Input for “grep” Command
The following example demonstrates how you can use the HereDoc variable as input to the grep command to search for specific patterns. Check the bash script to know how to use the HereDoc variable as input of the grep command:
Here, grep_pattern="linuxsimply" assigns the pattern linuxsimply to the HereDoc variable grep_pattern . Next, content=$(cat <<EOF … EOF) specifies a HereDoc block. The text with the specified search pattern between the delimiter <<EOF and EOF is assigned to the variable content . Now, in the line echo "$content" | grep "$grep_pattern" , the text inside the content variable is piped to the grep command where the command searches for lines that match with the specified pattern in the variable grep_pattern .
4. Bash “HereDoc” Variable for Dynamic HTML Generation
The following example is a basic illustration of the HereDoc variable in case of generating a simple & dynamic HTML page along with an HTML file including dynamic data. Let’s check the bash script to learn how to use the bash HereDoc variable for Dynamic HTML generation:
Here, title="A Dynamic Website" assigns the string A Dynamic Website to the variable title and content="<h1>Welcome to LinuxSimply</h1>" assigns the string <h1>Welcome to LinuxSimply</h1> to the variable content . Then, a HereDoc variable is defined in the line html_file=$(cat <<EOF and the content including the variable’s title and content between the delimiter <<EOF and EOF are assigned to the variable html_file .
Afterward, in the line echo "$html_file" > file.html, the content of the html_file variable is redirected to the file file.html . Finally, the echo command displays A dynamic html page ‘file.html’ is created. to confirm the creation of the dynamic HTML page.
5. Bash “HereDoc” Variable for Creating a Custom Script
Here’s an example of how you can use the HereDoc variable for defining the content of your own customized script, redirect it to a file, and make it executable. Check the script for creating a custom script using the HereDoc variable:
In the line custom=$(cat <<'EOF' , a HereDoc variable is defined, and the content between the delimiter <<'EOF' and EOF are assigned to the variable custom . Here, <<'EOF' and EOF are used to prevent the variable expansion. Next, the content of the custom variable is redirected to a custom script script.sh .
After that, in the line chmod u+x script.sh, the chmod command makes the script executable allowing permission to the owner of the file. Finally, the echo command displays A custom script ‘script.sh’ is created and made executable. indicating that the custom script is successfully created and made executable.
So far, from the whole article you have got that the HereDoc variable is such a flexible tool in Bash that enables you to create dynamic files, manipulate multiline contents, etc. By leveraging this variable properly, you can handle complex text-based tasks in your scripts in a powerful and effective way.
People Also Ask
What is the syntax of the heredoc.
The syntax of HereDoc (here document) is:
How to redirect HereDoc content to a file?
To redirect HereDoc content to a file, you can use the following syntax:
The content of HereDoc will be redirected to the file.txt file.
How to prevent variable expansion within a HereDoc?
You can prevent variable expansion within a HereDoc by enclosing the delimiter in single quotes ( ‘ ’ ).
What to do if I want to maintain indentation within the HereDoc?
If you want to maintain indentation within the HereDoc, then you need to ensure the indentation of the heredoc delimiter along with the content.
Can I use a HereDoc variable to provide input to a command?
Yes , you can use a HereDoc variable when you want to append multiple lines of input to a command.
How can I remove line breaks from the HereDoc content?
Append the tr command to remove line breaks from the HereDoc content before assigning it to a variable.
Can I use command substitution within a HereDoc?
Yes , you can use command substitution within a HereDoc.
What is cat <<EOF in bash?
The cat <<EOF construct is known as a here document in bash which is used to contain a block of text within a script or command. The EOF delimiter stands for the end of file that indicates the beginning and end of a here document.
- What Are Built-in Variables in Bash [2 Cases With Examples]
- An Ultimate Guide of Using Bash Environment Variables
- The “.bashrc” Environment Variables [4 Cases]
- String Variables in Bash [Manipulation, Interpolation & Testing]
- What is Variable Array in Bash? [4 Cases]
- An Extensive Exploration of Bash Special Variables [9 Examples]
- What is Boolean Variable in Bash? [3 Cases With Examples]
- What is PS1 Variable in Bash? [3 Customization Examples]
- What is PS3 Variable in Bash? [3 Practical Examples]
<< Go Back to Types of Variables in Bash | Bash Variables | Bash Scripting Tutorial
Nadiba Rahman
Hello, This is Nadiba Rahman, currently working as a Linux Content Developer Executive at SOFTEKO. I have completed my graduation with a bachelor’s degree in Electronics & Telecommunication Engineering from Rajshahi University of Engineering & Technology (RUET).I am quite passionate about crafting. I really adore exploring and learning new things which always helps me to think transparently. And this curiosity led me to pursue knowledge about Linux. My goal is to portray Linux-based practical problems and share them with you. Read Full Bio
Leave a Comment Cancel reply
Save my name, email, and website in this browser for the next time I comment.
Get In Touch!
Legal Corner
Copyright © 2024 LinuxSimply | All Rights Reserved.
- PHOENIXNAP HOME
- Colocation Overview
- Data Center as a Service Solutions for Digital Transformation
- Hardware as a Service Flexible Hardware Leasing
- Meet-Me Room The Interconnectivity Hub
- Schedule a Tour Guided Virtual Data Center Tour
- Data Center Locations Global Data Center Footprint
- Platform Overview
- Rancher Deployment One-Click Kubernetes Deployment
- Intel Xeon E-2300 Entry-Level Servers
- 4th Gen Intel Xeon Scalable CPUs Boost Data-Intensive Workloads
- Alliances Technology Partnerships
- Object Storage S3-Compatible Storage Solution
- Dedicated Servers Overview
- FlexServers Vertical CPU Scaling
- Intel Xeon-E Servers Intel Xeon 2200 Microarchitecture
- GPU Servers Servers with NVIDIA Tesla GPUs
- Dedicated Servers vs. BMC Compare Popular Platforms
- Promotions See Available Discounts
- Buy Now See All Servers
- Managed Private Cloud (MPC) Highly Customizable Cloud
- Data Security Cloud Secure-By-Design Cloud
- Hybrid Cloud Multi-Platform Environment
- Edge Computing Globally Distributed Servers
- Object Storage S3 API Compatible Storage Service
- Bare Metal Cloud API-Driven Dedicated Servers
- Alternative Cloud Provider Overcome Public Cloud Limitations
- Backup Solutions Veeam-Powered Services
- Disaster Recovery VMware, Veeam, Zerto
- Veeam Cloud Connect Backup and Replication
- Managed Backup for Microsoft 365 Veeam-Powered Service
- Data Security Cloud Secure-by-Design Cloud
- Encryption Management Platform (EMP) Cryptographic Key Management
- Confidential Computing Data-in-Use Encryption
- Ransomware Protection Data Protection and Availability
- DDoS Protection Network Security Features
- CONTACT SUPPORT
- Network Overview Global Network Footprint
- Network Locations U.S., Europe, APAC, LATAM
- Speed Test Download Speed Test
- Blog IT Tips and Tricks
- Glossary IT Terms and Definitions
- Resource Library Knowledge Resources
- Events Let's Meet!
- Newsroom Media Library
- Developers Development Resources Portal
- APIs Access Our Public APIs
- GitHub Public Code Repositories
- Search for:
Bash HereDoc Tutorial With Examples
Home » SysAdmin » Bash HereDoc Tutorial With Examples
Introduction
A here document ( HereDoc ) is a section of code that acts as a separate file. A HereDoc is a multiline string or a file literal for sending input streams to other commands and programs.
HereDocs are especially useful when redirecting multiple commands at once, which helps make Bash scripts neater and easier to understand.
This article teaches you the basics of using HereDoc notation and some typical use cases.
Prerequisites
- Access to the command line/terminal as a sudo user.
- A text editor to write Bash scripts .
- Basic Linux commands. For a quick reference, grab our Linux commands cheat sheet .
Bash HereDoc Syntax
The syntax for writing a HereDoc is:
It consists of the following elements:
- COMMAND is optional. Works for any command that accepts redirection.
- << is the redirection operator for forwarding a HereDoc to the COMMAND .
- - is a parameter for tab suppression.
- DELIMITER in the first line defines a HereDoc delimiter token. END , EOT , and EOF are most common, but any multicharacter word that won't appear in the body works. Omit single quotes on the first line to allow command and variable expansion.
- The DELIMITER in the last line indicates the end of a HereDoc. Use the same word from the first line without the leading whitespaces.
The HereDoc itself contains any number of lines with strings , variables, commands, and other inputs.
Bash HereDoc Examples
This section showcases how to use the HereDoc notation in various situations. The most common use case is with the cat command .
Multiline String
Open the terminal and enter the following text, pressing Enter after each line:
The cat command reads the HereDoc and writes the contents to the terminal.
Variable Expansion
A HereDoc accepts the use of variables and reads them.
To see how this works, create two variables in the terminal:
Pass the HereDoc to a cat command to print the two variables along with an environment variable :
All the variables expand, and their respective values print to the terminal.
Command Expansion
HereDocs accept command substitution. Run the following code in the terminal line by line to see the results:
Encompass each command in $() to evaluate a statement and fetch the results. Omitting $() treats the text as a string.
Ignore Variable and Command Expansion
Add single or double quotes to the first delimiter to ignore variable and command expansion in a HereDoc.
For example:
Adding quotes to the delimiter treats the contents as a HereDoc literal.
Piping and Redirecting
Use piping or redirecting to forward the command results to another command. For example, create a Bash script and add the following contents to pipe a command:
Alternatively, use redirect notation to achieve the same result:
Run the Bash script to see the results.
In both cases, the output from the cat and a HereDoc command pipes (or redirects) to the base64 -d command. As a result, the script decodes the message from the HereDoc.
Write to File
HereDoc allows writing multiline documents through one command.
To create a file and save the HereDoc contents, use the following format:
If the document does not exist, the command creates it. Check the file contents to confirm:
The console shows the file contents.
Note: Learn different methods on how to write to file in Bash .
Tab Suppression
Add a dash ( - ) after redirection to suppress leading tabs. For example, create and run the following script:
Without tab suppression, the message prints to the console with indentation. Adding the dash removes the tab indent and outputs the message without the leading spaces.
Note: If the text shows up with the leading spaces, press TAB instead of copying and pasting the example code.
Inside Statements and Loops
When working with a HereDoc inside statements and loops, keep in mind the following behavior:
- Code inside statements and loops is indented. Add a dash after the redirect operator to print messages from a HereDoc without indentation.
- The ending delimiter cannot have spaces or indentations before it.
Try the following example code to see how to use a HereDoc inside an if statement :
The dash ensures the indents don't show up when the program runs. The ending delimiter is not indented, and adding spaces causes an error.
Multiline Comments
A HereDoc with the null command ( : ) creates the effect of block comments in Bash scripts.
Using HereDoc notation as a block comment is unconventional. In general, Bash does not support block commenting.
Escape Characters
To avoid character interpretation, add a backslash ( \ ) before a character:
Alternatively, avoid character interpretation completely by escaping the delimiter:
Using quotation marks on the delimiter is equivalent in this case.
Add parameters to a function by forwarding information through a HereDoc. For example, create a function to read lines and add information through the HereDoc:
The function stores the information provided by the HereDoc into variables.
Run the script to print the variable values to the terminal.
HereDoc and SSH
A HereDoc is convenient for executing multiple commands on a remote machine. Pass a HereDoc to the SSH connection to run multiple commands.
The command prints the local and remote users to the console.
HereDoc and SFTP
SFTP helps transfer data securely via the SSH protocol . Forward a HereDoc to run multiple SFTP commands automatically:
The code uploads a sample file to the remote machine.
Note: Learn how to read files line by line in Bash using here strings.
After going through the examples in this guide, you know how to use HereDoc notation in various situations. HereDoc helps pass multiple commands at once as input for different commands.
Next, learn about advanced text processing with the AWK command .
Bash Heredoc
Published on May 9, 2019
When writing shell scripts you may be in a situation where you need to pass a multiline block of text or code to an interactive command, such as tee , cat , or sftp .
In Bash and other shells like Zsh, a Here document (Heredoc) is a type of redirection that allows you to pass multiple lines of input to a command.
The syntax of writing HereDoc takes the following form:
- You can use any string as a delimiting identifier, the most commonly used are EOF or END.
- If the delimiting identifier is unquoted, the shell will substitute all variables, commands and special characters before passing the here-document lines to the command.
- Appending a minus sign to the redirection operator <<- , will cause all leading tab characters to be ignored. This allows you to use indentation when writing here-documents in shell scripts. Leading whitespace characters are not allowed, only tab.
- The here-document block can contain strings, variables, commands and any other type of input.
- The last line ends with the delimiting identifier. White space in front of the delimiter is not allowed.
Basic Heredoc Examples #
In this section, we will look at some basic examples of how to use heredoc.
Heredoc is most often used in combination with the cat command .
In the following example, we are passing two lines of text containing an environment variable and a command to cat using a here document.
As you can see from the output below, both the variable and the command output are substituted:
Let’s see what will happen if we enclose the delimiter in single or double quotes.
You can notice that when the delimiter is quoted no parameter expansion and command substitution is done by the shell.
If you are using a heredoc inside a statement or loop, use the <<- redirection operation that allows you to indent your code.
Instead of displaying the output on the screen you can redirect it to a file using the > , >> operators.
If the file.txt doesn’t exist it will be created. When using > the file will be overwritten, while the >> will append the output to the file.
The heredoc input can also be piped. In the following example the sed command will replace all instances of the l character with e :
To write the piped data to a file:
Using Heredoc with SSH #
Using Heredoc is one of the most convenient and easiest ways to execute multiple commands on a remote system over SSH .
When using unquoted delimiter make sure you escape all variables, commands and special characters otherwise they will be interpolated locally:
You may also want to set up an SSH key-based authentication and connect to your Linux servers without entering a password.
Conclusion #
In this guide, you have learned what is heredoc and how to use it in your shell scripts.
If you have any questions or feedback, feel free to leave a comment.
Related Tutorials
Bash: Append to File
Writing comments in bash scripts, pushd and popd commands in linux.
- How to Increment and Decrement Variable in Bash (Counter)
- Bash Shebang
- How to Check if a String Contains a Substring in Bash
- Bash Source Command
If you like our content, please consider buying us a coffee. Thank you for your support!
Sign up to our newsletter and get our latest tutorials and news straight to your mailbox.
We’ll never share your email address or spam you.
Related Articles
Feb 7, 2024
Mar 18, 2024
Sep 21, 2019
Here Document and Here String
Last updated: March 18, 2024
Baeldung Pro comes with both absolutely No-Ads as well as finally with Dark Mode , for a clean learning experience:
>> Explore a clean Baeldung
Once the early-adopter seats are all used, the price will go up and stay at $33/year.
Modern software architecture is often broken. Slow delivery leads to missed opportunities, innovation is stalled due to architectural complexities, and engineering resources are exceedingly expensive.
Orkes is the leading workflow orchestration platform built to enable teams to transform the way they develop, connect, and deploy applications, microservices, AI agents, and more.
With Orkes Conductor managed through Orkes Cloud, developers can focus on building mission critical applications without worrying about infrastructure maintenance to meet goals and, simply put, taking new products live faster and reducing total cost of ownership.
Try a 14-Day Free Trial of Orkes Conductor today.
1. Overview
In this tutorial, we’ll be taking a look at the syntax of Linux’s here document and here string. In addition to that, we’ll also demonstrate several common usage patterns associated with them.
2. Definition
In Linux, here document (also commonly referred to as heredoc) refers to a special block of code that contains multi-line strings that will be redirected to a command . On the other hand, here string is a simpler version of heredoc, offering somewhat similar functionality, albeit not as powerful as the latter .
To understand the benefits of using either one, imagine that we want to send several lines of command over ssh to another remote system. In addition to that, let’s assume that the command needs to be sent without an interactive terminal. We can easily achieve that with the following:
In the trivial example above, we’re executing two lines of code over ssh on the remote system. However, if we’ve more commands to execute, the solution above will become cluttered and verbose. To make it more succinct, we can replace it with a heredoc:
The command above is doing the same thing as the previous command in a much more concise form.
3. Here Document
3.1. syntax.
A heredoc consists of the ‘<<‘ redirection operator, followed by a delimiter token. After the delimiter token, lines of string can be defined to form the content. Finally, the delimiter token is placed at the end to serve as the termination. The delimiter token can be any value as long as it is unique enough that it won’t appear within the content . For example, we can create a heredoc that consists of several lines of simple string and have it output by the command cat :
In this example, we’ve defined the delimiter token as “EOF”. On the other hand, the three lines of string that are sandwiched between the pair of “EOF” tokens serve as the content. Then, those three strings are fed into the cat command, which will then print them to the console.
Do take note that the delimiter token will not be in the output of the heredoc .
3.2. Tab Suppression
Commonly, we add indentations to our heredoc with tabs so that it is much easier to read, especially in a shell script. However, rarely do we want the tabs to be part of the output. To suppress the tab indentations in the output, we can prefix the redirection operator ‘<<‘ with a dash symbol.
Concretely, if we do the following:
We’ll get the following output on our console:
However, white spaces will not be suppressed even with the dash prefix .
3.3. Here Document With Parameter Substitution
We can also parameterize a heredoc by using variables, thereby making it dynamic. For example, we can change the output of a heredoc based on the value of a parameter:
The output will then be customized based on the value of the variable USER .
3.4. Here Document With Command Substitution
In addition to parameters, heredoc also supports command substitution in its content. For example, let’s say we want to customize the output such that it always prints the current date and time:
Now, whenever we run the command above, the output will always have the current date and time.
3.5. Passing Arguments to Function With Here Document
We can also make use of heredoc to pass arguments to a function that otherwise requires interactive user input. For example, let’s consider the following function:
When invoked, the function will wait for the user’s input to capture the value for the variables username and passphrase :
Other than supplying the input interactively, we can construct a heredoc to pass the values:
3.6. Escape Special Characters in Here Document
By default, the special characters like $, \, and ` are interpreted in heredoc so that functionality like parameter and command substitution could work. To escape them, we can prefix the special character with a backslash ‘\’:
In the case that we want to escape all the special characters, we can either quote the delimiter token or prefix the delimiter token with a backslash .
The three different ways of escaping all the special characters are:
One important thing to note is that when all the special characters are escaped, parameter and command substitution will no longer work . The reason is that the special characters that are used in parameter and command substitution will be escaped, not interpreted.
3.7. Disable Block of Code Using Here Document
In a shell script, one way to disable a block of code is to prefix every line of the code with a ‘#’ to make them into comments. However, we can do it much more efficiently using heredoc with the dummy command ‘:’. For example, we can disable several lines of code in our shell script:
By turning a block of code into a heredoc and redirecting it to the dummy command ‘:’, we’ve essentially prevented the block of code from executing. We’ve enclosed the delimiter token to escape all the special characters in the content . This is done to prevent any command substitution from being executed , thereby preventing any unintended side effects.
4. Here String
Here string is very similar to a heredoc, except that the former is a much simpler version of heredoc. For that reason, here string does not need a delimiter token . It is usually preferred whenever we need a quick way to redirect some strings into a command.
4.1. Syntax
To construct a here string, we use “<<<” operator to redirect a string into a command. Concretely, the syntax is:
What it essentially does is expanding the variable VAR and redirect the output string to the COMMAND.
4.2. Basic Usage
The simplest form of here string comprised of a command, followed by a “<<<” redirector and a string:
Besides that, we can redirect a variable that contains a string:
4.3. Here String With Parameter
Similar to a heredoc, we can also parameterize the output of our here string with variables:
4.4. Escape Special Characters in Here String
To escape special characters like $, \, and `, we can enclose the string with the single quote instead of the double quote:
If we enclose the here string with a single quote, variable expansion such as ${USER} will stop working.
5. Conclusion
In this article, we’ve introduced the syntax for heredoc and here string. Besides that, we’ve also briefly compared their differences in terms of functionality and syntax. Finally, we’ve also demonstrated several sample usages on both heredoc and here string.
Learn Docker With My Newest Course
Dive into Docker takes you from "What is Docker?" to confidently applying Docker to your own projects. It's packed with best practices and examples. Start Learning Docker →
Updated on April 23, 2024 in #linux
Output, Assign, Pipe and Redirect a Heredoc in a Shell Script
You may want to do this when working with multi-line strings..
A “Here Document” is a useful way to output text.
It’s a feature of most programming languages. It’s commonly used to output multiple lines of text In such a way that you don’t need to worry about using a convoluted way of breaking up lines or escaping quotes.
We’ll break down the first example in the most detail. The other examples incrementally combine using other shell features to handle specific use cases.
It’s worth pointing out all of these examples will work with sh , bash and other compatible shells. There is 1 exception that only works with Bash or Bash-compatible shells and that will be called out, however in that case we’ll use an alternative method that works with sh too.
This could be useful to output a multi-line string.
With interpolation
We’re sending input to cat which then outputs it back. A heredoc expects us to pass in a string delimiter too. Using EOF (end of file) is commonly used but it’s not required, technically you can use any string as long as it matches on the top and bottom.
It’s really important that the bottom EOF exists on its own line with no extra indentation. If you deviate from that, it won’t be considered delimited.
Here we can see that variable interpolation works too because ${numbers} got interpolated to its value of 123 . All expressions will get interpolated.
If our multi-line string had single ' or double " quotes inside of it everything will just work without needing to escape them. That’s quite nice and also important to call out because you may choose to use a heredoc even for 1 line of output if you find yourself working with a string that has a lot of mixed quotes.
Without interpolation
This is the same as above except variables and other expression won’t get interpolated.
The only difference is we wrapped EOF in single quotes. Technically double quotes works too but I prefer using single quotes because when working with shell scripts if you wrap a string in single quotes it won’t interpolate your value. This matches the same pattern.
It’s also worth pointing out if you didn’t use 'EOF' you can still output something like ${numbers} literally by escaping the $ with \${numbers} .
What if you want to assign the heredoc to a variable?
This is pretty interesting because we’re doing the same cat strategy as before. The only difference is using command substitution to run it in a sub-shell and assign it to a variable.
If you Google around for this you might see this StackOverflow answer that suggests using read instead to “avoid a useless cat”.
That would look like this:
The above produces the same output as the cat version, but it won’t work with sh because POSIX compliant shell doesn’t support the -d flag with read . This flag says that input lines should be delimited by a null byte.
There is a comment that I agree with which questions if it’s really worth avoiding the useless cat. I don’t mind the useless cat because in my opinion it’s more understandable given it piggy backs off prior knowledge of using a heredoc in other ways. It’s also more portable.
You can also pipe your heredoc into another command.
Notice how the output got sorted. It might seem weird to see the pipe at the top instead of after the bottom EOF but if you think about the heredoc “owning” the delimiter then it might make more sense.
The pipe is still operating like any other pipe sequence you might use on the shell. The output of cat is being piped into sort as input.
Pipe (with file support)
Certain tools may accept input but only in the form of a file. We’re going to cover 2 examples using kubectl and curl . It’s really not different than the above example.
I’ve omit the output here because it’s not important. You may have seen this pattern used in a bunch of spots within Kubernetes’ documentation. If not, that’s ok because none of this is specific to Kubernetes.
The main takeaway is kubectl apply expects you to pass in a file. A common but not enforced Unix convention is to allow using - as a file name which will read the file from STDIN instead of a real file on disk.
It’s what allows us to pass in a multi-line string file as input to kubectl and other tools that use this pattern. It’s quite handy for writing scripts that use these tools because it lets you avoid creating temporary files on disk only to delete them after the command is run.
The curl error makes sense because I don’t have a web server running on localhost but this pattern does work for when you want to make a real HTTP request to a site while sending your payload in as a multi-line string.
It uses the - trick as mentioned before, except in this case curl excepts you to use @- because @ lets curl differentiate that we want to use a file.
By the way, --data-binary is being used instead of --data ( -d ) so that when you send your payload with the request, new lines aren’t stripped.
Lastly we can redirect our multi-line string to a file. You can choose to use >> instead of > to append to the file too.
Technically the script doesn’t output anything but it did write an example_output file out with the contents of a b c on new lines.
If you ran the above command you can run cat example_output && rm $_ to both see the file and then delete it. I’ve made a post in the past about what $_ does .
The demo video below runs all of these commands and shows how they work.
# Demo Video
- 0:20 – A run down on heredocs
- 2:56 – Avoiding variable or expression interpolation
- 4:31 – Assigning a multi-line string to a variable with cat vs read
- 7:19 – Piping a heredoc to another command as input
- 8:20 – Piping things into kubectl where it expects a file as input
- 10:04 – Doing the same as before but with curl
- 11:14 – Redirecting a multi-line string to a file
What’s your preferred way to hide user input? Let me know below.
Never Miss a Tip, Trick or Tutorial
Like you, I'm super protective of my inbox, so don't worry about getting spammed. You can expect a few emails per year (at most), and you can 1-click unsubscribe at any time. See what else you'll get too.
How-To Geek
How to Use "Here Documents" in Bash on Linux
Your changes have been saved
Email is sent
Email has already been sent
Please verify your email address.
You’ve reached your account maximum for followed topics.
Quick Links
Here documents, basic principles of here documents, simple examples, handling tab characters, redirecting to a file, piping the output to another command, sending parameters to a function, creating and sending an email, using here documents with ssh, strange name, neat features.
The strangely named "here documents" let you use input/out redirection inside Bash scripts on Linux. They're a great way to automate commands you need to run on a remote computer.
Many commands in Linux have two or three letter names. This is partly what gives rise to the notion that Linux is hard to learn and full of arcane commands. But one of the weirdest names in Linux isn't one of the cryptically short ones. "Here documents" aren't documents, and it isn't really clear what the "here" refers to, either.
They are a relatively obscure construct, but they are useful. Of course, this is Linux, so there's more than one way to skin a cat. Some of the functionality provided by here documents can be reproduced in other ways. Those alternate methods are usually more complicated. In programming and scripting, "more complicated" also means "more prone to bugs," and that your code is harder to maintain.
Where here documents really excel is in the automation of commands that you want to send to a remote computer from a connection established from within a script. Making the connection is easy, but once the connection has been made, how do you "pump" your commands from your script into the shell on the remote computer? Here documents let you do that very simply.
The idiomatic representation of a here document looks like this:
COMMAND << limit_string
limit_string
- COMMAND : This can be any Linux command that accepts redirected input. Note, the echo command doesn't accept redirected input . If you need to write to the screen, you can use the cat command, which does .
- << : The redirection operator.
- limit_string : This is a label. It can be whatever you like as long as it doesn't appear in the list of data you're redirecting into the command. It is used to mark the end of the text, data, and variables list.
- Data List : A list of data to be fed to the command. It can contain commands, text, and variables. The contents of the data list are fed into the command one line at a time until the _limit_string is encountered.
You'll probably see examples of here documents that use "EOF" as the limit string. We don't favor that approach. It works, but "EOF" means "End of File." Apart from the rare case where a home document is the last thing in a script file, "EOF" is being used erroneously.
It will make your scripts much more readable if you use a limit string that refers to what you're doing. If you're sending a series of commands to a remote computer over Secure Shell (SSH), a limit string called something like "_remote_commands" would make perfect sense. You don't need to start them with an underscore " _ " character. We do that because it marks them as something out of the ordinary in your script.
You can use here documents on the command line and in scripts. When you type the following in a terminal window, you'll see a " > " line continuation prompt each time you hit "Enter." When you type the "_end_of_text" limit string and hit "Enter," the list of websites is passed to cat, and they are displayed in the terminal window.
cat << _end_of_text
Review Geek
CloudSavvy IT
_end_of_text
That's not the most worthwhile of exercises, but it does demonstrate that nothing is sent to the command until the entire list of data is collated and the limit string is encountered. The cat command doesn't receive any input until you enter the limit string "_end_of_text" and hit the "Enter" key.
We can do the same thing in a script. Type or copy this example into an editor, save the file as "heredoc-1.sh", and close the editor.
#!/bin/bash
cat << "_end_of_text"
Your user name is: $(whoami)
Your current working directory is: $PWD
Your Bash version is: $BASH_VERSION
As you follow this article, each time you create a script, you'll need to make it executable before it will run. In each case, use the chmod command . Substitute the name of the script in each example for the script name used here.
chmod +x heredoc-1.sh
This script contains two environment variables, $PWD and $BASH_VERSION . The environment variable names are replaced by their data values---the current working directory and the version of Bash---when the script is executed.
The script also uses command substitution on the whoami command . The name of the command is replaced by its own output. The output from the entire script is written to the terminal window by the cat command. We run the script by calling it by name:
./heredoc-1.sh
If you modify the script and wrap the limit string in the first line of the here document in quotation marks " " ", the data list is passed to the here document command verbatim. Variable names are displayed instead of variable values, and command substitution will not take place.
cat <<- "_end_of_text"
By default, tab characters in your data list will be retained and written to the terminal window. Copy and save this example as "heredoc-2.sh." Make it executable using the chmod command. Edit the indented lines to make sure that they have one or two tab characters at the start of the line rather than a series of spaces.
./heredoc-2.sh
The tabs are written to the terminal window.
By adding a dash " - " to the redirection operator, the here document will ignore leading tab characters. Save this example as "heredoc-3.sh" and make it executable.
cat <<- _end_of_text
./heredoc-3.sh
The tabs are ignored. This might seem trivial, but it is a neat way to cope with leading tabs due to indented sections of scripts.
Loops and other logical constructs are usually indented. If your here document is contained in an indented section of a script, using a dash " - " with the redirection operator removes formatting issues caused by the leading tab characters.
if true; then
cat <<- _limit_string
Line 1 with a leading tab.
Line 2 with a leading tab.
Line 3 with a leading tab.
_limit_string
The output from the command used with the here document can be redirected into a file. Use the " > " (create the file) or " >> " (create the file if it doesn't exist, append to the file if it does) redirection operators after the limit string in the first line of the here document.
This script is "heredoc-4.sh." It will redirect its output to a text file called "session.txt."
cat << _end_of_text > session.txt
./heredoc-4.sh
cat session.text
The output from the command used in a here document can be piped as the input to another command. Use the pipe " | " operator after the limit string in the first line of the here document. We're going to pipe the output from the here document command, cat , into sed . We want to substitute all occurrences of the letter "a" with the letter "e".
Name this script "heredoc-5.sh."
cat << _end_of_text | sed 's/a/e/g'
./heredoc-5.sh
"Gaak" is corrected to "Geek."
The command that is used with a here document can be a function in the script.
This script passes some vehicle data into a function. The function reads the data as though it had been typed in by a user. The values of the variables are then printed. Save this script as "heredoc-6.sh".
# the set_car_details() function
set_car_details ()
read new_used
read delivery_collect
read location
# The here document that passes the data to set_car_details()
set_car_details << _mars_rover_data
Perseverance Rover
Mars (long,lat) 77.451865,18.445161
2.2 billion
_mars_rover_data
# Retrieve the vehicle details
echo "Make: $make"
echo "Model: $model"
echo "New or Used: $new_used"
echo "Delivery or Collection: $delivery_collect"
echo "Location: $location"
echo "Price \$: $price"
./heredoc-6.sh
The vehicle details are written to the terminal window.
We can use a here document to compose and send an email. Note that we can pass parameters to the command in front of the redirection operator. We're using the Linux mail command to send an email through the local mail system to the user account called "dave". The -s (subject) option allows us to specify the subject for the email.
This example forms script "heredoc-7.sh."
article="Here Documents"
mail -s 'Workload status' dave << _project_report
User name: $(whoami)
Has completed assignment:
Article: $article
_project_report
./heredoc-7.sh
There is no visible output from this script. But when we check our mail, we see that the email was composed, dispatched, and delivered.
Here documents are a powerful and convenient way to execute some commands on a remote computer once an SSH connection has been established. If you've set up SSH keys between the two computers, the login process will be fully automatic. In this quick and dirty example, you'll be prompted for the password for the user account on the remote computer.
This script is "heredoc-8.sh". We're going to connect to a remote computer called "remote-pc". The user account is called "dave". We're using the -T (disable pseudo-terminal allocation) option because we don't need an interactive pseudo-terminal to be assigned to us.
In the "do some work in here" section of the script, we could pass a list of commands, and these would be executed on the remote computer. Of course, you could just call a script that was on the remote computer. The remote script could hold all of the commands and routines that you want to have executed.
All that our script---heredoc-8.sh---is going to do is update a connection log on the remote computer. The user account and a time and date stamp are logged to a text file.
ssh -T [email protected] << _remote_commands
# do some work in here
# update connection log
echo $USER "-" $(date) >> /home/dave/conn_log/script.log
_remote_commands
When we run the command, we are prompted for the password for the account on the remote computer.
./heredoc-8.sh
Some information about the remote computer is displayed, and we're returned to the command prompt.
On the remote computer, we can use cat to check the connection log:
cat conn_log/script.log
Each connection is listed for us.
Related: How to Create and Install SSH Keys From the Linux Shell
Here documents are quirky but powerful, especially when used to send commands to a remote computer. It'd be a simple matter to script a backup routine using rsync . The script could then connect to the remote computer, check the remaining storage space, and send an alerting email if space was getting low.
Related: How to Back Up Your Linux System With rsync
- Trending Categories
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
Bash HereDoc Tutorial With Examples
If you're a Linux or Unix user, you're probably familiar with Bash, command-line shell that's commonly used on these systems. Bash has a lot of powerful features that can make working on command line much more efficient, and one of those features is called a "HereDoc." In this tutorial, we'll explain what a HereDoc is, how it works, and give you some examples of how you can use it in your Bash scripts.
What is a HereDoc?
A HereDoc, short for "Here Document," is a way to include a block of text within a Bash script. This block of text can contain anything you want, including commands, variables, and other special characters. HereDocs are useful because they allow you to include large blocks of text without having to worry about escaping special characters or dealing with complicated quoting rules.
Here's an example of a HereDoc in action −
In this example, we're using "cat" command to output a block of text to terminal. "<< EOF" syntax tells Bash to start a HereDoc block, and "EOF" at end tells Bash where block ends. Anything between those two markers is included in HereDoc.
The HereDoc block can contain any text you want, including variables, commands, and other special characters. Here's an example that demonstrates how to use a HereDoc to store a SQL query in a variable −
In this example, we're using a HereDoc to store a SQL query in "query" variable. We're then using "echo" command to output contents of variable to terminal. This can be a very useful technique when you need to store large blocks of text in a script.
HereDoc Syntax
The basic syntax for a HereDoc is as follows −
In this syntax, "command" is command that will receive HereDoc as input, and "text" is block of text that will be included in HereDoc. "EOF" marker tells Bash where HereDoc ends.
The HereDoc syntax is very flexible and can be used in a variety of ways. Here are some examples −
You can use a HereDoc to provide input to a command, like this −
You can use a HereDoc to assign a block of text to a variable, like this −
You can use a HereDoc to create a file with a block of text, like this −
You can use a HereDoc to append a block of text to a file, like this −
HereDoc Examples
Let's take a look at some real-world examples of how HereDocs can be used in Bash scripts.
Example 1: HereDoc to Create a Configuration File
Suppose you're writing a script that needs to read settings from a configuration file. Instead of creating file manually, you can use a HereDoc to generate file from within your script. Here's an example −
In this example, we're using a HereDoc to generate a configuration file called "config.ini". file contains several sections, including a "server" section and a "database" section. Each section contains several key-value pairs that define settings for script.
By using a HereDoc to generate configuration file, we can ensure that file is always generated correctly and that it contains all necessary settings.
Example 2: HereDoc to Run a SQL Script
Suppose you have a SQL script that you need to run on a remote server. Instead of copying script to server and running it manually, you can use a HereDoc to run script remotely. Here's an example −
In this example, we're using a HereDoc to run a SQL script called "script.sql" on a remote server. We're using "ssh" command to connect to server, and "<< EOF" syntax to start a HereDoc block.
The HereDoc block contains a single command, "mysql -u myuser -p mypassword < script.sql", which runs SQL script on remote server. By using a HereDoc to run script remotely, we can avoid need to copy script to server manually.
Example 3: HereDoc to Generate HTML Output
Suppose you're writing a script that needs to generate HTML output. Instead of concatenating strings and tags manually, you can use a HereDoc to generate HTML code. Here's an example −
In this example, we're using a HereDoc to generate an HTML file called "index.html". file contains a basic HTML structure, including a title, a header, and some sample text. By using a HereDoc to generate HTML code, we can ensure that code is always generated correctly and that it contains all necessary tags.
Additional tips and best practices for HereDocs
Use descriptive markers.
When defining HereDoc block, use a descriptive marker instead of just "EOF". For example, if you're defining a SQL query, you could use "<< SQL" as marker. This makes code more readable and helps avoid confusion.
Use Single Quotes
When defining HereDoc block, it's best to use single quotes around marker. This prevents Bash from interpreting any variables or special characters in block.
Use Indentation
When defining HereDoc block, it's a good idea to use indentation to make code more readable. This is especially useful when block contains multiple levels of indentation.
Use "-r" option
When reading input from a HereDoc, it's a good idea to use "-r" option with read command. This prevents backslashes from being interpreted as escape characters.
Avoid Using Tabs
When defining HereDoc block, avoid using tabs for indentation. This can cause issues with some commands, especially those that expect input to be formatted a certain way.
Use Variables in HereDocs
You can use variables in HereDocs just like you would in any other part of your Bash script. This can be a powerful technique for generating dynamic content.
Use HereDocs with other Bash Features
HereDocs can be used in combination with other Bash features, such as loops, conditionals, and functions. This can make your scripts even more powerful and flexible.
By following these tips and best practices, you can use HereDocs effectively in your Bash scripts and take advantage of their many benefits. Whether you're generating configuration files, running remote scripts, or generating HTML output, HereDocs can help make your Bash scripts more efficient and effective.
In this tutorial, we've explained what a HereDoc is and how it works in Bash. We've also given you some examples of how you can use HereDocs in your Bash scripts, including generating configuration files, running SQL scripts remotely, and generating HTML output.
HereDocs are a powerful feature of Bash that can help you write more efficient and effective scripts. By using HereDocs, you can include large blocks of text in your scripts without having to worry about escaping special characters or dealing with complicated quoting rules. So if you're a Bash user, be sure to give HereDocs a try!
- Related Articles
- Bash wait Command with Examples
- Bash Script for Loop Explained with Examples
- Bash if elif else Statement A Comprehensive Tutorial
- GUI Testing Tutorial: User Interface (UI) Test Cases with Examples
- Backend Testing Tutorial (What is, Tools, Examples)
- Bash declare Statement Syntax and Examples
- Fragment Tutorial with Example in Android Studio?
- What is SOA Testing? Tutorial with Example
- What is Storage Testing Tutorial with Type & Concepts?
- Testing Bash Scripts With Bats in Linux
- Bash Math Operations (Bash Arithmetic) Explained
- ELK Stack Tutorial: Get Started with Elasticsearch, Logstash, Kibana, & Beats
- Model-Based Testing Tutorial
- Bash Printf - How to Print a Variable in Bash
- End-to-End Testing Tutorial: What is E2E Testing with Example
Kickstart Your Career
Get certified by completing the course
- Open Source
Bash Heredoc Tutorial For Beginners
When working with Bash scripts, you may end up in a situation where you have to process a series of inputs using the same command. Fortunately, there is a way in Bash to achieve this in a more optimal way using HereDoc .
HereDoc, acronym for Here Document , is an input Redirection method to pass multiple inputs to a program or command. The concept of heredoc is not exclusively related to Bash alone. Many popular programming languages like Perl, Ruby, PHP support heredoc.
In this article, we will take a look at the syntax and usage of heredoc with some real-world use cases. All the examples in this article are created to be simple, so even a newbie can understand this concept easily. Let’s jump in and start playing with heredoc in Bash.
Table of Contents
HereDoc Syntax
The following diagram illustrates the syntax of heredoc.
- Command - Any command (cat, wc, mail, etc..) that accepts redirection.
- Redirection Operator ( << ) - The default operator for HereDoc is << . It redirects the block of code to the command for processing.
- Delimiter Token - The delimiter token denotes the start and end of the document (code block). The delimiter token can be anything but it should be identical. Typically you will see EOF is used as delimiter tokens which mean "End Of File Stream".
Printing MultiLine String Using HereDoc In Bash
Let’s start with a simple example of redirecting a multiline string and printing it to the terminal.
The cat command accepts a stream of inputs and with heredoc, and you can redirect the block of code to print it to the terminal.
Take a look at the above code snippet. I have three lines that are redirected to the cat command. I am using EOF as the delimiter. However, you can use anything as you wish but keep the starting and ending delimiter identical.
Let’s try with one more simple example. I am redirecting the same three-line to word count program. I am using a different delimiter ( BLK ) here.
Sample output:
Redirecting And Pipe In HereDoc
You can combine the output redirection operator with heredoc and redirect the output to a file instead of printing it to the terminal.
I am using the same example which I used in the previous section and redirecting the output to a file named log_op.txt .
The output of heredoc can be sent to the pipe operator for further processing.
Tab Suppression In HereDoc
When there are white spaces (tabs) in your code block and if you wish to suppress it, then use "-" after the redirection operator. An important point to be noted is that only tabs will be suppressed, not spaces.
Take a look at the below example. I have added a conditional statement to the same example that we have seen in previous sections. The first two lines in heredoc are tabbed (4) and the third line is spaced (2).
When the code snippet is submitted, then my output will be as follows.
As you can see, line 1 and 2 tabs are suppressed, but in line 3 since spaces are used it is not suppressed.
Variable And Command Expansion In HereDoc
It is not that you can pass only strings within the heredoc code block. You can pass user-defined and environmental variables and run commands within the code block.
Take a look at the below example. Within the code block, I have one user-defined variable "${AUTHOR}" , one environmental variable "${SHELL}" , an external command "whoami" .
When this snippet is submitted, variables and commands will be expanded then it will be redirected to the cat command.
You can enclose the starting delimiter with single quotes to suppress the expansion within the code block. This way everything within the code block will be treated as a string literal.
MultiLine Comments With HereDoc
As you may already know, Bash does not support multi-line comments. Using heredoc, you can create multi-line comments by redirecting the block of code to the no-op command ( : ).
The no-op is bash built-in which takes the input and returns exit code zero. You can consider this as a synonym to the bash built-in "true" which also exits exit code zero.
Heads Up: Almost every text editor has the feature of selecting multiple lines and allows you to comment or uncomment using a keystroke. It is better to stick with this approach.
Escaping Special Characters In HereDoc
Code blocks may contain special characters. If you wish to escape the special characters, there are few ways to get it done.
You can enclose the delimiter with single or double quotes or prefix backslash with the delimiter. This way all the special characters will be escaped.
Instead of escaping all the special characters, you can also escape particular special characters within the block by adding a backslash before any special character.
HereDoc Use Case
Till now we have seen the core construct of heredoc and its basic usage. Now let’s see some real-life use cases. In my experience, I have used heredoc when working with ssh, and database clients where I have to run a group of commands.
Example 1: Running As Different User Inside The Script
In some cases, you may want to run certain parts of your code as a different user. In that case, you can use heredoc to redirect the commands to run as a different user.
Take a look at the below example. I am redirecting the block of code to the su command which will switch the user to "ostechnix" and will create a file named test if it does not exist.
Example 2: Using HereDoc with DB Client
When you want to run a series of commands against a database, heredoc will come in handy.
Take a look at the below example. I am interacting with MongoDB client mongosh and within the heredoc, code block commands are passed to create a new database, collection, and adding a sample document.
You may or may not be knowing MongoDB but that is fine. This is just to show how to use heredoc to interact with db clients. You can use any db client like MySQL, psql, sqlite depending upon which database you are working with.
Example 3: Executing Remote Commands with HereDoc and SSH
When you wish to run commands over the remote server, then you can use heredoc in combination with ssh command. Normally using ssh command, you can run commands in the remote server in the following way.
You have to repeat the same command again and again if you wish to run more commands in the remote host. With heredoc, you can group all the commands and run them.
When the same code needs to be executed across multiple nodes you can add for loop along with heredoc. I am using the same file creation snippet which we have seen in the first example.
- The array variable "server" holds the list of server names.
- For loop iterates over the array variable.
- The file creating command is passed to the SSH command that will iterate over and create files in each server. Make sure you add -T flag to ssh command which will disable pseudo-terminal allocation.
Heredoc is an important concept to understand and use in Bash scripts. When you write a lot of scripts you will come to know more about heredoc and ways to optimally use it.
If you have never used heredoc before, start the terminal and try all the code snippets from the article to have a better understanding.
Related read:
- Bash Scripting – Variables Explained With Examples
- Bash Scripting – Functions Explained With Examples
Bash Echo Command Explained With Examples In Linux
- Bash Scripting – For Loop Explained With Examples
- Bash Scripting – While And Until Loop Explained With Examples
Karthick is a passionate software engineer who loves to explore new technologies. He is a public speaker and loves writing about technology especially about Linux and opensource.
How To Zoom In And Zoom Out Videos Using FFmpeg
You may also like, bash scripting – string manipulation, bash scripting – variables explained with examples, shellcheck – a free utility to find bugs..., how to check package availability in debian and..., how to debug bash scripts in linux and..., leave a comment cancel reply.
Save my name, email, and website in this browser for the next time I comment.
This site uses Akismet to reduce spam. Learn how your comment data is processed .
This website uses cookies to improve your experience. By using this site, we will assume that you're OK with it. Accept Read More
Stack Exchange Network
Stack Exchange network consists of 183 Q&A communities including Stack Overflow , the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
How to pass variables to a HEREDOC in bash?
I want to do something like this:
- shell-script
Worked for me.
Looks like when you take ==end in the ' variable doesn't substitute.
The Here Documents section of the manual says:
The format of here-documents is: <<[-]word here-document delimiter No parameter expansion, command substitution, arithmetic expansion, or pathname expansion is performed on word . If any characters in word are quoted, the delimiter is the result of quote removal on word, and the lines in the here-document are not expanded. If word is unquoted, all lines of the here-document are subjected to parameter expansion, command substitution, and arithmetic expansion. [...]
- You can also use double-quotes ( cat << "==end" > test ), but hard-quotes prevent substitution indeed. – Mat Commented Aug 2, 2012 at 7:31
- Double quotes also prevent parameter expansion – Quantum7 Commented Jun 12, 2020 at 8:33
You must log in to answer this question.
Not the answer you're looking for browse other questions tagged linux bash shell-script ..
- The Overflow Blog
- Masked self-attention: How LLMs learn relationships between tokens
- Deedy Das: from coding at Meta, to search at Google, to investing with Anthropic
- Featured on Meta
- User activation: Learnings and opportunities
- Preventing unauthorized automated access to the network
Hot Network Questions
- Oxidation of methanoic acid (formic acid) to carbon dioxide and water
- Is there no diagonal relationship for carbon and phosphorus?
- In a shell script, how do I wait for a volume to be available?
- This puzzle is delicious
- Which ancient philosopher compared thoughts to birds?
- What is the name for this BC-BE back-to-back transistor configuration?
- Is it natural to say "could he" instead of "if he could"? E.g.: "Could he have cast himself in the part of Mr Copthorne, he would not have attempted…"
- How can Mayday shoot webs without a web-shooter device?
- In what range does incrementing and decrementing Java doubles work?
- What is the position of Lebanon about the presence of the Hezbollah on its territory?
- Why does not Earth radiate more energy into the space while it gets warmer?
- What does "we are out"mean here?
- Can one freely add an explanation to a quotation in square brackets?
- Why won't simple code get auto-vectorized with SSE and AVX in modern compilers?
- Why is there a difference between battery post voltage and battery terminal voltage?
- What is the name of a movie containing a futuristic plane using solid state fuel?
- After rolling a die 10 times, what is the probability that the sequence of rolls is non-decreasing?
- Can I possibly win in this kind of situation?
- How long could the Deep Space Network remain in contact with Voyager if the antenna was double the size?
- Used car dealership refused to let me use my OBDII on their car, is this a red flag?
- Geometry nodes mapping 0-1 UV to every mesh face
- What is the origin of the many extra mnemonics in Manx Software Systems’ 8086 assembler?
- Need help in identifying and learning to identify this unknown protocol, which has a good change to be proprietary of the hardware I'm analyzing
- How does 自由に不自由していない work here?
- Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers
- Advertising & Talent Reach devs & technologists worldwide about your product, service or employer brand
- OverflowAI GenAI features for Teams
- OverflowAPI Train & fine-tune LLMs
- Labs The future of collective knowledge sharing
- About the company Visit the blog
Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Get early access and see previews of new features.
How to assign variables with Command results with HEREDOC in shell script
Have to assign variable with command results , I am using set of commands that to be executed in another server and results to be assigned in variable and process from it
The code goes like
since the file is not getting assigned , i could store the contents in one variable which is to be used in the rest of the code and all other follwing codes are blocked . when examing sample.csv also the result does came
how to assign this variable
2 Answers 2
It's not at all clear to me what exactly what you are doing, but you should realize that the line a=$(cat $file) (I've used $() notation rather than backticks to avoid a clash with SO markup and because backticks are a scourge on humanity that should be avoided at all costs) is expanding both $file and the process substitution on the local host. Similarly, the ls -l is listing the directory on the local host from which you run Jump.Sh. You can avoid that by simply quoting the EOF :
will prevent any expansion locally and pass the raw text as input to Jump.Sh
- Thanks William . Its work like charm. One more clarification, the thing i am trying to do is to get the contents of file in one varible ( from other server) and use that variable in local . Does the assinging variable will be set at environment ? if so will it be deletd after session overs ? – Katchy Commented Aug 21, 2014 at 14:12
- If you want a variable on the local machine to contain the content of a file on the remote, I would simply do a=$(ssh remote cat /p/a/t/h) . (In reality, I would probably copy the file to the local host or access it over a network drive or run a server on the remote host that would allow a client to connect to a socket and get the data, but cat via ssh is not an unreasonable 4th choice.) – William Pursell Commented Aug 21, 2014 at 17:39
- now i am got into a new problem, before the Jump.sh line there is variable initialization for filename="sample.txt<date>" which is to searched inside remote server where date is passed by user input which dynamically changes. after i given 'EOF' i can't able to access that filename variable inside this commands. is there any work around for that? banging with errors sorry – Katchy Commented Aug 21, 2014 at 17:57
You wrap the command and the whole heredoc in $( ... ) :
The way you wrote your question forces me to say: the terminating heredoc word ( EOF ) must appear by itself on a line with no additional whitespace (unless you use <<-EOF , in which case leading tabs (only) are ignored).
Are you aware that this command will give you a lot more than just the filename:
That's a strange way to store a filename in a variable. What are you really trying to do there?
- If i give like this getting error saying ")" expected and unexpected end of line . i want to get the content of file in a variable where the file name is retrieved from that ls command. is there any way to get it ? – Katchy Commented Aug 21, 2014 at 18:02
- what i am trying is to get the file name store in the variable "actually the ls command with filename will return only one file for sure(only one file is created for particular date) actually we need get the date from local machine to remote machine. based on the date we store the file name in one variable and contents in one variable. and then print those variable by passing into file. I am really struck in getting the local variable to remote if i use 'EOH' and can't assign variable if i use without quotes but command runs in remote only. help needed on this . – Katchy Commented Aug 21, 2014 at 20:50
- i tired with Jump.sh Arg1="0821" "bash -s" <<'EOF' file=$(ls -l sample_$Arg1) echo $file EOF no use , date is not getting substituted and tried with $ and \$ for local and remote variable no use Tried with passing script with parameters no use – Katchy Commented Aug 21, 2014 at 20:51
Your Answer
Reminder: Answers generated by artificial intelligence tools are not allowed on Stack Overflow. Learn more
Sign up or log in
Post as a guest.
Required, but never shown
By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy .
Not the answer you're looking for? Browse other questions tagged shell unix eof or ask your own question .
- The Overflow Blog
- Masked self-attention: How LLMs learn relationships between tokens
- Deedy Das: from coding at Meta, to search at Google, to investing with Anthropic
- Featured on Meta
- User activation: Learnings and opportunities
- Preventing unauthorized automated access to the network
- Feedback Requested: How do you use the tagged questions page?
Hot Network Questions
- This puzzle is delicious
- Why is there a difference between battery post voltage and battery terminal voltage?
- What is Poirot saying about the feather pen in "The Murder of Roger Ackroyd"?
- Geometry nodes mapping 0-1 UV to every mesh face
- Is it considered vulgar to use "massive" to describe female breast size?
- How do I convey extreme speed to readers?
- How much homotopy theory before higher category theory?
- How do you measure exactly 31 minutes by burning the ropes?
- Is this solar system mechanically possible?
- Short Python script to count ngram frequencies
- How can things be possible per an impossible world?
- Modern x86-64 architecture diagram?
- Is mathematical Platonism possible without mind-body dualism?
- In the Silmarillion or the Appendices to ROTK, do the Dwarves of Khazad-dûm know about the Balrog below prior to Durin receiving the ring?
- Informal "chats" with potential grad school advisors
- Can I redeem myself with a better research paper if my thesis sucks?
- Oxidation of methanoic acid (formic acid) to carbon dioxide and water
- Is there no diagonal relationship for carbon and phosphorus?
- What are major reasons why Republicans support the death penalty?
- What does "we are out"mean here?
- Induction proof for floor function composition
- Where does the energy go if a fully loaded lithium battery in an electric vehicle cools down?
- displaylink-driver not installable with 6.1.0-25-amd64 kernel
- Do solar panels get less hot than objects of a similar color?
Stack Exchange Network
Stack Exchange network consists of 183 Q&A communities including Stack Overflow , the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Expanding only certain variables inside a heredoc
This is an extension of the question I asked . Passing directory from command line to shell script
I have a script which writes another script using a heredoc. I need to be able to write unexpanded variables in the heredoc, so I use single quotes ( 'EOF' ). However, I need one variable to be expanded. Given the script below, how can I write the value of $sourcedir inside the heredoc?
I am running this script using
I am able to pass /home/dir/data as $1 to sourcedir but it also needs to be passed to BASEDIR , in array script which is submitted to cluster using qsub.
- shell-script
- command-line
- As simple as BASEDIR=$1 . You need to escape all $ 's in your embedded script. If you don't get what I mean I can write an answer. – Weijun Zhou Commented Mar 12, 2019 at 19:05
- @WeijunZhou the actual script is much, much longer than this (I know this from chatting with the OP), so escaping all $ just to keep the value of one variable will be complicated. – terdon ♦ Commented Mar 12, 2019 at 19:08
- @ron it would help if you could show how you want $sourcedir in the array script. – terdon ♦ Commented Mar 12, 2019 at 19:08
- 1 In this case I usually do cat > wrapperscript.sh << EOF , then put run_array_job.sh $1 in the heredoc for wrapperscript.sh. – Weijun Zhou Commented Mar 12, 2019 at 19:10
- 1 You can leave the EOF quoted so that you don't need to escape the $ s for run_array_job.sh , but leave out the quote when you write heredoc for wrapperscript.sh – Weijun Zhou Commented Mar 12, 2019 at 19:12
2 Answers 2
That's somewhat hard to do, since unlike with quotes, you can't just stop and restart expansion in an here-doc. But we could post-process the here-doc with sed :
Running that produces the output:
The sed command simply changes all instances of %%sourcedir%% with whatever the value of $sourcedir (as long as it doesn't contain commas; you'd need to change the separator for the s command to something else then.)
I changed the placeholder to another format for clarity's sake, but you could also leave it as $sourcedir and then use sed -e "s,\$sourcedir,$sourcedir," . (Though note that it would also match $sourcedirectory and other similar variables, but not ${sourcedir} even though that would be equivalent in the shell.)
Alternatively, use GNU envsubst on the document, if you have it (it's part of gettext ):
- Is envsubst GNU only? – Weijun Zhou Commented Mar 12, 2019 at 19:49
- @WeijunZhou, yep, seems to be. – ilkkachu Commented Mar 12, 2019 at 20:16
You can most straightforwardly do this for your usage just by breaking your heredoc in two parts:
This just builds the first part of the file, appends the single line that you wanted the variable available for at the end of that first part using >> , and then joins the rest of the document on the end in the same way.
You end up with the same coherent file at the end, and only write a couple of lines extra. If you have multiple variables to pass through, you can put them all in at once as well.
- Thanks @Michael ,@Weijun also provided the same answer in the chat room. – Ron Commented Mar 12, 2019 at 20:53
You must log in to answer this question.
Not the answer you're looking for browse other questions tagged bash shell-script shell command-line cluster ..
- The Overflow Blog
- Masked self-attention: How LLMs learn relationships between tokens
- Deedy Das: from coding at Meta, to search at Google, to investing with Anthropic
- Featured on Meta
- User activation: Learnings and opportunities
- Preventing unauthorized automated access to the network
Hot Network Questions
- Order of function transformations
- Why does not Earth radiate more energy into the space while it gets warmer?
- How can Mayday shoot webs without a web-shooter device?
- Incorporated dough mix into sourdough starter
- Is mathematical Platonism possible without mind-body dualism?
- If a 'fire temple' was built in a gigantic city, with many huge perpetual flames inside, how could they keep smoke from bothering non-worshippers?
- There are some people in neighborhood wanting to put in community pool. There are some that do not want it. Can those not interested be forced to pay?
- What are major reasons why Republicans support the death penalty?
- In a shell script, how do I wait for a volume to be available?
- Concern with Gregory's argument for particle-like mutual gravitation of two spheres
- Definition of Chow quotient
- What does "we are out"mean here?
- After rolling a die 10 times, what is the probability that the sequence of rolls is non-decreasing?
- What is the name for this BC-BE back-to-back transistor configuration?
- What happens if parents refuse to name their newborn child?
- Randomly color the words
- Why is there a difference between battery post voltage and battery terminal voltage?
- What is Poirot saying about the feather pen in "The Murder of Roger Ackroyd"?
- Where does the energy go if a fully loaded lithium battery in an electric vehicle cools down?
- Informal "chats" with potential grad school advisors
- Switch or switches in the context of trains in American English?
- How do I convey extreme speed to readers?
- Modern x86-64 architecture diagram?
- Has mandated/suggested cycling liability insurance been implemented successfully in any jurisdiction?
IMAGES
VIDEO
COMMENTS
@JohnM - I have just tried a heredoc assignment with single-quoted 'EOF', with escaped linebreaks with ` in the content: if the second line has cd` command, I get back: ".sh: line X: cd: command not found"; but if I double-quote "EOF"; then bash variables ${A} do not get preserved as strings (they get expanded); but then, line-breaks are preserved - and, I don't have a problem running a ...
Note that any of the quoting mechanisms -- \____HERE or "____HERE" or '____HERE'-- will disable all variable interpolation, and turn the here-document into a piece of literal text. A common task is to combine local variables with script which should be evaluated by a different shell, programming language, or remote host.
About the OP solution: You do not need an eval to assign a variable if you allow some constant variable to be used. the general structure of calling a function that receives the HEREDOC could also be implemented.
How to assign a heredoc value to a variable in Bash? 283. Using variables inside a bash heredoc. 4. set variable in heredoc section. 1. How to assign variables with Command results with HEREDOC in shell script. 2. In Bash, how can a here-document contain a variable and then be stored in a variable? 0.
You can use the HereDoc variable to define text and then redirect the content to a file. This lets you overwrite a file with the stored text of the HereDoc variable. Let's see a bash script of how to redirect content using the HereDoc variable in bash: #!/bin/bash. output_file="file.txt". text=$(cat <<EOF.
The HereDoc itself contains any number of lines with strings, variables, commands, and other inputs. Bash HereDoc Examples. This section showcases how to use the HereDoc notation in various situations. The most common use case is with the cat command. Multiline String. Open the terminal and enter the following text, pressing Enter after each line:
If you want to use both variables then you'll need to leave the heredoc keyword unquoted, so that variable expansion takes place for the locally defined variable, and then escape (with a backslash) the variable that you want to be expanded remotely, i.e.:
In Bash and other shells like Zsh, a Here document (Heredoc) is a type of redirection that allows you to pass multiple lines of input to a command. The syntax of writing HereDoc takes the following form: DELIMITER. The first line starts with an optional command followed by the special redirection operator << and the delimiting identifier.
We can also parameterize a heredoc by using variables, thereby making it dynamic. For example, we can change the output of a heredoc based on the value of a parameter: cat <<EOF Hello ${USER} EOF. The output will then be customized based on the value of the variable USER. 3.4. Here Document With Command Substitution
Output. Assign. Pipe. Redirect. Demo Video. A "Here Document" is a useful way to output text. It's a feature of most programming languages. It's commonly used to output multiple lines of text In such a way that you don't need to worry about using a convoluted way of breaking up lines or escaping quotes. We'll break down the first ...
Use the "> " (create the file) or ">> " (create the file if it doesn't exist, append to the file if it does) redirection operators after the limit string in the first line of the here document. This script is "heredoc-4.sh." It will redirect its output to a text file called "session.txt." #!/bin/bash.
"EOF" marker tells Bash where HereDoc ends. The HereDoc syntax is very flexible and can be used in a variety of ways. Here are some examples −. You can use a HereDoc to provide input to a command, like this −. command << EOF input EOF You can use a HereDoc to assign a block of text to a variable, like this −. variable=$(cat << EOF text EOF )
7K. When working with Bash scripts, you may end up in a situation where you have to process a series of inputs using the same command. Fortunately, there is a way in Bash to achieve this in a more optimal way using HereDoc.. HereDoc, acronym for Here Document, is an input Redirection method to pass multiple inputs to a program or command. The concept of heredoc is not exclusively related to ...
Looks like when you take ==end in the ' variable doesn't substitute. The Here Documents section of the manual says: The format of here-documents is: <<[-]word. here-document. delimiter. No parameter expansion, command substitution, arithmetic expansion, or pathname expansion is performed on word. If any characters in word are quoted, the ...
bash$ bash -c 'lsof -a -p $$ -d0' << EOF > EOF lsof 1213 bozo 0r REG 3,5 0 30386 /tmp/t1213--sh (deleted) Some utilities will not work inside a here document . The closing limit string , on the final line of a here document, must start in the first character position.
Anyway, variable assignment was not performed inside here-document. - cuonglm. Commented Sep 23, 2015 at 7:00. file variable is being assigned in sub shell created for SSH session. Parent shell is unable to access those variables.. ... bash - add blank line to heredoc via variable. 1. Bash | Txt file > tmp> process tmp> ouput.
This doesn't set the variable foo to the contents of the heredoc: foo=<<EOF one two three EOF It's a variable assignment to an empty string, with a redirection. This might make it clearer what's happening: foo="" <<EOF one two three EOF But you don't really need heredocs for this. Just do: foo="one two three"
1. Have to assign variable with command results , I am using set of commands that to be executed in another server and results to be assigned in variable and process from it. The code goes like. Jump.Sh <username>@ [host] > sample.csv << EOF #will navigate particular box whre following cmd executed cd <dirname> file=`ls -l <filename> | head -1 ...
This is an extension of the question I asked . Passing directory from command line to shell script I have a script which writes another script using a heredoc. I need to be able to write unexpanded