How to correctly store a newline character in a (POSIX) shell variable? [duplicate]
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
This question already has an answer here:
How can I set an environment variable which contains newline characters?
3 answers
update attempt to clarify more, using an example, does the following shell commands:
SHELLVARIABLE="1st line,
2nd line,
3rd line,
"
printf '%s' "$SHELLVARIABLE"
generate this output:
1st line,<newline>2nd line,<newline>3rd line,<newline>
<newline>
being the character 0x0a
/ n
?
Original question formulation
What is the correct (POSIX-confirm) way to store a newline character 0x0a
(aka known by its commen c style escap n
) into a shell variable.
I want to make sure that doing the following:
SHELLVARIABLE="
"
is, not merely working by chance but instead is indeed the correct way.
Since POSIX's printf is perfectly capable of producing a newline character (i.e. printf 'n'
, or printf '%b' '012'
) I first attempted a more explicit (? or correct?) form:
SHELLVARIABLE="$(printf 'n')"
though a tempting approach, does not work. As the according to the standard, command substitution (i.e. via $()
and ` `
) shall remove
sequences of one or more newlines at the end of the substitution.
Note: Asking to store a single trailing newline into a shell variable is only to abstract the more general use case (i indeed seek answer to) that is how to store a string into POSIX shell variable which ends with the newline character.
shell posix newlines
marked as duplicate by Thomas Dickey, Rui F Ribeiro, peterh, RalfFriedl, Community⦠Sep 8 at 19:38
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |Â
up vote
1
down vote
favorite
This question already has an answer here:
How can I set an environment variable which contains newline characters?
3 answers
update attempt to clarify more, using an example, does the following shell commands:
SHELLVARIABLE="1st line,
2nd line,
3rd line,
"
printf '%s' "$SHELLVARIABLE"
generate this output:
1st line,<newline>2nd line,<newline>3rd line,<newline>
<newline>
being the character 0x0a
/ n
?
Original question formulation
What is the correct (POSIX-confirm) way to store a newline character 0x0a
(aka known by its commen c style escap n
) into a shell variable.
I want to make sure that doing the following:
SHELLVARIABLE="
"
is, not merely working by chance but instead is indeed the correct way.
Since POSIX's printf is perfectly capable of producing a newline character (i.e. printf 'n'
, or printf '%b' '012'
) I first attempted a more explicit (? or correct?) form:
SHELLVARIABLE="$(printf 'n')"
though a tempting approach, does not work. As the according to the standard, command substitution (i.e. via $()
and ` `
) shall remove
sequences of one or more newlines at the end of the substitution.
Note: Asking to store a single trailing newline into a shell variable is only to abstract the more general use case (i indeed seek answer to) that is how to store a string into POSIX shell variable which ends with the newline character.
shell posix newlines
marked as duplicate by Thomas Dickey, Rui F Ribeiro, peterh, RalfFriedl, Community⦠Sep 8 at 19:38
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
Not absolutely sure I understand the Q, but you may find something useful at para 3.243 in this document. As I read all of this, <newline> is defined IAW the convention defined for the output device.
â Seamus
Sep 8 at 14:45
1
Related: unix.stackexchange.com/questions/20035/â¦
â Kusalananda
Sep 8 at 15:23
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
This question already has an answer here:
How can I set an environment variable which contains newline characters?
3 answers
update attempt to clarify more, using an example, does the following shell commands:
SHELLVARIABLE="1st line,
2nd line,
3rd line,
"
printf '%s' "$SHELLVARIABLE"
generate this output:
1st line,<newline>2nd line,<newline>3rd line,<newline>
<newline>
being the character 0x0a
/ n
?
Original question formulation
What is the correct (POSIX-confirm) way to store a newline character 0x0a
(aka known by its commen c style escap n
) into a shell variable.
I want to make sure that doing the following:
SHELLVARIABLE="
"
is, not merely working by chance but instead is indeed the correct way.
Since POSIX's printf is perfectly capable of producing a newline character (i.e. printf 'n'
, or printf '%b' '012'
) I first attempted a more explicit (? or correct?) form:
SHELLVARIABLE="$(printf 'n')"
though a tempting approach, does not work. As the according to the standard, command substitution (i.e. via $()
and ` `
) shall remove
sequences of one or more newlines at the end of the substitution.
Note: Asking to store a single trailing newline into a shell variable is only to abstract the more general use case (i indeed seek answer to) that is how to store a string into POSIX shell variable which ends with the newline character.
shell posix newlines
This question already has an answer here:
How can I set an environment variable which contains newline characters?
3 answers
update attempt to clarify more, using an example, does the following shell commands:
SHELLVARIABLE="1st line,
2nd line,
3rd line,
"
printf '%s' "$SHELLVARIABLE"
generate this output:
1st line,<newline>2nd line,<newline>3rd line,<newline>
<newline>
being the character 0x0a
/ n
?
Original question formulation
What is the correct (POSIX-confirm) way to store a newline character 0x0a
(aka known by its commen c style escap n
) into a shell variable.
I want to make sure that doing the following:
SHELLVARIABLE="
"
is, not merely working by chance but instead is indeed the correct way.
Since POSIX's printf is perfectly capable of producing a newline character (i.e. printf 'n'
, or printf '%b' '012'
) I first attempted a more explicit (? or correct?) form:
SHELLVARIABLE="$(printf 'n')"
though a tempting approach, does not work. As the according to the standard, command substitution (i.e. via $()
and ` `
) shall remove
sequences of one or more newlines at the end of the substitution.
Note: Asking to store a single trailing newline into a shell variable is only to abstract the more general use case (i indeed seek answer to) that is how to store a string into POSIX shell variable which ends with the newline character.
This question already has an answer here:
How can I set an environment variable which contains newline characters?
3 answers
shell posix newlines
shell posix newlines
edited Sep 8 at 15:13
asked Sep 8 at 14:05
humanityANDpeace
4,63743350
4,63743350
marked as duplicate by Thomas Dickey, Rui F Ribeiro, peterh, RalfFriedl, Community⦠Sep 8 at 19:38
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Thomas Dickey, Rui F Ribeiro, peterh, RalfFriedl, Community⦠Sep 8 at 19:38
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
Not absolutely sure I understand the Q, but you may find something useful at para 3.243 in this document. As I read all of this, <newline> is defined IAW the convention defined for the output device.
â Seamus
Sep 8 at 14:45
1
Related: unix.stackexchange.com/questions/20035/â¦
â Kusalananda
Sep 8 at 15:23
add a comment |Â
Not absolutely sure I understand the Q, but you may find something useful at para 3.243 in this document. As I read all of this, <newline> is defined IAW the convention defined for the output device.
â Seamus
Sep 8 at 14:45
1
Related: unix.stackexchange.com/questions/20035/â¦
â Kusalananda
Sep 8 at 15:23
Not absolutely sure I understand the Q, but you may find something useful at para 3.243 in this document. As I read all of this, <newline> is defined IAW the convention defined for the output device.
â Seamus
Sep 8 at 14:45
Not absolutely sure I understand the Q, but you may find something useful at para 3.243 in this document. As I read all of this, <newline> is defined IAW the convention defined for the output device.
â Seamus
Sep 8 at 14:45
1
1
Related: unix.stackexchange.com/questions/20035/â¦
â Kusalananda
Sep 8 at 15:23
Related: unix.stackexchange.com/questions/20035/â¦
â Kusalananda
Sep 8 at 15:23
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
Using a hard newline in a quoted string is fine.
Though personally, I'd avoid it if you just want the newline: I find it looks odd and there's a slight risk of accidentally hitting extra spaces at the end of the first line (so you'd get e.g. <space><newline>
). But that's just me.
Command substitution indeed strips trailing newlines, but only those. The workaround is to make sure that the final character is something other than a newline and then strip that character off.
Both of these should give you a newline:
nl1='
'
nl2="$(printf 'nx')"
nl2="$nl2%x"
I am happy to read your answer confirming that I can use indeed a "hard newline" (is that how it is referred, when occring verbatitm in a thring?). Also great to have the alternative more explicit, yet overly "boilerplate" solution. Thank you. I would like to accept your answer, do you happen to know where there is a document/ref/spec that defines your given options?
â humanityANDpeace
Sep 8 at 15:18
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
Using a hard newline in a quoted string is fine.
Though personally, I'd avoid it if you just want the newline: I find it looks odd and there's a slight risk of accidentally hitting extra spaces at the end of the first line (so you'd get e.g. <space><newline>
). But that's just me.
Command substitution indeed strips trailing newlines, but only those. The workaround is to make sure that the final character is something other than a newline and then strip that character off.
Both of these should give you a newline:
nl1='
'
nl2="$(printf 'nx')"
nl2="$nl2%x"
I am happy to read your answer confirming that I can use indeed a "hard newline" (is that how it is referred, when occring verbatitm in a thring?). Also great to have the alternative more explicit, yet overly "boilerplate" solution. Thank you. I would like to accept your answer, do you happen to know where there is a document/ref/spec that defines your given options?
â humanityANDpeace
Sep 8 at 15:18
add a comment |Â
up vote
1
down vote
accepted
Using a hard newline in a quoted string is fine.
Though personally, I'd avoid it if you just want the newline: I find it looks odd and there's a slight risk of accidentally hitting extra spaces at the end of the first line (so you'd get e.g. <space><newline>
). But that's just me.
Command substitution indeed strips trailing newlines, but only those. The workaround is to make sure that the final character is something other than a newline and then strip that character off.
Both of these should give you a newline:
nl1='
'
nl2="$(printf 'nx')"
nl2="$nl2%x"
I am happy to read your answer confirming that I can use indeed a "hard newline" (is that how it is referred, when occring verbatitm in a thring?). Also great to have the alternative more explicit, yet overly "boilerplate" solution. Thank you. I would like to accept your answer, do you happen to know where there is a document/ref/spec that defines your given options?
â humanityANDpeace
Sep 8 at 15:18
add a comment |Â
up vote
1
down vote
accepted
up vote
1
down vote
accepted
Using a hard newline in a quoted string is fine.
Though personally, I'd avoid it if you just want the newline: I find it looks odd and there's a slight risk of accidentally hitting extra spaces at the end of the first line (so you'd get e.g. <space><newline>
). But that's just me.
Command substitution indeed strips trailing newlines, but only those. The workaround is to make sure that the final character is something other than a newline and then strip that character off.
Both of these should give you a newline:
nl1='
'
nl2="$(printf 'nx')"
nl2="$nl2%x"
Using a hard newline in a quoted string is fine.
Though personally, I'd avoid it if you just want the newline: I find it looks odd and there's a slight risk of accidentally hitting extra spaces at the end of the first line (so you'd get e.g. <space><newline>
). But that's just me.
Command substitution indeed strips trailing newlines, but only those. The workaround is to make sure that the final character is something other than a newline and then strip that character off.
Both of these should give you a newline:
nl1='
'
nl2="$(printf 'nx')"
nl2="$nl2%x"
answered Sep 8 at 15:12
ilkkachu
52.1k679144
52.1k679144
I am happy to read your answer confirming that I can use indeed a "hard newline" (is that how it is referred, when occring verbatitm in a thring?). Also great to have the alternative more explicit, yet overly "boilerplate" solution. Thank you. I would like to accept your answer, do you happen to know where there is a document/ref/spec that defines your given options?
â humanityANDpeace
Sep 8 at 15:18
add a comment |Â
I am happy to read your answer confirming that I can use indeed a "hard newline" (is that how it is referred, when occring verbatitm in a thring?). Also great to have the alternative more explicit, yet overly "boilerplate" solution. Thank you. I would like to accept your answer, do you happen to know where there is a document/ref/spec that defines your given options?
â humanityANDpeace
Sep 8 at 15:18
I am happy to read your answer confirming that I can use indeed a "hard newline" (is that how it is referred, when occring verbatitm in a thring?). Also great to have the alternative more explicit, yet overly "boilerplate" solution. Thank you. I would like to accept your answer, do you happen to know where there is a document/ref/spec that defines your given options?
â humanityANDpeace
Sep 8 at 15:18
I am happy to read your answer confirming that I can use indeed a "hard newline" (is that how it is referred, when occring verbatitm in a thring?). Also great to have the alternative more explicit, yet overly "boilerplate" solution. Thank you. I would like to accept your answer, do you happen to know where there is a document/ref/spec that defines your given options?
â humanityANDpeace
Sep 8 at 15:18
add a comment |Â
Not absolutely sure I understand the Q, but you may find something useful at para 3.243 in this document. As I read all of this, <newline> is defined IAW the convention defined for the output device.
â Seamus
Sep 8 at 14:45
1
Related: unix.stackexchange.com/questions/20035/â¦
â Kusalananda
Sep 8 at 15:23