Questions tagged [here-document]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
0 votes
0 answers
206 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
62 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
  • 645
0 votes
1 answer
365 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
  • 29.6k
1 vote
1 answer
766 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
  • 131
1 vote
1 answer
92 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
38 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
358 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
81 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
310 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
205 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
0 votes
2 answers
191 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
457 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
671 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
207 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
467 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
  • 466
0 votes
2 answers
100 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,271
3 votes
1 answer
316 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
337 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
41 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
219 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,899
0 votes
1 answer
178 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
  • 645
0 votes
2 answers
760 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
71 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
1k 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
5k 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
  • 547
0 votes
2 answers
266 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
608 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,016
23 votes
2 answers
7k 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
204 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
113 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
10 votes
2 answers
2k 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
5k 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
19k 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
  • 169
1 vote
1 answer
224 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
257 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
1k 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
329 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
282 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
2k 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
  • 193
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
156 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
  • 34.7k
1 vote
2 answers
778 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
59 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
5 votes
3 answers
3k views

Is there a way to use HEREDOC for Bash and Zsh, and be able to use arguments?

Bash and Zsh's HEREDOC seems to act like a file, instead of string, and if I hope to do something like foo() { ruby << 'EOF' 3.times do puts "Ruby is getting the ...
nonopolarity's user avatar
  • 3,009
1 vote
3 answers
444 views

Variables inside heredoc with multiple files

I have a heredoc with xml content, and I add the content of another file like this: bar file: <bar> $baz </bar> the script: bar=$(cat bar.xml) # this would be the ideal, of course it ...
schrodingerscatcuriosity's user avatar
3 votes
1 answer
253 views

Strange behavior of nested heredoc

Sorry if dup. I couldn't find any similar issue here, though. I accidentally found this strange behavior. Could anyone explain this logic? Why parsed as 'ABC'? $ cat repro.sh bash -s <<-EOS1 ...
m.pigeon's user avatar
1 vote
1 answer
1k views

Why does read appear to fail silently in my script called from another script?

Let me paint a picture for your. You write some deployment scripts or build scripts or somethings etc. which run commands on the remote to do things like e.g. create users, install/update package etc. ...
Timo's user avatar
  • 633
4 votes
0 answers
99 views

${$} working fine in ksh93 but ${$} not working when I use in cat << EOF

I'm executing this in the ksh93 shell. If I use normally like ~# echo ${$} 1295 but using ${$} in a here-document is not working: ~#cat <<EOF ~#> ${$} ~#> EOF syntax error: `$' unexpected ...
Saikiran Marupaka's user avatar
1 vote
3 answers
701 views

Passing huge amounts of data as input to a shell command

Using a bash shell operated through a pipe (non-interactive), I'm trying to pass huge amounts of data to a shell command. So far, I cannot get it to work reliably. For example, using a here document, ...
ternary's user avatar
  • 101

1
2 3 4 5