Questions tagged [here-document]
230 questions
131
votes
3 answers
How do you use output redirection in combination with here-documents and cat?
Let's say I have a script that I want to pipe to another command or redirect to a file (piping to sh for the examples). Assume that I'm using bash.
I could do it using echo:
echo "touch somefile
echo foo > somefile" | sh
I could also do almost the…
strugee
- 14,723
- 17
- 73
- 119
118
votes
9 answers
Can't indent heredoc to match code block's indentation
If there's a "First World Problems" for scripting, this would be it.
I have the following code in a script I'm updating:
if [ $diffLines -eq 1 ]; then
dateLastChanged=$(stat --format '%y' /.bbdata | awk '{print $1" "$2}' | sed…
Bratchley
- 16,684
- 13
- 64
- 103
39
votes
1 answer
passing and setting variables in a heredoc
I have a script that has to do many different things on many different remote machines. I thought that a heredoc would work for this, but I am not able to use a variable defined elsewhere in the script and one defined in the heredoc.
Here is some…
trubliphone
- 503
- 1
- 4
- 6
30
votes
3 answers
How to execute code in a new tmux session - from within current session?
I use Ubuntu 16.04 and I need the following tmux solution because I want to run a timeout process with sleep as in my particular case I wasn't satisfied from at and encountered a bug with nohup (when combining nohup-sleep). Now, tmux seems as best…
user149572
23
votes
2 answers
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.o )\r\n > ^ <
echo /\\_/\\\n\( o.o \)\n \> ^ <
How do I…
James Geddes
- 341
- 2
- 7
23
votes
2 answers
Is it possible to use multiple here-docs in bash?
Can one use multiple here-docs to provide input to a command in bash?
$ cat << foo
> EOF1
> bar
> EOF2
bar
Obviously, in both cases, the second here-doc is used as stdin, and replaces the first reference. Is the…
Sparhawk
- 19,561
- 18
- 86
- 152
22
votes
1 answer
What does POSIX require for quoted here documents inside command substitution?
In How do I bring HEREDOC text into a shell script variable? someone reports a problem using a here document with a quoted delimiter word inside $(...) command substitution, where a backslash \ at the end of a line inside the document triggers…
Michael Homer
- 74,824
- 17
- 212
- 233
21
votes
3 answers
bash script error stty: standard input: Inappropriate ioctl for device
I'm using here-documents in a bash script to automate installation and setup where a password is required many times. I enter the password once and the script passes it to the various commands. In most instances the here-document approach handles…
MountainX
- 17,168
- 59
- 155
- 264
20
votes
3 answers
Use sudo tee with heredoc to append to existing file
From Generate script in bash and save it to location requiring sudo we have this method, which I like:
sudo tee "$OUTFILE" > /dev/null <<'EOF'
foo
bar
EOF
However, I would like to use that approach to append to an existing file $OUTFILE. The above…
MountainX
- 17,168
- 59
- 155
- 264
18
votes
3 answers
How to combine Bash's process substitution with HERE-document?
In Bash version 4.2.47(1)-release when I try to catenate formatted text that comes from a HERE-dcoument like so:
cat <(fmt --width=10 <
Tim Friske
- 2,190
- 3
- 23
- 36
15
votes
4 answers
Why is the JSON content from heredoc not parsable?
I have a JSON fragment.
The following does not work:
VALUE=<
Jim
- 1,353
- 4
- 19
- 27
14
votes
3 answers
How to understand "cat > file_name << blah" command?
In following command cat takes the content of here-doc and redirects it to file named conf:
cat > conf << EOF
var1="cat"
var2="dog"
var3="hamster"
EOF
How to understand the order of commands here? Does bash first processes everything else(here-doc…
Martin
- 7,284
- 40
- 125
- 208
13
votes
5 answers
Using a here-doc for `sed` and a file
I want to use a here-doc for sed commands and provide the file to be read and the output file.
I've looked at Here-Documents from Advanced Bash Scripting guide but it does not mention anything about regular arguments in using a here-doc. Is it even…
Tristian
- 1,499
- 2
- 13
- 13
13
votes
2 answers
Passing a variable to a bash script that uses 'EOF' and considers the variable a literal
in this script i end up with "$1" being saved to the /test file.
#!/bin/bash
cat > /test << 'EOF'
$1
EOF
the truth is.. i need to keep
'EOF'
as 'EOF' because my argument($1) contains dollar signs.
but i need that argument to be saved rather than…
user72685
- 293
- 3
- 4
- 9
12
votes
3 answers
bash: Some issue when using read <<<"$VARIABLE" on a read-only root partition. Any known workarounds?
Just by coincidence I had to use my ATA-ID-to-device-name script (found here: https://serverfault.com/questions/244944/linux-ata-errors-translating-to-a-device-name/426561#426561) on a read-only / partition. In case you're curious, it was an Ubuntu…
syntaxerror
- 2,236
- 2
- 27
- 49