When does the loop starting with while [ -n “$1” ]; executes?
Clash Royale CLAN TAG#URR8PPP
I have a shell script with while loop starting with
while [ -n "$1" ]; do
I know that $1
refers to the argument right after the name of the script, ie firstargument
, when I execute ./myscript.sh firstargument
.
What do the -n
do?
linux bash shell-script shell
add a comment |
I have a shell script with while loop starting with
while [ -n "$1" ]; do
I know that $1
refers to the argument right after the name of the script, ie firstargument
, when I execute ./myscript.sh firstargument
.
What do the -n
do?
linux bash shell-script shell
add a comment |
I have a shell script with while loop starting with
while [ -n "$1" ]; do
I know that $1
refers to the argument right after the name of the script, ie firstargument
, when I execute ./myscript.sh firstargument
.
What do the -n
do?
linux bash shell-script shell
I have a shell script with while loop starting with
while [ -n "$1" ]; do
I know that $1
refers to the argument right after the name of the script, ie firstargument
, when I execute ./myscript.sh firstargument
.
What do the -n
do?
linux bash shell-script shell
linux bash shell-script shell
asked Jan 18 at 9:46
zabopzabop
1063
1063
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
The -n
is a test for a non-empty string.
If the "$1"
expands to an empty string, then that particular test fails and the loop will not execute.
It is likely that the body of the loop contains a shift
statement to shift the next positional parameter into $1
, and that the loop in this way loops over the arguments to the script, until it finds an empty argument or comes to the end of the list of arguments.
The test
utility is equivalent to [
, but [
requires that the last argument is ]
.
The test could also be written as
while test -n "$1"; do
Both [
and test
are likely built into your shell, but should also be available as external commands under a standard path like /bin
.
You will be able to read more about this and other tests in man test
, as well as in the manual for your shell (as it's a built-in utility).
This -n
test is also one of the standard tests and therefore also listed in the POSIX standard for the test
utility.
If this loop is supposed to loop over all arguments of the shell script, than it may be failing to do so if an argument is empty:
$ sh -c 'while [ -n "$1" ]; do printf "arg: %sn" "$1"; shift; done' sh 1 2 3 "" 4 5 6
arg: 1
arg: 2
arg: 3
Instead, it should possibly use something like
for arg do
if [ -n "$arg" ]; then
# do something with "$arg"
fi
done
... depending on what the script does, obviously.
4
... and thewhile
is probably coupled to ashift
inside the loop (which is why it would be used instead ofif
).
– Stephen Kitt
Jan 18 at 9:54
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%2f495240%2fwhen-does-the-loop-starting-with-while-n-1-executes%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
The -n
is a test for a non-empty string.
If the "$1"
expands to an empty string, then that particular test fails and the loop will not execute.
It is likely that the body of the loop contains a shift
statement to shift the next positional parameter into $1
, and that the loop in this way loops over the arguments to the script, until it finds an empty argument or comes to the end of the list of arguments.
The test
utility is equivalent to [
, but [
requires that the last argument is ]
.
The test could also be written as
while test -n "$1"; do
Both [
and test
are likely built into your shell, but should also be available as external commands under a standard path like /bin
.
You will be able to read more about this and other tests in man test
, as well as in the manual for your shell (as it's a built-in utility).
This -n
test is also one of the standard tests and therefore also listed in the POSIX standard for the test
utility.
If this loop is supposed to loop over all arguments of the shell script, than it may be failing to do so if an argument is empty:
$ sh -c 'while [ -n "$1" ]; do printf "arg: %sn" "$1"; shift; done' sh 1 2 3 "" 4 5 6
arg: 1
arg: 2
arg: 3
Instead, it should possibly use something like
for arg do
if [ -n "$arg" ]; then
# do something with "$arg"
fi
done
... depending on what the script does, obviously.
4
... and thewhile
is probably coupled to ashift
inside the loop (which is why it would be used instead ofif
).
– Stephen Kitt
Jan 18 at 9:54
add a comment |
The -n
is a test for a non-empty string.
If the "$1"
expands to an empty string, then that particular test fails and the loop will not execute.
It is likely that the body of the loop contains a shift
statement to shift the next positional parameter into $1
, and that the loop in this way loops over the arguments to the script, until it finds an empty argument or comes to the end of the list of arguments.
The test
utility is equivalent to [
, but [
requires that the last argument is ]
.
The test could also be written as
while test -n "$1"; do
Both [
and test
are likely built into your shell, but should also be available as external commands under a standard path like /bin
.
You will be able to read more about this and other tests in man test
, as well as in the manual for your shell (as it's a built-in utility).
This -n
test is also one of the standard tests and therefore also listed in the POSIX standard for the test
utility.
If this loop is supposed to loop over all arguments of the shell script, than it may be failing to do so if an argument is empty:
$ sh -c 'while [ -n "$1" ]; do printf "arg: %sn" "$1"; shift; done' sh 1 2 3 "" 4 5 6
arg: 1
arg: 2
arg: 3
Instead, it should possibly use something like
for arg do
if [ -n "$arg" ]; then
# do something with "$arg"
fi
done
... depending on what the script does, obviously.
4
... and thewhile
is probably coupled to ashift
inside the loop (which is why it would be used instead ofif
).
– Stephen Kitt
Jan 18 at 9:54
add a comment |
The -n
is a test for a non-empty string.
If the "$1"
expands to an empty string, then that particular test fails and the loop will not execute.
It is likely that the body of the loop contains a shift
statement to shift the next positional parameter into $1
, and that the loop in this way loops over the arguments to the script, until it finds an empty argument or comes to the end of the list of arguments.
The test
utility is equivalent to [
, but [
requires that the last argument is ]
.
The test could also be written as
while test -n "$1"; do
Both [
and test
are likely built into your shell, but should also be available as external commands under a standard path like /bin
.
You will be able to read more about this and other tests in man test
, as well as in the manual for your shell (as it's a built-in utility).
This -n
test is also one of the standard tests and therefore also listed in the POSIX standard for the test
utility.
If this loop is supposed to loop over all arguments of the shell script, than it may be failing to do so if an argument is empty:
$ sh -c 'while [ -n "$1" ]; do printf "arg: %sn" "$1"; shift; done' sh 1 2 3 "" 4 5 6
arg: 1
arg: 2
arg: 3
Instead, it should possibly use something like
for arg do
if [ -n "$arg" ]; then
# do something with "$arg"
fi
done
... depending on what the script does, obviously.
The -n
is a test for a non-empty string.
If the "$1"
expands to an empty string, then that particular test fails and the loop will not execute.
It is likely that the body of the loop contains a shift
statement to shift the next positional parameter into $1
, and that the loop in this way loops over the arguments to the script, until it finds an empty argument or comes to the end of the list of arguments.
The test
utility is equivalent to [
, but [
requires that the last argument is ]
.
The test could also be written as
while test -n "$1"; do
Both [
and test
are likely built into your shell, but should also be available as external commands under a standard path like /bin
.
You will be able to read more about this and other tests in man test
, as well as in the manual for your shell (as it's a built-in utility).
This -n
test is also one of the standard tests and therefore also listed in the POSIX standard for the test
utility.
If this loop is supposed to loop over all arguments of the shell script, than it may be failing to do so if an argument is empty:
$ sh -c 'while [ -n "$1" ]; do printf "arg: %sn" "$1"; shift; done' sh 1 2 3 "" 4 5 6
arg: 1
arg: 2
arg: 3
Instead, it should possibly use something like
for arg do
if [ -n "$arg" ]; then
# do something with "$arg"
fi
done
... depending on what the script does, obviously.
edited Jan 18 at 10:12
answered Jan 18 at 9:48
KusalanandaKusalananda
128k16241398
128k16241398
4
... and thewhile
is probably coupled to ashift
inside the loop (which is why it would be used instead ofif
).
– Stephen Kitt
Jan 18 at 9:54
add a comment |
4
... and thewhile
is probably coupled to ashift
inside the loop (which is why it would be used instead ofif
).
– Stephen Kitt
Jan 18 at 9:54
4
4
... and the
while
is probably coupled to a shift
inside the loop (which is why it would be used instead of if
).– Stephen Kitt
Jan 18 at 9:54
... and the
while
is probably coupled to a shift
inside the loop (which is why it would be used instead of if
).– Stephen Kitt
Jan 18 at 9:54
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%2f495240%2fwhen-does-the-loop-starting-with-while-n-1-executes%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