How to read input lines with newline characters from command line?shell: read: differentiate between EOF and...
How could an armless race establish civilization?
Different budgets within roommate group
How receiver knows the exact frequency in the channel to "listen to"?
How is this practical and very old scene shot?
How do I ensure my employees don't abuse my flexible work hours policy?
How Do I Know When I am in Private Mode?
Security Patch SUPEE-11155 - Possible issues?
Can a nowhere continuous function have a connected graph?
Why wasn't EBCDIC designed with contiguous alphanumeric characters?
What will happen if I checked in for another room in the same hotel, but not for the booked one?
Divergent Series & Continued Fraction (from Gauss' Mathematical Diary)
"save as image" no confirmation when overwriting
What is "oversubscription" in Networking?
If two black hole event horizons overlap (touch) can they ever separate again?
for xml path('') output
13th chords on guitar
Comment traduire « That screams X »
Company threatening to call my current job after I declined their offer
Closest Proximity of Oceans to Freshwater Springs
Does Latin have any neuter words for humans?
Two palindromes are not enough
I hit a pipe with a mower and now it won't turn
Is there reliable evidence that depleted uranium from the 1999 NATO bombing is causing cancer in Serbia?
Using “ser” without "un/una"?
How to read input lines with newline characters from command line?
shell: read: differentiate between EOF and newlinebash: read: how to capture 'n' (newline) character?What does the -p option do in the read command?Read arguments separated by newlineBash newline doesn't printprocessing command output line by line, without mixing standard inputHow to combine Bash's read with HERE-document when shopt -os errexit is in place?How is a trace of commands different from shell input lines?How to signal end-of-input to “read -N”?How to use linebreak in read -p command while using variables in prompt stringHow to make a prefill line with read command?how to check if the first line of file contain a specific string?How to bind a key in bash to a function that modifies the current command lineIs there a character as `delim` of `read`, so that `read` reads the entire of a file at once?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}
I found this to get user input from the command line. But it is failing into recognizing the new line characters I put into the input. Doing:
#!/bin/bash
read -e -p "Multiline input=" variable;
printf "'variable=%s'" "${variable}";
- Typing
'multinline'
onMultiline input=
makesprintf
output'variable=multinline'
- Typing
'multi\nline'
onMultiline input=
makesprintf
output'variable=multinline'
How printf
can print the new line I read by read -p
, i.e., output
multi
line
Instead of multinline
or multinline
?
Related questions:
- What does the -p option do in the read command?
- bash: read: how to capture 'n' (newline) character?
- shell: read: differentiate between EOF and newline
- https://stackoverflow.com/questions/4296108/how-do-i-add-a-line-break-for-read-command
- Read arguments separated by newline
- https://stackoverflow.com/questions/43190306/how-to-add-new-line-after-user-input-in-shell-scripting
bash shell posix input read
add a comment |
I found this to get user input from the command line. But it is failing into recognizing the new line characters I put into the input. Doing:
#!/bin/bash
read -e -p "Multiline input=" variable;
printf "'variable=%s'" "${variable}";
- Typing
'multinline'
onMultiline input=
makesprintf
output'variable=multinline'
- Typing
'multi\nline'
onMultiline input=
makesprintf
output'variable=multinline'
How printf
can print the new line I read by read -p
, i.e., output
multi
line
Instead of multinline
or multinline
?
Related questions:
- What does the -p option do in the read command?
- bash: read: how to capture 'n' (newline) character?
- shell: read: differentiate between EOF and newline
- https://stackoverflow.com/questions/4296108/how-do-i-add-a-line-break-for-read-command
- Read arguments separated by newline
- https://stackoverflow.com/questions/43190306/how-to-add-new-line-after-user-input-in-shell-scripting
bash shell posix input read
add a comment |
I found this to get user input from the command line. But it is failing into recognizing the new line characters I put into the input. Doing:
#!/bin/bash
read -e -p "Multiline input=" variable;
printf "'variable=%s'" "${variable}";
- Typing
'multinline'
onMultiline input=
makesprintf
output'variable=multinline'
- Typing
'multi\nline'
onMultiline input=
makesprintf
output'variable=multinline'
How printf
can print the new line I read by read -p
, i.e., output
multi
line
Instead of multinline
or multinline
?
Related questions:
- What does the -p option do in the read command?
- bash: read: how to capture 'n' (newline) character?
- shell: read: differentiate between EOF and newline
- https://stackoverflow.com/questions/4296108/how-do-i-add-a-line-break-for-read-command
- Read arguments separated by newline
- https://stackoverflow.com/questions/43190306/how-to-add-new-line-after-user-input-in-shell-scripting
bash shell posix input read
I found this to get user input from the command line. But it is failing into recognizing the new line characters I put into the input. Doing:
#!/bin/bash
read -e -p "Multiline input=" variable;
printf "'variable=%s'" "${variable}";
- Typing
'multinline'
onMultiline input=
makesprintf
output'variable=multinline'
- Typing
'multi\nline'
onMultiline input=
makesprintf
output'variable=multinline'
How printf
can print the new line I read by read -p
, i.e., output
multi
line
Instead of multinline
or multinline
?
Related questions:
- What does the -p option do in the read command?
- bash: read: how to capture 'n' (newline) character?
- shell: read: differentiate between EOF and newline
- https://stackoverflow.com/questions/4296108/how-do-i-add-a-line-break-for-read-command
- Read arguments separated by newline
- https://stackoverflow.com/questions/43190306/how-to-add-new-line-after-user-input-in-shell-scripting
bash shell posix input read
bash shell posix input read
asked 48 mins ago
useruser
1561 silver badge11 bronze badges
1561 silver badge11 bronze badges
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
If typing in n
(as in the two characters and
n
) is acceptable, then you can use printf
to interpret it:
#!/bin/bash
IFS= read -rep "Multiline input=" variable;
printf -v variable "%b" "$variable"
printf "'variable=%s'n" "${variable}";
For example:
~ ./foo.sh
Multiline input=foonbar
'variable=foo
bar'
From the bash manual:
The backslash character ‘’ may be used to remove any special meaning
for the next character read and for line continuation.
The "line continuation" bit seems to imply you can't escape newlines unless you use a different character as the line delimiter.
if already usingbash
, then instead ofprintf %b ...
the OP could use justvariable=${variable//\n/$'n'}
(and unlikeprintf -v
, this will also work in ksh and mksh).
– mosvy
3 mins 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%2f527170%2fhow-to-read-input-lines-with-newline-characters-from-command-line%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
If typing in n
(as in the two characters and
n
) is acceptable, then you can use printf
to interpret it:
#!/bin/bash
IFS= read -rep "Multiline input=" variable;
printf -v variable "%b" "$variable"
printf "'variable=%s'n" "${variable}";
For example:
~ ./foo.sh
Multiline input=foonbar
'variable=foo
bar'
From the bash manual:
The backslash character ‘’ may be used to remove any special meaning
for the next character read and for line continuation.
The "line continuation" bit seems to imply you can't escape newlines unless you use a different character as the line delimiter.
if already usingbash
, then instead ofprintf %b ...
the OP could use justvariable=${variable//\n/$'n'}
(and unlikeprintf -v
, this will also work in ksh and mksh).
– mosvy
3 mins ago
add a comment |
If typing in n
(as in the two characters and
n
) is acceptable, then you can use printf
to interpret it:
#!/bin/bash
IFS= read -rep "Multiline input=" variable;
printf -v variable "%b" "$variable"
printf "'variable=%s'n" "${variable}";
For example:
~ ./foo.sh
Multiline input=foonbar
'variable=foo
bar'
From the bash manual:
The backslash character ‘’ may be used to remove any special meaning
for the next character read and for line continuation.
The "line continuation" bit seems to imply you can't escape newlines unless you use a different character as the line delimiter.
if already usingbash
, then instead ofprintf %b ...
the OP could use justvariable=${variable//\n/$'n'}
(and unlikeprintf -v
, this will also work in ksh and mksh).
– mosvy
3 mins ago
add a comment |
If typing in n
(as in the two characters and
n
) is acceptable, then you can use printf
to interpret it:
#!/bin/bash
IFS= read -rep "Multiline input=" variable;
printf -v variable "%b" "$variable"
printf "'variable=%s'n" "${variable}";
For example:
~ ./foo.sh
Multiline input=foonbar
'variable=foo
bar'
From the bash manual:
The backslash character ‘’ may be used to remove any special meaning
for the next character read and for line continuation.
The "line continuation" bit seems to imply you can't escape newlines unless you use a different character as the line delimiter.
If typing in n
(as in the two characters and
n
) is acceptable, then you can use printf
to interpret it:
#!/bin/bash
IFS= read -rep "Multiline input=" variable;
printf -v variable "%b" "$variable"
printf "'variable=%s'n" "${variable}";
For example:
~ ./foo.sh
Multiline input=foonbar
'variable=foo
bar'
From the bash manual:
The backslash character ‘’ may be used to remove any special meaning
for the next character read and for line continuation.
The "line continuation" bit seems to imply you can't escape newlines unless you use a different character as the line delimiter.
answered 27 mins ago
murumuru
40.5k5 gold badges98 silver badges172 bronze badges
40.5k5 gold badges98 silver badges172 bronze badges
if already usingbash
, then instead ofprintf %b ...
the OP could use justvariable=${variable//\n/$'n'}
(and unlikeprintf -v
, this will also work in ksh and mksh).
– mosvy
3 mins ago
add a comment |
if already usingbash
, then instead ofprintf %b ...
the OP could use justvariable=${variable//\n/$'n'}
(and unlikeprintf -v
, this will also work in ksh and mksh).
– mosvy
3 mins ago
if already using
bash
, then instead of printf %b ...
the OP could use just variable=${variable//\n/$'n'}
(and unlike printf -v
, this will also work in ksh and mksh).– mosvy
3 mins ago
if already using
bash
, then instead of printf %b ...
the OP could use just variable=${variable//\n/$'n'}
(and unlike printf -v
, this will also work in ksh and mksh).– mosvy
3 mins 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%2f527170%2fhow-to-read-input-lines-with-newline-characters-from-command-line%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