getops $OPTARG is empty if flag value contains bracketsHow to check if a string contains a substring in BashUsing getopts within user-defined-function in bourne shellCheck if a Bash array contains a valueHow to create a flag with getopts to run a commandgetopts: optional $OPT_ARGJMESPath query expression with bash variableHow to throw error to only use one option flag when using getopts in bash script?getopts - if a flag is empty getopts use the next flagbash getopts ignores options unless they are specified in a certain waybash echo environment variable containing escaped characters
Infinite points on circle
Counting multiples of 3 up to a given number
Can a person become a professor in English without having a Bachelor degree in English?
What does Windows' "Tuning up Application Start" do?
French equivalents of "X puts the smile back on her face"
Three phase systems - are there any single phase devices that are connected between two phases instead of between one phase and neutral?
Project Euler # 25 The 1000 digit Fibonacci index
Does a hash function have a Upper bound on input length?
Bowing signs (?) in Lajos Montag's double bass method
How does the Gameboy's memory bank switching work?
Is there an English word to describe when a sound "protrudes"?
When we are talking about black hole evaporation - what exactly happens?
How is it possible for the induced emf to take negative values in Faraday's Law of induction?
You have no, but can try for yes
Killing a star safely
Remove side menu(right side) from finder
why neutral does not shock. how can a neutral be neutral in ac current?
How deep is the Underdark? What is its max and median depth?
What is the difference between uniform velocity and constant velocity?
How to tell readers that I know my story is factually incorrect?
How can electronics on board JWST survive the low operating temperature while it's difficult to survive lunar nights?
Found old paper shares of Motorola Inc that has since been broken up
TCP connections hang during handshake
Why is there an extra "t" in Lemmatization?
getops $OPTARG is empty if flag value contains brackets
How to check if a string contains a substring in BashUsing getopts within user-defined-function in bourne shellCheck if a Bash array contains a valueHow to create a flag with getopts to run a commandgetopts: optional $OPT_ARGJMESPath query expression with bash variableHow to throw error to only use one option flag when using getopts in bash script?getopts - if a flag is empty getopts use the next flagbash getopts ignores options unless they are specified in a certain waybash echo environment variable containing escaped characters
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
When I pass a flag containing [...]
to my bash script, getops
gives me an empty string when I try to grab the value with $OPTARG
.
shopt -s nullglob
while getopts ":f:" opt; do
case $opt in
f)
str=$OPTARG
;;
esac
done
echo $str
Running the script:
$ script.sh -f [0.0.0.0]
<blank line>
How can I get the original value back inside the script?
bash echo getopts
add a comment |
When I pass a flag containing [...]
to my bash script, getops
gives me an empty string when I try to grab the value with $OPTARG
.
shopt -s nullglob
while getopts ":f:" opt; do
case $opt in
f)
str=$OPTARG
;;
esac
done
echo $str
Running the script:
$ script.sh -f [0.0.0.0]
<blank line>
How can I get the original value back inside the script?
bash echo getopts
A bash script should start with#!/bin/bash
- what happened when you add this line to your script?
– Yaron
Mar 26 at 12:45
1
note that when I execute the script I got the following output:[0.0.0.0]
– Yaron
Mar 26 at 12:46
I found the cause but don't know how to handle it. The script is much bigger, this is just small part. There was ashopt -s nullglob
at the top. This breaks the script.
– Gilrich
Mar 26 at 13:02
Assumiong thatshopt -s nullglob
is needed - your can disable it when your run your small part of the code. what happened when you addshopt -u nullglob
before your small part of the code, andshopt -s nullglob
after your code?
– Yaron
Mar 26 at 13:15
I can put it after the flag switch-statement but not after theecho command
. As soon as I put theshopt -s nullglob
the variable evaluates to the empty string.
– Gilrich
Mar 26 at 13:33
add a comment |
When I pass a flag containing [...]
to my bash script, getops
gives me an empty string when I try to grab the value with $OPTARG
.
shopt -s nullglob
while getopts ":f:" opt; do
case $opt in
f)
str=$OPTARG
;;
esac
done
echo $str
Running the script:
$ script.sh -f [0.0.0.0]
<blank line>
How can I get the original value back inside the script?
bash echo getopts
When I pass a flag containing [...]
to my bash script, getops
gives me an empty string when I try to grab the value with $OPTARG
.
shopt -s nullglob
while getopts ":f:" opt; do
case $opt in
f)
str=$OPTARG
;;
esac
done
echo $str
Running the script:
$ script.sh -f [0.0.0.0]
<blank line>
How can I get the original value back inside the script?
bash echo getopts
bash echo getopts
edited Mar 26 at 14:25
Yaron
6,2276 gold badges32 silver badges45 bronze badges
6,2276 gold badges32 silver badges45 bronze badges
asked Mar 26 at 12:37
GilrichGilrich
878 bronze badges
878 bronze badges
A bash script should start with#!/bin/bash
- what happened when you add this line to your script?
– Yaron
Mar 26 at 12:45
1
note that when I execute the script I got the following output:[0.0.0.0]
– Yaron
Mar 26 at 12:46
I found the cause but don't know how to handle it. The script is much bigger, this is just small part. There was ashopt -s nullglob
at the top. This breaks the script.
– Gilrich
Mar 26 at 13:02
Assumiong thatshopt -s nullglob
is needed - your can disable it when your run your small part of the code. what happened when you addshopt -u nullglob
before your small part of the code, andshopt -s nullglob
after your code?
– Yaron
Mar 26 at 13:15
I can put it after the flag switch-statement but not after theecho command
. As soon as I put theshopt -s nullglob
the variable evaluates to the empty string.
– Gilrich
Mar 26 at 13:33
add a comment |
A bash script should start with#!/bin/bash
- what happened when you add this line to your script?
– Yaron
Mar 26 at 12:45
1
note that when I execute the script I got the following output:[0.0.0.0]
– Yaron
Mar 26 at 12:46
I found the cause but don't know how to handle it. The script is much bigger, this is just small part. There was ashopt -s nullglob
at the top. This breaks the script.
– Gilrich
Mar 26 at 13:02
Assumiong thatshopt -s nullglob
is needed - your can disable it when your run your small part of the code. what happened when you addshopt -u nullglob
before your small part of the code, andshopt -s nullglob
after your code?
– Yaron
Mar 26 at 13:15
I can put it after the flag switch-statement but not after theecho command
. As soon as I put theshopt -s nullglob
the variable evaluates to the empty string.
– Gilrich
Mar 26 at 13:33
A bash script should start with
#!/bin/bash
- what happened when you add this line to your script?– Yaron
Mar 26 at 12:45
A bash script should start with
#!/bin/bash
- what happened when you add this line to your script?– Yaron
Mar 26 at 12:45
1
1
note that when I execute the script I got the following output:
[0.0.0.0]
– Yaron
Mar 26 at 12:46
note that when I execute the script I got the following output:
[0.0.0.0]
– Yaron
Mar 26 at 12:46
I found the cause but don't know how to handle it. The script is much bigger, this is just small part. There was a
shopt -s nullglob
at the top. This breaks the script.– Gilrich
Mar 26 at 13:02
I found the cause but don't know how to handle it. The script is much bigger, this is just small part. There was a
shopt -s nullglob
at the top. This breaks the script.– Gilrich
Mar 26 at 13:02
Assumiong that
shopt -s nullglob
is needed - your can disable it when your run your small part of the code. what happened when you add shopt -u nullglob
before your small part of the code, and shopt -s nullglob
after your code?– Yaron
Mar 26 at 13:15
Assumiong that
shopt -s nullglob
is needed - your can disable it when your run your small part of the code. what happened when you add shopt -u nullglob
before your small part of the code, and shopt -s nullglob
after your code?– Yaron
Mar 26 at 13:15
I can put it after the flag switch-statement but not after the
echo command
. As soon as I put the shopt -s nullglob
the variable evaluates to the empty string.– Gilrich
Mar 26 at 13:33
I can put it after the flag switch-statement but not after the
echo command
. As soon as I put the shopt -s nullglob
the variable evaluates to the empty string.– Gilrich
Mar 26 at 13:33
add a comment |
2 Answers
2
active
oldest
votes
Short summary: Double-quote your variable references. And use shellcheck.net.
Long explanation: When you use a variable without double-quotes around it (e.g. echo $str
), the shell tries to split its value into words, and expand anything that looks like a wildcard expression into a list of matching files. In the case of [0.0.0.0]
, the brackets make it a wildcard expression that'll match either the character "0" or "." (equivalent to [0.]
). If you had a file named "0", it would expand to that string. With no matching file(s), it's normally left unexpanded, but with the nullglob
set it expands to ... null.
Turning off nullglob
solves the problem if there are no matching files, but isn't really the right way do it. I remember (but can't find right now) a question we had about a script that failed on one particular computer, and it turned out the reason was that one computer happened to have a file that matched a bracket expression in an unquoted variable's value.
The right solution is to put double-quotes around the variable reference. This tells the shell to skip word splitting and wildcard expansion. Here's an interactive example:
$ str='[0.0.0.0]' # Quotes aren't actually needed here, but they don't hurt
$ echo $str # This works without nullglob or a matching file
[0.0.0.0]
$ shopt -s nullglob
$ echo $str # This fails because of nullglob
$ shopt -u nullglob
$ touch 0
$ echo $str # This fails because of a matching file
0
$ echo "$str" # This just works, no matter whether file(s) match and/or nullglob is set
[0.0.0.0]
So in your script, simply change the last line to:
echo "$str"
Note that double-quotes are not required in either case $opt in
or str=$OPTARG
because variables in those specific contexts aren't subject to word splitting or wildcard expansion. But IMO keeping track of which contexts it's safe to leave the double-quotes off is more hassle than it's worth, and you should just double-quote 'em all.
BTW, shellcheck.net is good at spotting common mistakes like this; I recommend feeding your scripts through it, since this is probably not the only place you have this problem.
Great, in-depth answer! However, your proposed solution looks exactly like the line I already have. (see last line of my code block)
– Gilrich
Mar 26 at 16:32
@Gilrich D'Oh! After copying the original line, I just clean forgot to actually apply the fix. I've edited it to correct...
– Gordon Davisson
Mar 26 at 16:40
add a comment |
Assuming that shopt -s nullglob
is needed in the bigger script.
You can temporary disable shopt -s nullglob
using shopt -u nullglob
shopt -s nullglob
shopt -u nullglob
while getopts ":f:" opt; do
case $opt in
f)
str=$OPTARG
;;
esac
done
echo $str
shopt -s nullglob
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
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: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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%2fstackoverflow.com%2fquestions%2f55357379%2fgetops-optarg-is-empty-if-flag-value-contains-brackets%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Short summary: Double-quote your variable references. And use shellcheck.net.
Long explanation: When you use a variable without double-quotes around it (e.g. echo $str
), the shell tries to split its value into words, and expand anything that looks like a wildcard expression into a list of matching files. In the case of [0.0.0.0]
, the brackets make it a wildcard expression that'll match either the character "0" or "." (equivalent to [0.]
). If you had a file named "0", it would expand to that string. With no matching file(s), it's normally left unexpanded, but with the nullglob
set it expands to ... null.
Turning off nullglob
solves the problem if there are no matching files, but isn't really the right way do it. I remember (but can't find right now) a question we had about a script that failed on one particular computer, and it turned out the reason was that one computer happened to have a file that matched a bracket expression in an unquoted variable's value.
The right solution is to put double-quotes around the variable reference. This tells the shell to skip word splitting and wildcard expansion. Here's an interactive example:
$ str='[0.0.0.0]' # Quotes aren't actually needed here, but they don't hurt
$ echo $str # This works without nullglob or a matching file
[0.0.0.0]
$ shopt -s nullglob
$ echo $str # This fails because of nullglob
$ shopt -u nullglob
$ touch 0
$ echo $str # This fails because of a matching file
0
$ echo "$str" # This just works, no matter whether file(s) match and/or nullglob is set
[0.0.0.0]
So in your script, simply change the last line to:
echo "$str"
Note that double-quotes are not required in either case $opt in
or str=$OPTARG
because variables in those specific contexts aren't subject to word splitting or wildcard expansion. But IMO keeping track of which contexts it's safe to leave the double-quotes off is more hassle than it's worth, and you should just double-quote 'em all.
BTW, shellcheck.net is good at spotting common mistakes like this; I recommend feeding your scripts through it, since this is probably not the only place you have this problem.
Great, in-depth answer! However, your proposed solution looks exactly like the line I already have. (see last line of my code block)
– Gilrich
Mar 26 at 16:32
@Gilrich D'Oh! After copying the original line, I just clean forgot to actually apply the fix. I've edited it to correct...
– Gordon Davisson
Mar 26 at 16:40
add a comment |
Short summary: Double-quote your variable references. And use shellcheck.net.
Long explanation: When you use a variable without double-quotes around it (e.g. echo $str
), the shell tries to split its value into words, and expand anything that looks like a wildcard expression into a list of matching files. In the case of [0.0.0.0]
, the brackets make it a wildcard expression that'll match either the character "0" or "." (equivalent to [0.]
). If you had a file named "0", it would expand to that string. With no matching file(s), it's normally left unexpanded, but with the nullglob
set it expands to ... null.
Turning off nullglob
solves the problem if there are no matching files, but isn't really the right way do it. I remember (but can't find right now) a question we had about a script that failed on one particular computer, and it turned out the reason was that one computer happened to have a file that matched a bracket expression in an unquoted variable's value.
The right solution is to put double-quotes around the variable reference. This tells the shell to skip word splitting and wildcard expansion. Here's an interactive example:
$ str='[0.0.0.0]' # Quotes aren't actually needed here, but they don't hurt
$ echo $str # This works without nullglob or a matching file
[0.0.0.0]
$ shopt -s nullglob
$ echo $str # This fails because of nullglob
$ shopt -u nullglob
$ touch 0
$ echo $str # This fails because of a matching file
0
$ echo "$str" # This just works, no matter whether file(s) match and/or nullglob is set
[0.0.0.0]
So in your script, simply change the last line to:
echo "$str"
Note that double-quotes are not required in either case $opt in
or str=$OPTARG
because variables in those specific contexts aren't subject to word splitting or wildcard expansion. But IMO keeping track of which contexts it's safe to leave the double-quotes off is more hassle than it's worth, and you should just double-quote 'em all.
BTW, shellcheck.net is good at spotting common mistakes like this; I recommend feeding your scripts through it, since this is probably not the only place you have this problem.
Great, in-depth answer! However, your proposed solution looks exactly like the line I already have. (see last line of my code block)
– Gilrich
Mar 26 at 16:32
@Gilrich D'Oh! After copying the original line, I just clean forgot to actually apply the fix. I've edited it to correct...
– Gordon Davisson
Mar 26 at 16:40
add a comment |
Short summary: Double-quote your variable references. And use shellcheck.net.
Long explanation: When you use a variable without double-quotes around it (e.g. echo $str
), the shell tries to split its value into words, and expand anything that looks like a wildcard expression into a list of matching files. In the case of [0.0.0.0]
, the brackets make it a wildcard expression that'll match either the character "0" or "." (equivalent to [0.]
). If you had a file named "0", it would expand to that string. With no matching file(s), it's normally left unexpanded, but with the nullglob
set it expands to ... null.
Turning off nullglob
solves the problem if there are no matching files, but isn't really the right way do it. I remember (but can't find right now) a question we had about a script that failed on one particular computer, and it turned out the reason was that one computer happened to have a file that matched a bracket expression in an unquoted variable's value.
The right solution is to put double-quotes around the variable reference. This tells the shell to skip word splitting and wildcard expansion. Here's an interactive example:
$ str='[0.0.0.0]' # Quotes aren't actually needed here, but they don't hurt
$ echo $str # This works without nullglob or a matching file
[0.0.0.0]
$ shopt -s nullglob
$ echo $str # This fails because of nullglob
$ shopt -u nullglob
$ touch 0
$ echo $str # This fails because of a matching file
0
$ echo "$str" # This just works, no matter whether file(s) match and/or nullglob is set
[0.0.0.0]
So in your script, simply change the last line to:
echo "$str"
Note that double-quotes are not required in either case $opt in
or str=$OPTARG
because variables in those specific contexts aren't subject to word splitting or wildcard expansion. But IMO keeping track of which contexts it's safe to leave the double-quotes off is more hassle than it's worth, and you should just double-quote 'em all.
BTW, shellcheck.net is good at spotting common mistakes like this; I recommend feeding your scripts through it, since this is probably not the only place you have this problem.
Short summary: Double-quote your variable references. And use shellcheck.net.
Long explanation: When you use a variable without double-quotes around it (e.g. echo $str
), the shell tries to split its value into words, and expand anything that looks like a wildcard expression into a list of matching files. In the case of [0.0.0.0]
, the brackets make it a wildcard expression that'll match either the character "0" or "." (equivalent to [0.]
). If you had a file named "0", it would expand to that string. With no matching file(s), it's normally left unexpanded, but with the nullglob
set it expands to ... null.
Turning off nullglob
solves the problem if there are no matching files, but isn't really the right way do it. I remember (but can't find right now) a question we had about a script that failed on one particular computer, and it turned out the reason was that one computer happened to have a file that matched a bracket expression in an unquoted variable's value.
The right solution is to put double-quotes around the variable reference. This tells the shell to skip word splitting and wildcard expansion. Here's an interactive example:
$ str='[0.0.0.0]' # Quotes aren't actually needed here, but they don't hurt
$ echo $str # This works without nullglob or a matching file
[0.0.0.0]
$ shopt -s nullglob
$ echo $str # This fails because of nullglob
$ shopt -u nullglob
$ touch 0
$ echo $str # This fails because of a matching file
0
$ echo "$str" # This just works, no matter whether file(s) match and/or nullglob is set
[0.0.0.0]
So in your script, simply change the last line to:
echo "$str"
Note that double-quotes are not required in either case $opt in
or str=$OPTARG
because variables in those specific contexts aren't subject to word splitting or wildcard expansion. But IMO keeping track of which contexts it's safe to leave the double-quotes off is more hassle than it's worth, and you should just double-quote 'em all.
BTW, shellcheck.net is good at spotting common mistakes like this; I recommend feeding your scripts through it, since this is probably not the only place you have this problem.
edited Mar 26 at 16:39
answered Mar 26 at 15:54
Gordon DavissonGordon Davisson
74.9k11 gold badges80 silver badges98 bronze badges
74.9k11 gold badges80 silver badges98 bronze badges
Great, in-depth answer! However, your proposed solution looks exactly like the line I already have. (see last line of my code block)
– Gilrich
Mar 26 at 16:32
@Gilrich D'Oh! After copying the original line, I just clean forgot to actually apply the fix. I've edited it to correct...
– Gordon Davisson
Mar 26 at 16:40
add a comment |
Great, in-depth answer! However, your proposed solution looks exactly like the line I already have. (see last line of my code block)
– Gilrich
Mar 26 at 16:32
@Gilrich D'Oh! After copying the original line, I just clean forgot to actually apply the fix. I've edited it to correct...
– Gordon Davisson
Mar 26 at 16:40
Great, in-depth answer! However, your proposed solution looks exactly like the line I already have. (see last line of my code block)
– Gilrich
Mar 26 at 16:32
Great, in-depth answer! However, your proposed solution looks exactly like the line I already have. (see last line of my code block)
– Gilrich
Mar 26 at 16:32
@Gilrich D'Oh! After copying the original line, I just clean forgot to actually apply the fix. I've edited it to correct...
– Gordon Davisson
Mar 26 at 16:40
@Gilrich D'Oh! After copying the original line, I just clean forgot to actually apply the fix. I've edited it to correct...
– Gordon Davisson
Mar 26 at 16:40
add a comment |
Assuming that shopt -s nullglob
is needed in the bigger script.
You can temporary disable shopt -s nullglob
using shopt -u nullglob
shopt -s nullglob
shopt -u nullglob
while getopts ":f:" opt; do
case $opt in
f)
str=$OPTARG
;;
esac
done
echo $str
shopt -s nullglob
add a comment |
Assuming that shopt -s nullglob
is needed in the bigger script.
You can temporary disable shopt -s nullglob
using shopt -u nullglob
shopt -s nullglob
shopt -u nullglob
while getopts ":f:" opt; do
case $opt in
f)
str=$OPTARG
;;
esac
done
echo $str
shopt -s nullglob
add a comment |
Assuming that shopt -s nullglob
is needed in the bigger script.
You can temporary disable shopt -s nullglob
using shopt -u nullglob
shopt -s nullglob
shopt -u nullglob
while getopts ":f:" opt; do
case $opt in
f)
str=$OPTARG
;;
esac
done
echo $str
shopt -s nullglob
Assuming that shopt -s nullglob
is needed in the bigger script.
You can temporary disable shopt -s nullglob
using shopt -u nullglob
shopt -s nullglob
shopt -u nullglob
while getopts ":f:" opt; do
case $opt in
f)
str=$OPTARG
;;
esac
done
echo $str
shopt -s nullglob
answered Mar 26 at 13:18
YaronYaron
6,2276 gold badges32 silver badges45 bronze badges
6,2276 gold badges32 silver badges45 bronze badges
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- 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%2fstackoverflow.com%2fquestions%2f55357379%2fgetops-optarg-is-empty-if-flag-value-contains-brackets%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
A bash script should start with
#!/bin/bash
- what happened when you add this line to your script?– Yaron
Mar 26 at 12:45
1
note that when I execute the script I got the following output:
[0.0.0.0]
– Yaron
Mar 26 at 12:46
I found the cause but don't know how to handle it. The script is much bigger, this is just small part. There was a
shopt -s nullglob
at the top. This breaks the script.– Gilrich
Mar 26 at 13:02
Assumiong that
shopt -s nullglob
is needed - your can disable it when your run your small part of the code. what happened when you addshopt -u nullglob
before your small part of the code, andshopt -s nullglob
after your code?– Yaron
Mar 26 at 13:15
I can put it after the flag switch-statement but not after the
echo command
. As soon as I put theshopt -s nullglob
the variable evaluates to the empty string.– Gilrich
Mar 26 at 13:33