How to escape the null character in here-document?(bash and/or dash)How to disable emacs here document...
Why do members of Congress in committee hearings ask witnesses the same question multiple times?
Why don't American passenger airlines operate dedicated cargo flights any more?
What is the wife of a henpecked husband called?
Parsing a string of key-value pairs as a dictionary
We are very unlucky in my court
Dilemma of explaining to interviewer that he is the reason for declining second interview
How to acknowledge an embarrassing job interview, now that I work directly with the interviewer?
How would one buy a used TIE Fighter or X-Wing?
What's a good word to describe a public place that looks like it wouldn't be rough?
What is the in-universe cost of a TIE fighter?
Does Windows 10's telemetry include sending *.doc files if Word crashed?
If I sold a PS4 game I owned the disc for, can I reinstall it digitally?
Word or phrase for showing great skill at something without formal training in it
What is this metal M-shaped device for?
Can I become debt free or should I file for bankruptcy? How do I manage my debt and finances?
How do you funnel food off a cutting board?
Do authors have to be politically correct in article-writing?
The effects of magnetism in radio transmissions
It took me a lot of time to make this, pls like. (YouTube Comments #1)
How to explain planetary rings pulsating?
Quenching swords in dragon blood; why?
A minimum of two personnel "are" or "is"?
Lick explanation
Am I a Rude Number?
How to escape the null character in here-document?(bash and/or dash)
How to disable emacs here document completionHow to print “$” in here-documentHow to combine Bash's process substitution with HERE-document?history - bash, dash, zsh and .profileWhat are the bash shell length limitations for here-docs?Null and escape charactersIs it possible to use ANSI color escape codes in Bash here-documents?How to escape $ in here-documentWhat are the differences between here document and here string in their purposes?Dash - how to escape strange path characters
I want to perform shuf --zero-terminated
on multi-line strings with here-document.
bash dash
add a comment |
I want to perform shuf --zero-terminated
on multi-line strings with here-document.
bash dash
2
I don't think you can do this in a here-document in either of those shells. Shells are generally not good with nulls (except zsh). Why do you want a here-document specifically?
– Michael Homer
23 hours ago
@MichaelHomer Because I can't pass the multi-line strings entity(i.e string include the newline character) toshuf
as one single entity.
– illiterate
22 hours ago
add a comment |
I want to perform shuf --zero-terminated
on multi-line strings with here-document.
bash dash
I want to perform shuf --zero-terminated
on multi-line strings with here-document.
bash dash
bash dash
edited 10 hours ago
Rui F Ribeiro
40.7k1479137
40.7k1479137
asked 23 hours ago
illiterateilliterate
390112
390112
2
I don't think you can do this in a here-document in either of those shells. Shells are generally not good with nulls (except zsh). Why do you want a here-document specifically?
– Michael Homer
23 hours ago
@MichaelHomer Because I can't pass the multi-line strings entity(i.e string include the newline character) toshuf
as one single entity.
– illiterate
22 hours ago
add a comment |
2
I don't think you can do this in a here-document in either of those shells. Shells are generally not good with nulls (except zsh). Why do you want a here-document specifically?
– Michael Homer
23 hours ago
@MichaelHomer Because I can't pass the multi-line strings entity(i.e string include the newline character) toshuf
as one single entity.
– illiterate
22 hours ago
2
2
I don't think you can do this in a here-document in either of those shells. Shells are generally not good with nulls (except zsh). Why do you want a here-document specifically?
– Michael Homer
23 hours ago
I don't think you can do this in a here-document in either of those shells. Shells are generally not good with nulls (except zsh). Why do you want a here-document specifically?
– Michael Homer
23 hours ago
@MichaelHomer Because I can't pass the multi-line strings entity(i.e string include the newline character) to
shuf
as one single entity.– illiterate
22 hours ago
@MichaelHomer Because I can't pass the multi-line strings entity(i.e string include the newline character) to
shuf
as one single entity.– illiterate
22 hours ago
add a comment |
3 Answers
3
active
oldest
votes
Here-documents in Bash and dash don't support this. You can't store a null in a variable, they are removed from command substitutions, you can't write one in literally, and you can't use ANSI-C quoting inside the here-document. Neither shell is null-friendly and they are generally treated as (C-style) string terminators if one does get in.
You have a few options: use a real file, use zsh, use process substitution, or use standard input.
You can do exactly what you want in zsh, which is much more null-friendly.
zsh% null=$(printf 'x00')
zsh% hexdump -C <<EOT
heredoc> a${null}b${null}
heredoc> EOT
00000000 61 00 62 00 0a |a.b..|
00000005
Note though that heredocs have an implicit terminating newline, which may not be desirable (it'll be an extra field for shuf
after the final null).
For Bash, you can use process substitution almost equivalently to your heredoc in combination with printf
or echo -e
to create nulls inline:
bash$ hexdump -C < <(
printf 'item 1x00itemn2x00'
)
00000000 69 74 65 6d 20 31 00 69 74 65 6d 0a 32 00 |item 1.item.2.|
0000000e
This is not necessarily entirely equivalent to a here-document, because those are often secretly put into real files by the shell (which matters for seekability, among other things).
Since you probably want to suppress terminating newlines, you can't even use a heredoc internally within the commands there - it has to be printf
/echo -ne
if safe to get fine-grained control over the output.
You can't do process substitution in dash, but in any shell you could pipe in standard input from a subshell:
dash$ (
printf 'item 1x00'
printf 'itemn2x00'
) | hexdump -C
00000000 69 74 65 6d 20 31 00 69 74 65 6d 0a 32 00 |item 1.item.2.|
0000000e
shuf
is happy to read from standard input by default, so that should work for your concrete use case as I understand it. If you have a more complex command, being on the right-hand side of a pipeline can introduce some confounding elements with scoping.
Finally, you could write your data into a real file using printf
and use that instead of a here-document. That option has been covered in the other answer. You'll need to make sure you clean up the file afterwards, and may want to use mktemp
or similar if available to create a safe filename if there are any live security concerns.
Thank you, this is one elaborate answer, but lack the simple script work in bash and dash
– illiterate
17 hours ago
add a comment |
Thank you all.
Let I post one answer base on you all and maybe best for me.
This script works nicely in bash and dash, no require real file or process substitution in bash, no require an extra slow external program call, even you don't need to worry about any escape problem in entities as %s
in C printf, but you should still take care to string escape in your shell itself.
#!/bin/sh
printf '%s' "[tag1]
key1=value1
key2=value2
[/tag1]
" "[tag2]
key3=value3
key4=value4
[/tag2]
" | shuf --zero-terminated
#also see man printf(1)
For shuf
only(not intent to general here-document alternative):
shuf --echo "[tag1]
key1=value1
key2=value2
[/tag1]" "[tag2]
key3=value3
key4=value4
[/tag2]"
5
Don't useecho
for that, not even/bin/echo
(on GNU systems for instance, whether/bin/echo
supports-e
depends on whether$POSIXLY_CORRECT
is in the environment or not, on many other systems,/bin/echo
doesn't support-e
or-n
orx00
). Useprintf '%s' 'first record' 'second record'... | shuf...
– Stéphane Chazelas
19 hours ago
add a comment |
I do not think you can do what you want in a heredoc. However it is trivial to do using echo
as the following example shows:
$ cat demo
#!/bin/bash
echo -ne "one" > outfile
echo -ne "two" >> outfile
echo -ne "three" >> outfile
$ ./demo
$ od -a outfile
0000000 o n e nul t w o nul t h r e e nul
0000016
$
1
Thank you, butecho
is not trivial, see my answer
– illiterate
19 hours ago
@fpmurphy In addition to Stephane's excellent comment on illiterate's answer, see also the APPLICATION USAGE and RATIONALE sections of the POSIX spec forecho
.
– Charles Duffy
10 hours ago
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f503682%2fhow-to-escape-the-null-character-in-here-documentbash-and-or-dash%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Here-documents in Bash and dash don't support this. You can't store a null in a variable, they are removed from command substitutions, you can't write one in literally, and you can't use ANSI-C quoting inside the here-document. Neither shell is null-friendly and they are generally treated as (C-style) string terminators if one does get in.
You have a few options: use a real file, use zsh, use process substitution, or use standard input.
You can do exactly what you want in zsh, which is much more null-friendly.
zsh% null=$(printf 'x00')
zsh% hexdump -C <<EOT
heredoc> a${null}b${null}
heredoc> EOT
00000000 61 00 62 00 0a |a.b..|
00000005
Note though that heredocs have an implicit terminating newline, which may not be desirable (it'll be an extra field for shuf
after the final null).
For Bash, you can use process substitution almost equivalently to your heredoc in combination with printf
or echo -e
to create nulls inline:
bash$ hexdump -C < <(
printf 'item 1x00itemn2x00'
)
00000000 69 74 65 6d 20 31 00 69 74 65 6d 0a 32 00 |item 1.item.2.|
0000000e
This is not necessarily entirely equivalent to a here-document, because those are often secretly put into real files by the shell (which matters for seekability, among other things).
Since you probably want to suppress terminating newlines, you can't even use a heredoc internally within the commands there - it has to be printf
/echo -ne
if safe to get fine-grained control over the output.
You can't do process substitution in dash, but in any shell you could pipe in standard input from a subshell:
dash$ (
printf 'item 1x00'
printf 'itemn2x00'
) | hexdump -C
00000000 69 74 65 6d 20 31 00 69 74 65 6d 0a 32 00 |item 1.item.2.|
0000000e
shuf
is happy to read from standard input by default, so that should work for your concrete use case as I understand it. If you have a more complex command, being on the right-hand side of a pipeline can introduce some confounding elements with scoping.
Finally, you could write your data into a real file using printf
and use that instead of a here-document. That option has been covered in the other answer. You'll need to make sure you clean up the file afterwards, and may want to use mktemp
or similar if available to create a safe filename if there are any live security concerns.
Thank you, this is one elaborate answer, but lack the simple script work in bash and dash
– illiterate
17 hours ago
add a comment |
Here-documents in Bash and dash don't support this. You can't store a null in a variable, they are removed from command substitutions, you can't write one in literally, and you can't use ANSI-C quoting inside the here-document. Neither shell is null-friendly and they are generally treated as (C-style) string terminators if one does get in.
You have a few options: use a real file, use zsh, use process substitution, or use standard input.
You can do exactly what you want in zsh, which is much more null-friendly.
zsh% null=$(printf 'x00')
zsh% hexdump -C <<EOT
heredoc> a${null}b${null}
heredoc> EOT
00000000 61 00 62 00 0a |a.b..|
00000005
Note though that heredocs have an implicit terminating newline, which may not be desirable (it'll be an extra field for shuf
after the final null).
For Bash, you can use process substitution almost equivalently to your heredoc in combination with printf
or echo -e
to create nulls inline:
bash$ hexdump -C < <(
printf 'item 1x00itemn2x00'
)
00000000 69 74 65 6d 20 31 00 69 74 65 6d 0a 32 00 |item 1.item.2.|
0000000e
This is not necessarily entirely equivalent to a here-document, because those are often secretly put into real files by the shell (which matters for seekability, among other things).
Since you probably want to suppress terminating newlines, you can't even use a heredoc internally within the commands there - it has to be printf
/echo -ne
if safe to get fine-grained control over the output.
You can't do process substitution in dash, but in any shell you could pipe in standard input from a subshell:
dash$ (
printf 'item 1x00'
printf 'itemn2x00'
) | hexdump -C
00000000 69 74 65 6d 20 31 00 69 74 65 6d 0a 32 00 |item 1.item.2.|
0000000e
shuf
is happy to read from standard input by default, so that should work for your concrete use case as I understand it. If you have a more complex command, being on the right-hand side of a pipeline can introduce some confounding elements with scoping.
Finally, you could write your data into a real file using printf
and use that instead of a here-document. That option has been covered in the other answer. You'll need to make sure you clean up the file afterwards, and may want to use mktemp
or similar if available to create a safe filename if there are any live security concerns.
Thank you, this is one elaborate answer, but lack the simple script work in bash and dash
– illiterate
17 hours ago
add a comment |
Here-documents in Bash and dash don't support this. You can't store a null in a variable, they are removed from command substitutions, you can't write one in literally, and you can't use ANSI-C quoting inside the here-document. Neither shell is null-friendly and they are generally treated as (C-style) string terminators if one does get in.
You have a few options: use a real file, use zsh, use process substitution, or use standard input.
You can do exactly what you want in zsh, which is much more null-friendly.
zsh% null=$(printf 'x00')
zsh% hexdump -C <<EOT
heredoc> a${null}b${null}
heredoc> EOT
00000000 61 00 62 00 0a |a.b..|
00000005
Note though that heredocs have an implicit terminating newline, which may not be desirable (it'll be an extra field for shuf
after the final null).
For Bash, you can use process substitution almost equivalently to your heredoc in combination with printf
or echo -e
to create nulls inline:
bash$ hexdump -C < <(
printf 'item 1x00itemn2x00'
)
00000000 69 74 65 6d 20 31 00 69 74 65 6d 0a 32 00 |item 1.item.2.|
0000000e
This is not necessarily entirely equivalent to a here-document, because those are often secretly put into real files by the shell (which matters for seekability, among other things).
Since you probably want to suppress terminating newlines, you can't even use a heredoc internally within the commands there - it has to be printf
/echo -ne
if safe to get fine-grained control over the output.
You can't do process substitution in dash, but in any shell you could pipe in standard input from a subshell:
dash$ (
printf 'item 1x00'
printf 'itemn2x00'
) | hexdump -C
00000000 69 74 65 6d 20 31 00 69 74 65 6d 0a 32 00 |item 1.item.2.|
0000000e
shuf
is happy to read from standard input by default, so that should work for your concrete use case as I understand it. If you have a more complex command, being on the right-hand side of a pipeline can introduce some confounding elements with scoping.
Finally, you could write your data into a real file using printf
and use that instead of a here-document. That option has been covered in the other answer. You'll need to make sure you clean up the file afterwards, and may want to use mktemp
or similar if available to create a safe filename if there are any live security concerns.
Here-documents in Bash and dash don't support this. You can't store a null in a variable, they are removed from command substitutions, you can't write one in literally, and you can't use ANSI-C quoting inside the here-document. Neither shell is null-friendly and they are generally treated as (C-style) string terminators if one does get in.
You have a few options: use a real file, use zsh, use process substitution, or use standard input.
You can do exactly what you want in zsh, which is much more null-friendly.
zsh% null=$(printf 'x00')
zsh% hexdump -C <<EOT
heredoc> a${null}b${null}
heredoc> EOT
00000000 61 00 62 00 0a |a.b..|
00000005
Note though that heredocs have an implicit terminating newline, which may not be desirable (it'll be an extra field for shuf
after the final null).
For Bash, you can use process substitution almost equivalently to your heredoc in combination with printf
or echo -e
to create nulls inline:
bash$ hexdump -C < <(
printf 'item 1x00itemn2x00'
)
00000000 69 74 65 6d 20 31 00 69 74 65 6d 0a 32 00 |item 1.item.2.|
0000000e
This is not necessarily entirely equivalent to a here-document, because those are often secretly put into real files by the shell (which matters for seekability, among other things).
Since you probably want to suppress terminating newlines, you can't even use a heredoc internally within the commands there - it has to be printf
/echo -ne
if safe to get fine-grained control over the output.
You can't do process substitution in dash, but in any shell you could pipe in standard input from a subshell:
dash$ (
printf 'item 1x00'
printf 'itemn2x00'
) | hexdump -C
00000000 69 74 65 6d 20 31 00 69 74 65 6d 0a 32 00 |item 1.item.2.|
0000000e
shuf
is happy to read from standard input by default, so that should work for your concrete use case as I understand it. If you have a more complex command, being on the right-hand side of a pipeline can introduce some confounding elements with scoping.
Finally, you could write your data into a real file using printf
and use that instead of a here-document. That option has been covered in the other answer. You'll need to make sure you clean up the file afterwards, and may want to use mktemp
or similar if available to create a safe filename if there are any live security concerns.
answered 21 hours ago
Michael HomerMichael Homer
49.4k8133172
49.4k8133172
Thank you, this is one elaborate answer, but lack the simple script work in bash and dash
– illiterate
17 hours ago
add a comment |
Thank you, this is one elaborate answer, but lack the simple script work in bash and dash
– illiterate
17 hours ago
Thank you, this is one elaborate answer, but lack the simple script work in bash and dash
– illiterate
17 hours ago
Thank you, this is one elaborate answer, but lack the simple script work in bash and dash
– illiterate
17 hours ago
add a comment |
Thank you all.
Let I post one answer base on you all and maybe best for me.
This script works nicely in bash and dash, no require real file or process substitution in bash, no require an extra slow external program call, even you don't need to worry about any escape problem in entities as %s
in C printf, but you should still take care to string escape in your shell itself.
#!/bin/sh
printf '%s' "[tag1]
key1=value1
key2=value2
[/tag1]
" "[tag2]
key3=value3
key4=value4
[/tag2]
" | shuf --zero-terminated
#also see man printf(1)
For shuf
only(not intent to general here-document alternative):
shuf --echo "[tag1]
key1=value1
key2=value2
[/tag1]" "[tag2]
key3=value3
key4=value4
[/tag2]"
5
Don't useecho
for that, not even/bin/echo
(on GNU systems for instance, whether/bin/echo
supports-e
depends on whether$POSIXLY_CORRECT
is in the environment or not, on many other systems,/bin/echo
doesn't support-e
or-n
orx00
). Useprintf '%s' 'first record' 'second record'... | shuf...
– Stéphane Chazelas
19 hours ago
add a comment |
Thank you all.
Let I post one answer base on you all and maybe best for me.
This script works nicely in bash and dash, no require real file or process substitution in bash, no require an extra slow external program call, even you don't need to worry about any escape problem in entities as %s
in C printf, but you should still take care to string escape in your shell itself.
#!/bin/sh
printf '%s' "[tag1]
key1=value1
key2=value2
[/tag1]
" "[tag2]
key3=value3
key4=value4
[/tag2]
" | shuf --zero-terminated
#also see man printf(1)
For shuf
only(not intent to general here-document alternative):
shuf --echo "[tag1]
key1=value1
key2=value2
[/tag1]" "[tag2]
key3=value3
key4=value4
[/tag2]"
5
Don't useecho
for that, not even/bin/echo
(on GNU systems for instance, whether/bin/echo
supports-e
depends on whether$POSIXLY_CORRECT
is in the environment or not, on many other systems,/bin/echo
doesn't support-e
or-n
orx00
). Useprintf '%s' 'first record' 'second record'... | shuf...
– Stéphane Chazelas
19 hours ago
add a comment |
Thank you all.
Let I post one answer base on you all and maybe best for me.
This script works nicely in bash and dash, no require real file or process substitution in bash, no require an extra slow external program call, even you don't need to worry about any escape problem in entities as %s
in C printf, but you should still take care to string escape in your shell itself.
#!/bin/sh
printf '%s' "[tag1]
key1=value1
key2=value2
[/tag1]
" "[tag2]
key3=value3
key4=value4
[/tag2]
" | shuf --zero-terminated
#also see man printf(1)
For shuf
only(not intent to general here-document alternative):
shuf --echo "[tag1]
key1=value1
key2=value2
[/tag1]" "[tag2]
key3=value3
key4=value4
[/tag2]"
Thank you all.
Let I post one answer base on you all and maybe best for me.
This script works nicely in bash and dash, no require real file or process substitution in bash, no require an extra slow external program call, even you don't need to worry about any escape problem in entities as %s
in C printf, but you should still take care to string escape in your shell itself.
#!/bin/sh
printf '%s' "[tag1]
key1=value1
key2=value2
[/tag1]
" "[tag2]
key3=value3
key4=value4
[/tag2]
" | shuf --zero-terminated
#also see man printf(1)
For shuf
only(not intent to general here-document alternative):
shuf --echo "[tag1]
key1=value1
key2=value2
[/tag1]" "[tag2]
key3=value3
key4=value4
[/tag2]"
edited 17 hours ago
answered 19 hours ago
illiterateilliterate
390112
390112
5
Don't useecho
for that, not even/bin/echo
(on GNU systems for instance, whether/bin/echo
supports-e
depends on whether$POSIXLY_CORRECT
is in the environment or not, on many other systems,/bin/echo
doesn't support-e
or-n
orx00
). Useprintf '%s' 'first record' 'second record'... | shuf...
– Stéphane Chazelas
19 hours ago
add a comment |
5
Don't useecho
for that, not even/bin/echo
(on GNU systems for instance, whether/bin/echo
supports-e
depends on whether$POSIXLY_CORRECT
is in the environment or not, on many other systems,/bin/echo
doesn't support-e
or-n
orx00
). Useprintf '%s' 'first record' 'second record'... | shuf...
– Stéphane Chazelas
19 hours ago
5
5
Don't use
echo
for that, not even /bin/echo
(on GNU systems for instance, whether /bin/echo
supports -e
depends on whether $POSIXLY_CORRECT
is in the environment or not, on many other systems, /bin/echo
doesn't support -e
or -n
or x00
). Use printf '%s' 'first record' 'second record'... | shuf...
– Stéphane Chazelas
19 hours ago
Don't use
echo
for that, not even /bin/echo
(on GNU systems for instance, whether /bin/echo
supports -e
depends on whether $POSIXLY_CORRECT
is in the environment or not, on many other systems, /bin/echo
doesn't support -e
or -n
or x00
). Use printf '%s' 'first record' 'second record'... | shuf...
– Stéphane Chazelas
19 hours ago
add a comment |
I do not think you can do what you want in a heredoc. However it is trivial to do using echo
as the following example shows:
$ cat demo
#!/bin/bash
echo -ne "one" > outfile
echo -ne "two" >> outfile
echo -ne "three" >> outfile
$ ./demo
$ od -a outfile
0000000 o n e nul t w o nul t h r e e nul
0000016
$
1
Thank you, butecho
is not trivial, see my answer
– illiterate
19 hours ago
@fpmurphy In addition to Stephane's excellent comment on illiterate's answer, see also the APPLICATION USAGE and RATIONALE sections of the POSIX spec forecho
.
– Charles Duffy
10 hours ago
add a comment |
I do not think you can do what you want in a heredoc. However it is trivial to do using echo
as the following example shows:
$ cat demo
#!/bin/bash
echo -ne "one" > outfile
echo -ne "two" >> outfile
echo -ne "three" >> outfile
$ ./demo
$ od -a outfile
0000000 o n e nul t w o nul t h r e e nul
0000016
$
1
Thank you, butecho
is not trivial, see my answer
– illiterate
19 hours ago
@fpmurphy In addition to Stephane's excellent comment on illiterate's answer, see also the APPLICATION USAGE and RATIONALE sections of the POSIX spec forecho
.
– Charles Duffy
10 hours ago
add a comment |
I do not think you can do what you want in a heredoc. However it is trivial to do using echo
as the following example shows:
$ cat demo
#!/bin/bash
echo -ne "one" > outfile
echo -ne "two" >> outfile
echo -ne "three" >> outfile
$ ./demo
$ od -a outfile
0000000 o n e nul t w o nul t h r e e nul
0000016
$
I do not think you can do what you want in a heredoc. However it is trivial to do using echo
as the following example shows:
$ cat demo
#!/bin/bash
echo -ne "one" > outfile
echo -ne "two" >> outfile
echo -ne "three" >> outfile
$ ./demo
$ od -a outfile
0000000 o n e nul t w o nul t h r e e nul
0000016
$
answered 22 hours ago
fpmurphyfpmurphy
2,446915
2,446915
1
Thank you, butecho
is not trivial, see my answer
– illiterate
19 hours ago
@fpmurphy In addition to Stephane's excellent comment on illiterate's answer, see also the APPLICATION USAGE and RATIONALE sections of the POSIX spec forecho
.
– Charles Duffy
10 hours ago
add a comment |
1
Thank you, butecho
is not trivial, see my answer
– illiterate
19 hours ago
@fpmurphy In addition to Stephane's excellent comment on illiterate's answer, see also the APPLICATION USAGE and RATIONALE sections of the POSIX spec forecho
.
– Charles Duffy
10 hours ago
1
1
Thank you, but
echo
is not trivial, see my answer– illiterate
19 hours ago
Thank you, but
echo
is not trivial, see my answer– illiterate
19 hours ago
@fpmurphy In addition to Stephane's excellent comment on illiterate's answer, see also the APPLICATION USAGE and RATIONALE sections of the POSIX spec for
echo
.– Charles Duffy
10 hours ago
@fpmurphy In addition to Stephane's excellent comment on illiterate's answer, see also the APPLICATION USAGE and RATIONALE sections of the POSIX spec for
echo
.– Charles Duffy
10 hours ago
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f503682%2fhow-to-escape-the-null-character-in-here-documentbash-and-or-dash%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
I don't think you can do this in a here-document in either of those shells. Shells are generally not good with nulls (except zsh). Why do you want a here-document specifically?
– Michael Homer
23 hours ago
@MichaelHomer Because I can't pass the multi-line strings entity(i.e string include the newline character) to
shuf
as one single entity.– illiterate
22 hours ago