Questions tagged [here-document]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
0 votes
1 answer
15 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|...
user avatar
  • 1,619
0 votes
1 answer
48 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/...
user avatar
  • 481
0 votes
2 answers
78 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}' > ...
user avatar
1 vote
1 answer
53 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) ...
user avatar
  • 61
4 votes
1 answer
170 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;...
user avatar
  • 43
8 votes
4 answers
2k 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 ...
user avatar
  • 411
0 votes
2 answers
73 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` ...
user avatar
-1 votes
1 answer
149 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 ...
user avatar
  • 838
20 votes
2 answers
1k 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....
user avatar
0 votes
1 answer
55 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 "...
user avatar
1 vote
0 answers
36 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 ...
user avatar
10 votes
2 answers
773 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/...
user avatar
0 votes
3 answers
839 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 ...
user avatar
  • 93
1 vote
1 answer
4k 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(...
user avatar
  • 149
1 vote
1 answer
74 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 ...
user avatar
  • 111
0 votes
1 answer
86 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 ...
user avatar
3 votes
1 answer
154 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 ...
user avatar
0 votes
2 answers
125 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 ...
user avatar
  • 185
1 vote
1 answer
39 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 ...
user avatar
4 votes
1 answer
113 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 :...
user avatar
  • 1,832
1 vote
1 answer
692 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 ...
user avatar
  • 63
0 votes
1 answer
471 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 ...
user avatar
  • 1
4 votes
1 answer
94 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(...
user avatar
  • 29.6k
1 vote
2 answers
287 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 << '...
user avatar
  • 21
1 vote
0 answers
51 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 ...
user avatar
  • 111
5 votes
3 answers
1k 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 ...
user avatar
  • 2,647
1 vote
3 answers
150 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 ...
user avatar
4 votes
1 answer
197 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 ...
user avatar
1 vote
1 answer
350 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. ...
user avatar
  • 513
4 votes
0 answers
72 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 ...
user avatar
1 vote
3 answers
224 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, ...
user avatar
  • 91
0 votes
0 answers
35 views

Running commands remotely and script not exiting on error

I have a simple script which takes the first argument as a hostname and tries to connect to it and run some commands then saves the output locally. But whenever I pass an invalid hostname and it ...
user avatar
  • 15
0 votes
2 answers
836 views

SFTP with redirection and here doc has issues under Cron

The script below works fine when running from command line. I also put in a Cron Job, and I know the Cron Job is running because I see entries in the sftpFileLog.txt. But when running under Cron Job,...
user avatar
3 votes
2 answers
9k views

What is different between "<<-EOF" and "<<EOF" in bash script? [duplicate]

I did google this topic and all results are talking about <<EOF. But I saw scripts using <<-EOF, but I can not find anywehre by googling. Thus, What is different between <<-EOF and &...
user avatar
  • 267
0 votes
1 answer
356 views

How to use heredoc in a wget command in a shell script once the inserted text is not the last argument?

My problem: when testing an application I use wget to send a JSON to my local Tomcat. Summing up the command: wget --header='...' --post-data='LONG_JSON' -q0 URL Expanding the command: wget --...
user avatar
0 votes
0 answers
22 views

here-doc couldn't parse backtick [duplicate]

Why does this works bold="$(tput bold)" reset="$(tput sgr0)" c0="${reset}${bold}" cat <<EOF ${c0} \` EOF but not this one? bold="$(tput bold)" reset="$(tput sgr0)" c0="${reset}${bold}" cat &...
user avatar
  • 1
-1 votes
4 answers
281 views

What is the difference between a heredoc and process substitution?

In this answer, it is explained how to use a heredoc to interact with interactive programs by connecting their stdin with a heredoc. https://stackoverflow.com/questions/21130757/send-commands-to-...
user avatar
0 votes
1 answer
60 views

End here document anywhere

As title suggests, is it possible to end a here document anywhere, and not only on a new line? Usually heredocs are used as follow: $ cat << 'EOF' First line Seconde line EOF And the output ...
user avatar
  • 826
0 votes
2 answers
1k views

Using an here document variable in a while loop

I am having trouble using a Here Document variable in a loop. Because this works while IFS= read -r line; do echo "(${line})" done <<EOF one two three EOF But this doesn't foo=<<EOF ...
user avatar
  • 1,438
1 vote
1 answer
621 views

Creating nginx configuration files with script and variable expansion

I'm trying to create a nginx configuration file using a here-document with variable expansion. I use: cat <<EOF > /etc/nginx/nginx.conf settings EOF It works and expands the variables I ...
user avatar
  • 1,035
0 votes
1 answer
63 views

Bash here document: redirecting the input vs typing it directly

$ cat<<EOF > a > EOF a Can I automatically redirect the stream vs typing into stdin? I have tried but this is what I get. [debian:~]$ cat <<(printf "EOF\na\nEOF\n") -bash: syntax ...
user avatar
  • 193
1 vote
1 answer
19 views

unbundling a file of here docs when file's directory is different from pwd

All, I have a file (called, say, bundled_file.txt) of here docs stored in a directory, and the file looks like this: cat > a.txt << 'eof' ... ... ... 'eof' cat > b.txt << 'eof' ......
user avatar
  • 11
1 vote
3 answers
2k views

How to cat multiple lines with indentation in bash function?

I have a bash function yumtelegraf() { cat <<EOF | sudo tee /etc/yum.repos.d/influxdb.repo [influxdb] name = InfluxDB Repository - RHEL \$releasever baseurl = https://repos.influxdata.com/rhel/\...
user avatar
  • 922
4 votes
1 answer
983 views

How to redirect to a file from a heredoc

I have this: cat <<EOF 'use strict'; exports.default = { 'display names': {} } EOF > "$PWD/tmp.js" but this is syntactically wrong apparently, anybody know the syntax to do this?
user avatar
0 votes
1 answer
160 views

Why cat command redirect a new file get executed right away?

I want to generate a bash file that could be called later, like this cat <<-SCRIPT >test-$$ #!/bin/bash osType=$(grep -Po '^NAME="\K[^"]*' /etc/os-release) if [ "$...
user avatar
  • 327
2 votes
2 answers
3k views

preserving backslash-newlines in heredocs run in a subshell

So long as terminal backslashes are not present, the newlines of a heredoc are preserved after capturing the output of a heredoc in a variable: var=$(cat <<-EOF a b c EOF ) echo "$var" a b c ...
user avatar
  • 3,530
0 votes
0 answers
75 views

Source and save a script from a heredoc

I'd like a script to be able to make a copy of itself so it can perform cron tasks, but getting the location of a script from within itself is quite unreliable in posix. A better method would be to ...
user avatar
  • 272
-1 votes
1 answer
960 views

What is the hyphen (‐) used for in this command tee <<-'EOF'? [duplicate]

I saw the following command: sudo tee /etc/docker/daemon.json <<-'EOF' { "registry-mirrors": ["https://xxxxxxxx.mirror.aliyuncs.com"] } EOF What is the hyphen (‐) used for? It works fine ...
user avatar
1 vote
1 answer
35 views

Predefined Software installation inputs using Here document

I want to install software on a CentOS machine and I want to automate the inputs requested by the installation process: I am trying using heredoc but seems not working. using something like this ...
user avatar
  • 177
1 vote
1 answer
276 views

set -u finds unset value in heredoc in bash script [duplicate]

I have a script that I've simplified here: #!/bin/bash # test.sh set -u ### test-script ### cat <<SCRIPT > ~/test-file-output #!/bin/bash set -e usage() { cat <<EOF Does a thing ...
user avatar
  • 745

1
2 3 4 5