Skip to main content

Questions tagged [here-document]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
0 votes
2 answers
52 views

Cat file and heredoc to pipe

I do some automatic checks and want to send a mail if a file has changed. I want the mail body to contain the new version of the file, some static text and the old version of the file, so what I do is ...
MortenSickel's user avatar
  • 1,373
11 votes
1 answer
780 views

Bash 4: unexpected EOF while looking for matching `)'

This very simplified version of my script #!/usr/bin/env bash example="$(bash -rs <<'BASH' -- 'This has been executed in restricted shell' echo "$1" BASH )" echo "$...
pedral's user avatar
  • 121
5 votes
3 answers
555 views

"Here Document" as a Single Line Command?

I have this command to run a checksum in some software I want to install: echo "85762db0edc00ce19a2cd5496d1627903e6198ad850bbbdefb2ceaa46bd20cbd install.sh" | sha256sum -c But I would like ...
Cnnewb's user avatar
  • 51
5 votes
1 answer
300 views

How can I pass variables to a file printed from outside a bash script?

Assuming I have a variable v="c d e f g" and a file with the content: line1 $v line3 How can I make a bash script print this file with the content of the variable in line 2 as if I was ...
nath's user avatar
  • 5,804
0 votes
1 answer
69 views

How to combine ...if $x and a here-doc in perl? [closed]

How is one supposed to write this? print <<EOF; if $x bla EOF I think it is called postfix notation, and a here-doc. I get a syntax error. OK, I guess I'll just use print "bla" if $x; ...
Dan Jacobson's user avatar
0 votes
2 answers
47 views

Testing for dir via ssh script - nothing happens

I usually run server scripts like this, with a here-document. But when I test if a directory exists, the script continues even though the test fails -- why? ssh $user@$ip /bin/bash <<SCRIPT if [...
Vinn's user avatar
  • 186
0 votes
0 answers
502 views

How to use heredoc inside my ssh command

I have a bash script that connects to a remote machine via SSH and runs a series of commands. It seems that the heredoc within the outer script is not aligning properly, and I keep getting an error. -...
George Udosen's user avatar
0 votes
2 answers
96 views

How to pass a script-file to sed as a heredoc?

This converts 'a' to 'A'. sed 's/a/A/g' <(echo "foobar") foobAr This supplies the commands necessary to make each vowel upper-case: $ cat << EOF s/a/A/g s/e/E/g s/i/I/g s/o/O/g s/u/...
Erwann's user avatar
  • 667
0 votes
1 answer
700 views

How can I supply a heredoc to podman run?

When I run buildah, I typically use a heredoc, ctr=$(buildah from alpine:3); buildah run $ctr sh -- <<EOF apk update; apk add git; EOF buildah commit $ctr heredoc_demo; However, when I ...
Evan Carroll's user avatar
  • 31.7k
1 vote
1 answer
1k views

Is it possible to run sudo with HEREDOC?

Edit: Amended examples after @Kusalananda answer. My whole picture is more complex. I need multiple commands and a variable definition) I can't use su because I'm not root and I don't want to input a ...
Mat M's user avatar
  • 143
1 vote
1 answer
133 views

How can I concat results in the here document?

I have this script: while read $item; do # Some bash logic here done <<< "$({ cat /path/to/some/file; echo; })" Now I want to also use find to find the name of some directories,...
Saeed Neamati's user avatar
0 votes
1 answer
44 views

When would someone choose to use a limit string (<<-) instead of format here strings the normal way with <<<?

The - option to mark a here document limit string (<<-LimitString) will suppress leading tabs (but not spaces) in the output. What I'm wondering is why someone would want to remove the leading ...
thinksinbinary's user avatar
0 votes
1 answer
579 views

Unable to capture exit code of a script ran inside of a heredoc

I was playing around and got stuck while trying to capture the exit code of a script I'm running within a heredoc in a Docker container. Say I want to run this command to start a Docker container ...
Parsia Hedayat's user avatar
0 votes
1 answer
111 views

looping array inside shell script with a different user using HEREDOC doesnt work

I want to perform the set of actions inside a different user while running my script, so i am using HEREDOC to perform such actions, but no matter alternatives i try, i cannot able to reference my ...
Alapati Srikanth Chowdary's user avatar
3 votes
2 answers
645 views

Can I use Bash Scripting Logic within Here Document?

I have a list of 50 Linux servers that I need to SSH into and check that a key-value pair configuration parameter within a .conf file is set. I need to check that the key-value pair exists. If the key-...
g9s0x1's user avatar
  • 31
0 votes
1 answer
481 views

Why this loop is executed for an empty set?

Create an empty directory, then run this code: find /empty_directory -mindepth 1 -maxdepth 1 -type d | while read dir; do echo "Simple find: $dir" done printf '%s\n' '-----' ...
Saeed Neamati's user avatar
1 vote
2 answers
306 views

How to make a script fail when there is an error in here string?

I have a script similar to this: #!/bin/bash set -euo pipefail IFS=$'\n\t' while read -r l; do echo "${l}" done <<< "$(cat input.txt)" echo Success The command cat ...
Thomas Leplus's user avatar
0 votes
1 answer
623 views

lost variable from bash in yaml gitlab ci

Need help I am studying bash and gitlab can't get it to work gitlab pipeline .gitlab-ci.yml variables: var1: test1 var2: test2 stages: - build build-job: stage: build script: ...
tuytuy20's user avatar
3 votes
1 answer
946 views

Run a shell script in a console session without saving it to file

I have a question about shell scripting on Linux. I am trying to run this: $ sh << EOF #!/bin/sh function test { echo test echo test2 } test EOF The result is: test test2 But what if I want ...
tuytuy20's user avatar
0 votes
1 answer
318 views

Add heredoc (<<) to end of alias within function call

I have an alias which is my psql connection string for a database, let's say the alias is this: alias GQQ='psql "host=$host user=$redshift_uname dbname=$redshift_dbname port=$port pass word=$...
amstergc20's user avatar
0 votes
1 answer
635 views

Using heredoc to a character device in bash script. EOF error

The script: #!/bin/bash interface=enp4s0 mac_address=$(ip link show $interface | grep ether | awk '{print $2}') cat > /dev/usb/lp0 <<EOF SIZE 30 mm,90 mm,CLS,TEXT 200,40,0,90,2,2,"MAC ...
Cruise5's user avatar
  • 526
0 votes
2 answers
118 views

How to create heredocs on RAM instead of disk?

Saving following script in a file: cat <<EOF | wc -l $HOME abc $(hostname) EOF and executing it gives the following error: $ ./a.sh ./a.sh: line 1: cannot create temp file for here-document: ...
ceremcem's user avatar
  • 2,361
3 votes
1 answer
459 views

How to send heredoc command to background?

I want to send a heredoc command like this one cat <<EOF line 1 line 2 EOF to background. (The actual command is openssl with many lines of input which takes some time to complete). The command ...
Greendrake's user avatar
1 vote
2 answers
455 views

run command with STDIN available as root (but without root-password manual insert) inside a bash script

I want to run command as root inside a bash script. This command needs to be able to read from STDIN. I have a root password as a local variable inside bash script and don't want to enter it manually. ...
vdshb's user avatar
  • 113
0 votes
1 answer
46 views

Unexpand the script before executing for handling here document

Let's consider the following code #!/bin/bash var=$( cat <<-EOF Foo Bar EOF ) echo "${var}" This works as expected with tabs, i.e the var variable ...
GypsyCosmonaut's user avatar
0 votes
1 answer
280 views

using heredoc in case statement for remote ssh host

I have a list of ssh commands to run on remote server, so wrote this snippet. #!/usr/bin/env bash echo '' echo "WOULD YOU LIKE TO PERFORM FILES CLEANUP: " read choice case $choice in yes|...
Sollosa's user avatar
  • 1,951
0 votes
1 answer
205 views

Iterating through a command list using heredoc

This works fine: $ eval 'echo "1" > ~/Desktop/in/foo' But not this: $ while IFS= read _cmd; do eval "$_cmd"; done < <(cat << EOF 'echo "1" > ~/Desktop/...
Erwann's user avatar
  • 667
0 votes
2 answers
943 views

How to use awk $1 $2 in bash shell?

cat << EOF > test.sh cat $FILEA $FILEB | sort -k1,1 -k2,2n | mergeBed -i - | awk -F\\t '{print $1 "\t" NR "\t\t" $2 "\t" $3 "\t\t.\t\t" NR}' > ...
user503698's user avatar
1 vote
1 answer
77 views

Here-document with two variables

Trying two variables with a here-document to generate a list of bash scripts. I ran into the problem that I cannot put the two variables into the script properly and the output file (only one file) ...
Molly_K's user avatar
  • 161
4 votes
1 answer
2k views

sed: replace multi-line block with standard input

Given is input_file: 1 2 START foo END 3 Goal is to replace content inside including START ... END block with multi-line content from stdin: 1 2 hello world 3 What I tried: sed '/^START$/,/^END$/d;...
cohorte's user avatar
  • 43
10 votes
5 answers
6k views

How does "cat << EOF | grep" work in bash?

In this answer to How does "cat << EOF" work in bash? on Stack Overflow, I get the first two points. But I don't get the third point Pass multi-line string to a pipe in Bash Pass ...
jian's user avatar
  • 577
0 votes
2 answers
321 views

Date command not printing inside Heredoc in linux Shell script

I have a shell script where i want to print date which is inside a heredoc. Below is the code snippet. echo "This is a text" echo I want date to print inside heredoc <<EOF var1=`date` ...
Shubhro's user avatar
-1 votes
1 answer
685 views

ssh, heredoc and environment variables [duplicate]

(This is RHEL 8.4, if it matters.) The echo commands on lines 5 and 7 in this script run on the local host, but the hostname commands on lines 6 and 8 run on the remote host as expected. Why do lines ...
RonJohn's user avatar
  • 1,134
23 votes
2 answers
11k views

How do you output a multi-line string that includes slashes and other special characters?

I would like to output this on completion of my bash script. /\_/\ ( o.o ) > ^ < I have tried the following but all return errors. echo /\\_/\\\n\( o.o \)\n > ^ < echo \/\\_\/\\\r\n( o....
James Geddes's user avatar
0 votes
1 answer
254 views

Heredoc not working in bash script

I made a bash script that was working in Ubuntu mate but now it won't work in Manjaro. It fails on this method: #!/bin/bash dev_block="/dev/sdb" createPartitions(){ echo "...
FlexMcMurphy's user avatar
1 vote
0 answers
170 views

Appending Multiple Lines Into a File But Skip If They Already Exist

I deal with a lot of config files when I'm working with Linux and because I like automating the setup process I make good use of creating automated shell scripts. I want to find a fast and efficient ...
willowen100's user avatar
12 votes
2 answers
3k views

Bash : how to pass arguments to a heredoc script

Imagine you have a silly script test.sh to which arguments are passed that would look like this: bash test.sh arg1 arg2 arg3 with test.sh being a silly script that displays its command line: #!/bin/...
creatldd1 creatldd1's user avatar
0 votes
3 answers
6k views

After ssh to remote hosts, grep used inside EOF gives output "No such file or directory"

Scenario: I have a script to connect to remote hosts and grep specific keyword from a log file. Without using EOF the ssh was getting freezed but when used heredoc (EOF) that issue was gone. All my ...
MayD's user avatar
  • 133
2 votes
1 answer
23k views

warning: here-document at line 2 delimited by end-of-file (wanted `EOF')

I've read a lot here and then but still cannot find how to fix this: steps.wrap([$class: 'AnsiColorBuildWrapper', 'colorMapName': 'XTerm']) { steps.withCredentials([steps.usernamePassword(...
WhoAmI's user avatar
  • 179
1 vote
1 answer
292 views

Tabs not being removed by heredoc

I'm trying to use heredoc with wget using the following code: xargs wget <<-EOF https://example\ .com EOF Replit link (yes, I'm already using tabs as required by heredoc) But for ...
what the's user avatar
  • 113
0 votes
1 answer
266 views

Bash Script Execute ViM Command is Seemingly Not Interpreting the '$' Command

I'm trying to move a certain ViM command into a Linux Bash script. In ViM the command is :g/^SOMEWORD$/d To delete any lines containing only some word and nothing else. When I move it into a bash ...
TheLovelySausage's user avatar
4 votes
1 answer
2k views

Bash script : Cat multiple lines in file containing “$” characters [duplicate]

I'm doing a script to auto-install some stuff including a nginx web server. I'm creating the nginx conf file like this : cat >/etc/nginx/sites-available/bookstack.conf <<EOL server { listen ...
Darktortue's user avatar
0 votes
2 answers
407 views

How to append a Linux command line to file?

I am writing a bash script which will generate a Vagrantfile. The reason that i use a bash script to generate Vagrantfile is to enable my colleague to use a single script in setting up their ...
hellojoshhhy's user avatar
1 vote
1 answer
41 views

Why does my program continuously execute when supplying an argument with "<<<"?

I have a compiled COTS program (.exe) that I normally interact with via stdin (manual keyboard inputs). Instead, I'm trying to use the <<< to supply input via string instead. Normally I call ...
DyslexicHobo's user avatar
4 votes
1 answer
349 views

dc and standard input or heredoc with string command?

dc can read command from a file or/and from standard input. If I want to print user input : cat essai_dc [enter a number : ] n ? p dc essai_dc enter a number : 4 4 Now, if I try with a heredoc :...
ctac_'s user avatar
  • 1,960
3 votes
1 answer
3k views

Using single quotes inside input of heredocs

I need some help debugging my tcsh script. It uses heredocs. The code: <pre_setup> <<EOF1 <setup> <<EOF2 <command> exit 0 EOF2 exit 0 EOF1 The <pre_setup> is ...
vesii's user avatar
  • 203
0 votes
1 answer
1k views

Call for a HereDoc inside a Bash Shell loop?

Regarding this post How to include python script inside a bash script , I get used to work with python HereDoc. However, I want to include that python HereDoc inside a for loop, and so, it needs to be ...
PARCB's user avatar
  • 1
3 votes
1 answer
164 views

Heredoc in subfunction in exported function fails

This works: #!/bin/bash foo() { gen() { cat <<EOF bar EOF true } gen } foo export -f foo bash -c foo This fails in the very last line: #!/bin/bash foo() { gen(...
Ole Tange's user avatar
  • 35.8k
1 vote
2 answers
907 views

Bash doesn't output here-documents to file

This bash script seems broken: #!/bin/bash echo "It prints ok" cat << 'EOF' > ~/doFoo.sh echo 'nested script doing Foo' EOF echo "It never prints" cat << '...
logan46's user avatar
  • 21
1 vote
0 answers
61 views

"cat << limit_string > filename" vs "cat > filename << limit_string"

When using here documents to write multi-line messages to file, in bash, does it matter in which order we apply the here-document (<<) and output redirect (>) operators? In other words, is ...
djvg's user avatar
  • 111

1
2 3 4 5