The last command line in the shell script without a newline
Clash Royale CLAN TAG#URR8PPP
up vote
2
down vote
favorite
Why can the last command line in the shell script (uptime
below) be executed successfully without the newline at the end of the file?
For example:
root@jcdb:/tmp#
root@jcdb:/tmp#
root@jcdb:/tmp# cat dp
date
pwd
uptimeroot@jcdb:/tmp#
root@jcdb:/tmp#
root@jcdb:/tmp#
root@jcdb:/tmp# ./dp
Wed Jun 6 16:27:26 EAT 2018
/tmp
4:27pm up 1 day, 17:25, 2 users, load average: 0.39, 0.40, 0.41
root@jcdb:/tmp#
root@jcdb:/tmp#
shell-script
add a comment |Â
up vote
2
down vote
favorite
Why can the last command line in the shell script (uptime
below) be executed successfully without the newline at the end of the file?
For example:
root@jcdb:/tmp#
root@jcdb:/tmp#
root@jcdb:/tmp# cat dp
date
pwd
uptimeroot@jcdb:/tmp#
root@jcdb:/tmp#
root@jcdb:/tmp#
root@jcdb:/tmp# ./dp
Wed Jun 6 16:27:26 EAT 2018
/tmp
4:27pm up 1 day, 17:25, 2 users, load average: 0.39, 0.40, 0.41
root@jcdb:/tmp#
root@jcdb:/tmp#
shell-script
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
Why can the last command line in the shell script (uptime
below) be executed successfully without the newline at the end of the file?
For example:
root@jcdb:/tmp#
root@jcdb:/tmp#
root@jcdb:/tmp# cat dp
date
pwd
uptimeroot@jcdb:/tmp#
root@jcdb:/tmp#
root@jcdb:/tmp#
root@jcdb:/tmp# ./dp
Wed Jun 6 16:27:26 EAT 2018
/tmp
4:27pm up 1 day, 17:25, 2 users, load average: 0.39, 0.40, 0.41
root@jcdb:/tmp#
root@jcdb:/tmp#
shell-script
Why can the last command line in the shell script (uptime
below) be executed successfully without the newline at the end of the file?
For example:
root@jcdb:/tmp#
root@jcdb:/tmp#
root@jcdb:/tmp# cat dp
date
pwd
uptimeroot@jcdb:/tmp#
root@jcdb:/tmp#
root@jcdb:/tmp#
root@jcdb:/tmp# ./dp
Wed Jun 6 16:27:26 EAT 2018
/tmp
4:27pm up 1 day, 17:25, 2 users, load average: 0.39, 0.40, 0.41
root@jcdb:/tmp#
root@jcdb:/tmp#
shell-script
edited Jun 6 at 15:20
Kusalananda
101k13199312
101k13199312
asked Jun 6 at 14:42
lylklb
204
204
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
3
down vote
accepted
When the shell reads the shell script, its scanning code will accept the last word at the end of the script even if there is no terminating newline.
This is the effect of the first rule in the POSIX standard regarding how the shell should recognize tokens in its input:
If the end of input is recognized, the current token (if any) shall be delimited.
The fact that it say that the current token is delimited here means that the last word in the script, along with the rest of that non-terminated line, will be accepted as input to the interpreter rather than causing an error or putting the shell into an undefined state.
In comments, a reference is made to the vi
editor. A standard vi
editor is allowed to edit a file whose last line is not terminated by a newline. It will, however, terminate the last line of the buffer with a newline when writing it to file (except for when saving an empty file, in which case adding a newline is explicitly not allowed). This is also true for a standard ex
editor:
INPUT FILES
Input files shall be text files or files that would be text files except for an incomplete last line that is not longer than
LINE_MAX-1
bytes in length and contains noNUL
characters. By default, any incomplete last line shall be treated as if it had a trailing<newline>
. The editing of other forms of files may optionally be allowed byex
implementations.
The vim
editor has a setting, eol
, which determines whether it should preserve the non-terminated last line or not. The default behaviour is to terminate the last line. See :help eol
in vim
.
The input to the standard ed
editor is supposed to be a text file, which means that what happens if one is editing a file whose last line in not properly terminated (which makes it not a text file) is technically unspecified. On OpenBSD, the ed
editor will say "newline appended" when opening a non-terminated text file.
INPUT FILES
The input files shall be text files.
Thanksï¼ 1) Do all the types of shell, such as ksh/sh/csh/bash, conform to the POSIX standard rule ï¼ 2) But indeed the last command line is still regarded as a incomplete line while the script is opened with vi ï¼Â
â lylklb
Jun 7 at 2:10
@lylklb All of those shells, with the exception ofcsh
, are POSIX shells and should behave the same way with regards to this.csh
is not a POSIX shell, so you would have to test that individually. I don't think I understand your second question. Normally a "line" needs to be terminated by a newline, but since the POSIX parsing rules for shell scripts have this special rule, the last line of a shell script may remain unterminated.
â Kusalananda
Jun 7 at 6:03
1) I have tested it in the csh environment, and it also work fine w/o last newlineï¼ 2) It is just a message at the bottom of vi editor that prompts "Incomplete last line". I think that this symptom is mainly due to the non-POSIX compliant systems/editorsï¼Â
â lylklb
Jun 7 at 14:30
@lylklb 1) Good. 2) No it is not. The lines of a text file should all be delimited by a newline character. The parsing rules for shell script allows the last line to be unterminated. This allows the shell to execute scripts which are read from a command substitution, likesh -c "$(cat script)"
. The last line ofscript
will not be terminated by a newline when given to thesh
interpreter, since the command substitution removes it. Also, inprintf 'echo hello' | sh
the script (echo hello
) does not have a newline at the end.
â Kusalananda
Jun 7 at 14:49
Could you please tell me where can be found with more detail/documents about this parsing rule for the last line in shell scripts ï¼Â
â lylklb
Jun 8 at 2:08
 |Â
show 3 more comments
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
When the shell reads the shell script, its scanning code will accept the last word at the end of the script even if there is no terminating newline.
This is the effect of the first rule in the POSIX standard regarding how the shell should recognize tokens in its input:
If the end of input is recognized, the current token (if any) shall be delimited.
The fact that it say that the current token is delimited here means that the last word in the script, along with the rest of that non-terminated line, will be accepted as input to the interpreter rather than causing an error or putting the shell into an undefined state.
In comments, a reference is made to the vi
editor. A standard vi
editor is allowed to edit a file whose last line is not terminated by a newline. It will, however, terminate the last line of the buffer with a newline when writing it to file (except for when saving an empty file, in which case adding a newline is explicitly not allowed). This is also true for a standard ex
editor:
INPUT FILES
Input files shall be text files or files that would be text files except for an incomplete last line that is not longer than
LINE_MAX-1
bytes in length and contains noNUL
characters. By default, any incomplete last line shall be treated as if it had a trailing<newline>
. The editing of other forms of files may optionally be allowed byex
implementations.
The vim
editor has a setting, eol
, which determines whether it should preserve the non-terminated last line or not. The default behaviour is to terminate the last line. See :help eol
in vim
.
The input to the standard ed
editor is supposed to be a text file, which means that what happens if one is editing a file whose last line in not properly terminated (which makes it not a text file) is technically unspecified. On OpenBSD, the ed
editor will say "newline appended" when opening a non-terminated text file.
INPUT FILES
The input files shall be text files.
Thanksï¼ 1) Do all the types of shell, such as ksh/sh/csh/bash, conform to the POSIX standard rule ï¼ 2) But indeed the last command line is still regarded as a incomplete line while the script is opened with vi ï¼Â
â lylklb
Jun 7 at 2:10
@lylklb All of those shells, with the exception ofcsh
, are POSIX shells and should behave the same way with regards to this.csh
is not a POSIX shell, so you would have to test that individually. I don't think I understand your second question. Normally a "line" needs to be terminated by a newline, but since the POSIX parsing rules for shell scripts have this special rule, the last line of a shell script may remain unterminated.
â Kusalananda
Jun 7 at 6:03
1) I have tested it in the csh environment, and it also work fine w/o last newlineï¼ 2) It is just a message at the bottom of vi editor that prompts "Incomplete last line". I think that this symptom is mainly due to the non-POSIX compliant systems/editorsï¼Â
â lylklb
Jun 7 at 14:30
@lylklb 1) Good. 2) No it is not. The lines of a text file should all be delimited by a newline character. The parsing rules for shell script allows the last line to be unterminated. This allows the shell to execute scripts which are read from a command substitution, likesh -c "$(cat script)"
. The last line ofscript
will not be terminated by a newline when given to thesh
interpreter, since the command substitution removes it. Also, inprintf 'echo hello' | sh
the script (echo hello
) does not have a newline at the end.
â Kusalananda
Jun 7 at 14:49
Could you please tell me where can be found with more detail/documents about this parsing rule for the last line in shell scripts ï¼Â
â lylklb
Jun 8 at 2:08
 |Â
show 3 more comments
up vote
3
down vote
accepted
When the shell reads the shell script, its scanning code will accept the last word at the end of the script even if there is no terminating newline.
This is the effect of the first rule in the POSIX standard regarding how the shell should recognize tokens in its input:
If the end of input is recognized, the current token (if any) shall be delimited.
The fact that it say that the current token is delimited here means that the last word in the script, along with the rest of that non-terminated line, will be accepted as input to the interpreter rather than causing an error or putting the shell into an undefined state.
In comments, a reference is made to the vi
editor. A standard vi
editor is allowed to edit a file whose last line is not terminated by a newline. It will, however, terminate the last line of the buffer with a newline when writing it to file (except for when saving an empty file, in which case adding a newline is explicitly not allowed). This is also true for a standard ex
editor:
INPUT FILES
Input files shall be text files or files that would be text files except for an incomplete last line that is not longer than
LINE_MAX-1
bytes in length and contains noNUL
characters. By default, any incomplete last line shall be treated as if it had a trailing<newline>
. The editing of other forms of files may optionally be allowed byex
implementations.
The vim
editor has a setting, eol
, which determines whether it should preserve the non-terminated last line or not. The default behaviour is to terminate the last line. See :help eol
in vim
.
The input to the standard ed
editor is supposed to be a text file, which means that what happens if one is editing a file whose last line in not properly terminated (which makes it not a text file) is technically unspecified. On OpenBSD, the ed
editor will say "newline appended" when opening a non-terminated text file.
INPUT FILES
The input files shall be text files.
Thanksï¼ 1) Do all the types of shell, such as ksh/sh/csh/bash, conform to the POSIX standard rule ï¼ 2) But indeed the last command line is still regarded as a incomplete line while the script is opened with vi ï¼Â
â lylklb
Jun 7 at 2:10
@lylklb All of those shells, with the exception ofcsh
, are POSIX shells and should behave the same way with regards to this.csh
is not a POSIX shell, so you would have to test that individually. I don't think I understand your second question. Normally a "line" needs to be terminated by a newline, but since the POSIX parsing rules for shell scripts have this special rule, the last line of a shell script may remain unterminated.
â Kusalananda
Jun 7 at 6:03
1) I have tested it in the csh environment, and it also work fine w/o last newlineï¼ 2) It is just a message at the bottom of vi editor that prompts "Incomplete last line". I think that this symptom is mainly due to the non-POSIX compliant systems/editorsï¼Â
â lylklb
Jun 7 at 14:30
@lylklb 1) Good. 2) No it is not. The lines of a text file should all be delimited by a newline character. The parsing rules for shell script allows the last line to be unterminated. This allows the shell to execute scripts which are read from a command substitution, likesh -c "$(cat script)"
. The last line ofscript
will not be terminated by a newline when given to thesh
interpreter, since the command substitution removes it. Also, inprintf 'echo hello' | sh
the script (echo hello
) does not have a newline at the end.
â Kusalananda
Jun 7 at 14:49
Could you please tell me where can be found with more detail/documents about this parsing rule for the last line in shell scripts ï¼Â
â lylklb
Jun 8 at 2:08
 |Â
show 3 more comments
up vote
3
down vote
accepted
up vote
3
down vote
accepted
When the shell reads the shell script, its scanning code will accept the last word at the end of the script even if there is no terminating newline.
This is the effect of the first rule in the POSIX standard regarding how the shell should recognize tokens in its input:
If the end of input is recognized, the current token (if any) shall be delimited.
The fact that it say that the current token is delimited here means that the last word in the script, along with the rest of that non-terminated line, will be accepted as input to the interpreter rather than causing an error or putting the shell into an undefined state.
In comments, a reference is made to the vi
editor. A standard vi
editor is allowed to edit a file whose last line is not terminated by a newline. It will, however, terminate the last line of the buffer with a newline when writing it to file (except for when saving an empty file, in which case adding a newline is explicitly not allowed). This is also true for a standard ex
editor:
INPUT FILES
Input files shall be text files or files that would be text files except for an incomplete last line that is not longer than
LINE_MAX-1
bytes in length and contains noNUL
characters. By default, any incomplete last line shall be treated as if it had a trailing<newline>
. The editing of other forms of files may optionally be allowed byex
implementations.
The vim
editor has a setting, eol
, which determines whether it should preserve the non-terminated last line or not. The default behaviour is to terminate the last line. See :help eol
in vim
.
The input to the standard ed
editor is supposed to be a text file, which means that what happens if one is editing a file whose last line in not properly terminated (which makes it not a text file) is technically unspecified. On OpenBSD, the ed
editor will say "newline appended" when opening a non-terminated text file.
INPUT FILES
The input files shall be text files.
When the shell reads the shell script, its scanning code will accept the last word at the end of the script even if there is no terminating newline.
This is the effect of the first rule in the POSIX standard regarding how the shell should recognize tokens in its input:
If the end of input is recognized, the current token (if any) shall be delimited.
The fact that it say that the current token is delimited here means that the last word in the script, along with the rest of that non-terminated line, will be accepted as input to the interpreter rather than causing an error or putting the shell into an undefined state.
In comments, a reference is made to the vi
editor. A standard vi
editor is allowed to edit a file whose last line is not terminated by a newline. It will, however, terminate the last line of the buffer with a newline when writing it to file (except for when saving an empty file, in which case adding a newline is explicitly not allowed). This is also true for a standard ex
editor:
INPUT FILES
Input files shall be text files or files that would be text files except for an incomplete last line that is not longer than
LINE_MAX-1
bytes in length and contains noNUL
characters. By default, any incomplete last line shall be treated as if it had a trailing<newline>
. The editing of other forms of files may optionally be allowed byex
implementations.
The vim
editor has a setting, eol
, which determines whether it should preserve the non-terminated last line or not. The default behaviour is to terminate the last line. See :help eol
in vim
.
The input to the standard ed
editor is supposed to be a text file, which means that what happens if one is editing a file whose last line in not properly terminated (which makes it not a text file) is technically unspecified. On OpenBSD, the ed
editor will say "newline appended" when opening a non-terminated text file.
INPUT FILES
The input files shall be text files.
edited Jun 8 at 6:35
answered Jun 6 at 14:47
Kusalananda
101k13199312
101k13199312
Thanksï¼ 1) Do all the types of shell, such as ksh/sh/csh/bash, conform to the POSIX standard rule ï¼ 2) But indeed the last command line is still regarded as a incomplete line while the script is opened with vi ï¼Â
â lylklb
Jun 7 at 2:10
@lylklb All of those shells, with the exception ofcsh
, are POSIX shells and should behave the same way with regards to this.csh
is not a POSIX shell, so you would have to test that individually. I don't think I understand your second question. Normally a "line" needs to be terminated by a newline, but since the POSIX parsing rules for shell scripts have this special rule, the last line of a shell script may remain unterminated.
â Kusalananda
Jun 7 at 6:03
1) I have tested it in the csh environment, and it also work fine w/o last newlineï¼ 2) It is just a message at the bottom of vi editor that prompts "Incomplete last line". I think that this symptom is mainly due to the non-POSIX compliant systems/editorsï¼Â
â lylklb
Jun 7 at 14:30
@lylklb 1) Good. 2) No it is not. The lines of a text file should all be delimited by a newline character. The parsing rules for shell script allows the last line to be unterminated. This allows the shell to execute scripts which are read from a command substitution, likesh -c "$(cat script)"
. The last line ofscript
will not be terminated by a newline when given to thesh
interpreter, since the command substitution removes it. Also, inprintf 'echo hello' | sh
the script (echo hello
) does not have a newline at the end.
â Kusalananda
Jun 7 at 14:49
Could you please tell me where can be found with more detail/documents about this parsing rule for the last line in shell scripts ï¼Â
â lylklb
Jun 8 at 2:08
 |Â
show 3 more comments
Thanksï¼ 1) Do all the types of shell, such as ksh/sh/csh/bash, conform to the POSIX standard rule ï¼ 2) But indeed the last command line is still regarded as a incomplete line while the script is opened with vi ï¼Â
â lylklb
Jun 7 at 2:10
@lylklb All of those shells, with the exception ofcsh
, are POSIX shells and should behave the same way with regards to this.csh
is not a POSIX shell, so you would have to test that individually. I don't think I understand your second question. Normally a "line" needs to be terminated by a newline, but since the POSIX parsing rules for shell scripts have this special rule, the last line of a shell script may remain unterminated.
â Kusalananda
Jun 7 at 6:03
1) I have tested it in the csh environment, and it also work fine w/o last newlineï¼ 2) It is just a message at the bottom of vi editor that prompts "Incomplete last line". I think that this symptom is mainly due to the non-POSIX compliant systems/editorsï¼Â
â lylklb
Jun 7 at 14:30
@lylklb 1) Good. 2) No it is not. The lines of a text file should all be delimited by a newline character. The parsing rules for shell script allows the last line to be unterminated. This allows the shell to execute scripts which are read from a command substitution, likesh -c "$(cat script)"
. The last line ofscript
will not be terminated by a newline when given to thesh
interpreter, since the command substitution removes it. Also, inprintf 'echo hello' | sh
the script (echo hello
) does not have a newline at the end.
â Kusalananda
Jun 7 at 14:49
Could you please tell me where can be found with more detail/documents about this parsing rule for the last line in shell scripts ï¼Â
â lylklb
Jun 8 at 2:08
Thanksï¼ 1) Do all the types of shell, such as ksh/sh/csh/bash, conform to the POSIX standard rule ï¼ 2) But indeed the last command line is still regarded as a incomplete line while the script is opened with vi ï¼Â
â lylklb
Jun 7 at 2:10
Thanksï¼ 1) Do all the types of shell, such as ksh/sh/csh/bash, conform to the POSIX standard rule ï¼ 2) But indeed the last command line is still regarded as a incomplete line while the script is opened with vi ï¼Â
â lylklb
Jun 7 at 2:10
@lylklb All of those shells, with the exception of
csh
, are POSIX shells and should behave the same way with regards to this. csh
is not a POSIX shell, so you would have to test that individually. I don't think I understand your second question. Normally a "line" needs to be terminated by a newline, but since the POSIX parsing rules for shell scripts have this special rule, the last line of a shell script may remain unterminated.â Kusalananda
Jun 7 at 6:03
@lylklb All of those shells, with the exception of
csh
, are POSIX shells and should behave the same way with regards to this. csh
is not a POSIX shell, so you would have to test that individually. I don't think I understand your second question. Normally a "line" needs to be terminated by a newline, but since the POSIX parsing rules for shell scripts have this special rule, the last line of a shell script may remain unterminated.â Kusalananda
Jun 7 at 6:03
1) I have tested it in the csh environment, and it also work fine w/o last newlineï¼ 2) It is just a message at the bottom of vi editor that prompts "Incomplete last line". I think that this symptom is mainly due to the non-POSIX compliant systems/editorsï¼Â
â lylklb
Jun 7 at 14:30
1) I have tested it in the csh environment, and it also work fine w/o last newlineï¼ 2) It is just a message at the bottom of vi editor that prompts "Incomplete last line". I think that this symptom is mainly due to the non-POSIX compliant systems/editorsï¼Â
â lylklb
Jun 7 at 14:30
@lylklb 1) Good. 2) No it is not. The lines of a text file should all be delimited by a newline character. The parsing rules for shell script allows the last line to be unterminated. This allows the shell to execute scripts which are read from a command substitution, like
sh -c "$(cat script)"
. The last line of script
will not be terminated by a newline when given to the sh
interpreter, since the command substitution removes it. Also, in printf 'echo hello' | sh
the script (echo hello
) does not have a newline at the end.â Kusalananda
Jun 7 at 14:49
@lylklb 1) Good. 2) No it is not. The lines of a text file should all be delimited by a newline character. The parsing rules for shell script allows the last line to be unterminated. This allows the shell to execute scripts which are read from a command substitution, like
sh -c "$(cat script)"
. The last line of script
will not be terminated by a newline when given to the sh
interpreter, since the command substitution removes it. Also, in printf 'echo hello' | sh
the script (echo hello
) does not have a newline at the end.â Kusalananda
Jun 7 at 14:49
Could you please tell me where can be found with more detail/documents about this parsing rule for the last line in shell scripts ï¼Â
â lylklb
Jun 8 at 2:08
Could you please tell me where can be found with more detail/documents about this parsing rule for the last line in shell scripts ï¼Â
â lylklb
Jun 8 at 2:08
 |Â
show 3 more comments
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
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f448224%2fthe-last-command-line-in-the-shell-script-without-a-newline%23new-answer', 'question_page');
);
Post as a guest
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
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
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