BASH: Code snippet with double square bracket i[[3456]]86

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
4
down vote

favorite












I am trying to understand the following code snippet.



host_cpu='i386'

case "$host_cpu" in
i[[3456]]86)
echo "host_cpu=i386"
;;
x86_64)
echo "host_cpu=x86_64"
;;
*)
echo "AC_MSG_ERROR([unsupported CPU type]) "
;;
esac


I added the variable host_cpu='i386' myself to test the code and it switched to the third case of echo "AC_MSG_ERROR([unsupported CPU type]) ".



If I change the double bracket in i[[3456]]86) to a single bracket as in i[3456]86), it switches to the first option yielding i386. This seems correct to me.



I understand that [ and [[ are the test options. The test condition doesn't seem to apply here as the switch cases are expecting a character to be outputted. So I'm assuming for bash to pick it up as a test condition, it needs to be separated by a space as in [ a < b ] or [[ a << b ]]. Because there are no spaces in these case statement, it'll be treated as a regular expression. Is this correct?



So my question is why was the double square bracket was used here by the code writer? It didn't work when I tried to run the code, so what was their intention.



Note: The code was taken from a configure.ac in the GRUB source code.



Also the $host_cpu and host_cpu=i386 line seem unnecessary, can you explain why the writer would have done this:



case "$host_cpu" in
i[[3456]]86)
host_cpu=i386
;;

....... omitted code .....

AC_SUBST(host_cpu)


I'm thinking of using AC_SUBST($host_cpu). Why wouldn't you do it this way?










share|improve this question



















  • 2




    Looks like a bug. I suggest you submit a pull request. And no, it's not a regular expression; it's pattern matching, which works the same as file globs.
    – Wildcard
    Oct 4 at 1:56














up vote
4
down vote

favorite












I am trying to understand the following code snippet.



host_cpu='i386'

case "$host_cpu" in
i[[3456]]86)
echo "host_cpu=i386"
;;
x86_64)
echo "host_cpu=x86_64"
;;
*)
echo "AC_MSG_ERROR([unsupported CPU type]) "
;;
esac


I added the variable host_cpu='i386' myself to test the code and it switched to the third case of echo "AC_MSG_ERROR([unsupported CPU type]) ".



If I change the double bracket in i[[3456]]86) to a single bracket as in i[3456]86), it switches to the first option yielding i386. This seems correct to me.



I understand that [ and [[ are the test options. The test condition doesn't seem to apply here as the switch cases are expecting a character to be outputted. So I'm assuming for bash to pick it up as a test condition, it needs to be separated by a space as in [ a < b ] or [[ a << b ]]. Because there are no spaces in these case statement, it'll be treated as a regular expression. Is this correct?



So my question is why was the double square bracket was used here by the code writer? It didn't work when I tried to run the code, so what was their intention.



Note: The code was taken from a configure.ac in the GRUB source code.



Also the $host_cpu and host_cpu=i386 line seem unnecessary, can you explain why the writer would have done this:



case "$host_cpu" in
i[[3456]]86)
host_cpu=i386
;;

....... omitted code .....

AC_SUBST(host_cpu)


I'm thinking of using AC_SUBST($host_cpu). Why wouldn't you do it this way?










share|improve this question



















  • 2




    Looks like a bug. I suggest you submit a pull request. And no, it's not a regular expression; it's pattern matching, which works the same as file globs.
    – Wildcard
    Oct 4 at 1:56












up vote
4
down vote

favorite









up vote
4
down vote

favorite











I am trying to understand the following code snippet.



host_cpu='i386'

case "$host_cpu" in
i[[3456]]86)
echo "host_cpu=i386"
;;
x86_64)
echo "host_cpu=x86_64"
;;
*)
echo "AC_MSG_ERROR([unsupported CPU type]) "
;;
esac


I added the variable host_cpu='i386' myself to test the code and it switched to the third case of echo "AC_MSG_ERROR([unsupported CPU type]) ".



If I change the double bracket in i[[3456]]86) to a single bracket as in i[3456]86), it switches to the first option yielding i386. This seems correct to me.



I understand that [ and [[ are the test options. The test condition doesn't seem to apply here as the switch cases are expecting a character to be outputted. So I'm assuming for bash to pick it up as a test condition, it needs to be separated by a space as in [ a < b ] or [[ a << b ]]. Because there are no spaces in these case statement, it'll be treated as a regular expression. Is this correct?



So my question is why was the double square bracket was used here by the code writer? It didn't work when I tried to run the code, so what was their intention.



Note: The code was taken from a configure.ac in the GRUB source code.



Also the $host_cpu and host_cpu=i386 line seem unnecessary, can you explain why the writer would have done this:



case "$host_cpu" in
i[[3456]]86)
host_cpu=i386
;;

....... omitted code .....

AC_SUBST(host_cpu)


I'm thinking of using AC_SUBST($host_cpu). Why wouldn't you do it this way?










share|improve this question















I am trying to understand the following code snippet.



host_cpu='i386'

case "$host_cpu" in
i[[3456]]86)
echo "host_cpu=i386"
;;
x86_64)
echo "host_cpu=x86_64"
;;
*)
echo "AC_MSG_ERROR([unsupported CPU type]) "
;;
esac


I added the variable host_cpu='i386' myself to test the code and it switched to the third case of echo "AC_MSG_ERROR([unsupported CPU type]) ".



If I change the double bracket in i[[3456]]86) to a single bracket as in i[3456]86), it switches to the first option yielding i386. This seems correct to me.



I understand that [ and [[ are the test options. The test condition doesn't seem to apply here as the switch cases are expecting a character to be outputted. So I'm assuming for bash to pick it up as a test condition, it needs to be separated by a space as in [ a < b ] or [[ a << b ]]. Because there are no spaces in these case statement, it'll be treated as a regular expression. Is this correct?



So my question is why was the double square bracket was used here by the code writer? It didn't work when I tried to run the code, so what was their intention.



Note: The code was taken from a configure.ac in the GRUB source code.



Also the $host_cpu and host_cpu=i386 line seem unnecessary, can you explain why the writer would have done this:



case "$host_cpu" in
i[[3456]]86)
host_cpu=i386
;;

....... omitted code .....

AC_SUBST(host_cpu)


I'm thinking of using AC_SUBST($host_cpu). Why wouldn't you do it this way?







bash






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Oct 4 at 4:12

























asked Oct 4 at 1:30









supmethods

4924




4924







  • 2




    Looks like a bug. I suggest you submit a pull request. And no, it's not a regular expression; it's pattern matching, which works the same as file globs.
    – Wildcard
    Oct 4 at 1:56












  • 2




    Looks like a bug. I suggest you submit a pull request. And no, it's not a regular expression; it's pattern matching, which works the same as file globs.
    – Wildcard
    Oct 4 at 1:56







2




2




Looks like a bug. I suggest you submit a pull request. And no, it's not a regular expression; it's pattern matching, which works the same as file globs.
– Wildcard
Oct 4 at 1:56




Looks like a bug. I suggest you submit a pull request. And no, it's not a regular expression; it's pattern matching, which works the same as file globs.
– Wildcard
Oct 4 at 1:56










1 Answer
1






active

oldest

votes

















up vote
4
down vote













If that's a from a configure.ac file, then it's not a pure shell script, but is an Autoconf script using M4, where are quote characters.:




To fully understand where proper quotation is important, you first
need to know what the special characters are in Autoconf: ‘#’
introduces a comment inside which no macro expansion is performed,
‘,’ separates arguments, ‘[’ and ‘]’ are the quotes themselves,
‘(’ and ‘)’ (which M4 tries to match by pairs), and finally ‘$’
inside a macro definition.



Each time there can be a macro expansion, there is a quotation
expansion, i.e., one level of quotes is stripped:



 int tab[10];
⇒int tab10;
[int tab[10];]
⇒int tab[10];



That [[3456]] should be expanded by autoconf to [3456], which would be the correct output for use as a shell script.






share|improve this answer
















  • 1




    Indeed - as one can verify by adding a minimal AC_INIT([foo]) to the top of the snippet and then executing autoconf snippet
    – steeldriver
    Oct 4 at 2:27











  • Okay, thanks. I've read about m4 and they used the quote characters ` and ' in those articles. So the must have changed the quotes in the configure.ac to treat [ and ] as quotes.
    – supmethods
    Oct 4 at 3:56










  • The link you provided use [ and ] as quotes. Does this depend on M4 version used?
    – supmethods
    Oct 4 at 4:01










  • @supmethods you can change the quote characters in M4, and it seems [ ] are set as quote characters in autoconf scripts. The default quote characters in M4 are `' , as you have seen.
    – muru
    Oct 4 at 4:17











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',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f473123%2fbash-code-snippet-with-double-square-bracket-i345686%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
4
down vote













If that's a from a configure.ac file, then it's not a pure shell script, but is an Autoconf script using M4, where are quote characters.:




To fully understand where proper quotation is important, you first
need to know what the special characters are in Autoconf: ‘#’
introduces a comment inside which no macro expansion is performed,
‘,’ separates arguments, ‘[’ and ‘]’ are the quotes themselves,
‘(’ and ‘)’ (which M4 tries to match by pairs), and finally ‘$’
inside a macro definition.



Each time there can be a macro expansion, there is a quotation
expansion, i.e., one level of quotes is stripped:



 int tab[10];
⇒int tab10;
[int tab[10];]
⇒int tab[10];



That [[3456]] should be expanded by autoconf to [3456], which would be the correct output for use as a shell script.






share|improve this answer
















  • 1




    Indeed - as one can verify by adding a minimal AC_INIT([foo]) to the top of the snippet and then executing autoconf snippet
    – steeldriver
    Oct 4 at 2:27











  • Okay, thanks. I've read about m4 and they used the quote characters ` and ' in those articles. So the must have changed the quotes in the configure.ac to treat [ and ] as quotes.
    – supmethods
    Oct 4 at 3:56










  • The link you provided use [ and ] as quotes. Does this depend on M4 version used?
    – supmethods
    Oct 4 at 4:01










  • @supmethods you can change the quote characters in M4, and it seems [ ] are set as quote characters in autoconf scripts. The default quote characters in M4 are `' , as you have seen.
    – muru
    Oct 4 at 4:17















up vote
4
down vote













If that's a from a configure.ac file, then it's not a pure shell script, but is an Autoconf script using M4, where are quote characters.:




To fully understand where proper quotation is important, you first
need to know what the special characters are in Autoconf: ‘#’
introduces a comment inside which no macro expansion is performed,
‘,’ separates arguments, ‘[’ and ‘]’ are the quotes themselves,
‘(’ and ‘)’ (which M4 tries to match by pairs), and finally ‘$’
inside a macro definition.



Each time there can be a macro expansion, there is a quotation
expansion, i.e., one level of quotes is stripped:



 int tab[10];
⇒int tab10;
[int tab[10];]
⇒int tab[10];



That [[3456]] should be expanded by autoconf to [3456], which would be the correct output for use as a shell script.






share|improve this answer
















  • 1




    Indeed - as one can verify by adding a minimal AC_INIT([foo]) to the top of the snippet and then executing autoconf snippet
    – steeldriver
    Oct 4 at 2:27











  • Okay, thanks. I've read about m4 and they used the quote characters ` and ' in those articles. So the must have changed the quotes in the configure.ac to treat [ and ] as quotes.
    – supmethods
    Oct 4 at 3:56










  • The link you provided use [ and ] as quotes. Does this depend on M4 version used?
    – supmethods
    Oct 4 at 4:01










  • @supmethods you can change the quote characters in M4, and it seems [ ] are set as quote characters in autoconf scripts. The default quote characters in M4 are `' , as you have seen.
    – muru
    Oct 4 at 4:17













up vote
4
down vote










up vote
4
down vote









If that's a from a configure.ac file, then it's not a pure shell script, but is an Autoconf script using M4, where are quote characters.:




To fully understand where proper quotation is important, you first
need to know what the special characters are in Autoconf: ‘#’
introduces a comment inside which no macro expansion is performed,
‘,’ separates arguments, ‘[’ and ‘]’ are the quotes themselves,
‘(’ and ‘)’ (which M4 tries to match by pairs), and finally ‘$’
inside a macro definition.



Each time there can be a macro expansion, there is a quotation
expansion, i.e., one level of quotes is stripped:



 int tab[10];
⇒int tab10;
[int tab[10];]
⇒int tab[10];



That [[3456]] should be expanded by autoconf to [3456], which would be the correct output for use as a shell script.






share|improve this answer












If that's a from a configure.ac file, then it's not a pure shell script, but is an Autoconf script using M4, where are quote characters.:




To fully understand where proper quotation is important, you first
need to know what the special characters are in Autoconf: ‘#’
introduces a comment inside which no macro expansion is performed,
‘,’ separates arguments, ‘[’ and ‘]’ are the quotes themselves,
‘(’ and ‘)’ (which M4 tries to match by pairs), and finally ‘$’
inside a macro definition.



Each time there can be a macro expansion, there is a quotation
expansion, i.e., one level of quotes is stripped:



 int tab[10];
⇒int tab10;
[int tab[10];]
⇒int tab[10];



That [[3456]] should be expanded by autoconf to [3456], which would be the correct output for use as a shell script.







share|improve this answer












share|improve this answer



share|improve this answer










answered Oct 4 at 2:16









muru

33.9k578147




33.9k578147







  • 1




    Indeed - as one can verify by adding a minimal AC_INIT([foo]) to the top of the snippet and then executing autoconf snippet
    – steeldriver
    Oct 4 at 2:27











  • Okay, thanks. I've read about m4 and they used the quote characters ` and ' in those articles. So the must have changed the quotes in the configure.ac to treat [ and ] as quotes.
    – supmethods
    Oct 4 at 3:56










  • The link you provided use [ and ] as quotes. Does this depend on M4 version used?
    – supmethods
    Oct 4 at 4:01










  • @supmethods you can change the quote characters in M4, and it seems [ ] are set as quote characters in autoconf scripts. The default quote characters in M4 are `' , as you have seen.
    – muru
    Oct 4 at 4:17













  • 1




    Indeed - as one can verify by adding a minimal AC_INIT([foo]) to the top of the snippet and then executing autoconf snippet
    – steeldriver
    Oct 4 at 2:27











  • Okay, thanks. I've read about m4 and they used the quote characters ` and ' in those articles. So the must have changed the quotes in the configure.ac to treat [ and ] as quotes.
    – supmethods
    Oct 4 at 3:56










  • The link you provided use [ and ] as quotes. Does this depend on M4 version used?
    – supmethods
    Oct 4 at 4:01










  • @supmethods you can change the quote characters in M4, and it seems [ ] are set as quote characters in autoconf scripts. The default quote characters in M4 are `' , as you have seen.
    – muru
    Oct 4 at 4:17








1




1




Indeed - as one can verify by adding a minimal AC_INIT([foo]) to the top of the snippet and then executing autoconf snippet
– steeldriver
Oct 4 at 2:27





Indeed - as one can verify by adding a minimal AC_INIT([foo]) to the top of the snippet and then executing autoconf snippet
– steeldriver
Oct 4 at 2:27













Okay, thanks. I've read about m4 and they used the quote characters ` and ' in those articles. So the must have changed the quotes in the configure.ac to treat [ and ] as quotes.
– supmethods
Oct 4 at 3:56




Okay, thanks. I've read about m4 and they used the quote characters ` and ' in those articles. So the must have changed the quotes in the configure.ac to treat [ and ] as quotes.
– supmethods
Oct 4 at 3:56












The link you provided use [ and ] as quotes. Does this depend on M4 version used?
– supmethods
Oct 4 at 4:01




The link you provided use [ and ] as quotes. Does this depend on M4 version used?
– supmethods
Oct 4 at 4:01












@supmethods you can change the quote characters in M4, and it seems [ ] are set as quote characters in autoconf scripts. The default quote characters in M4 are `' , as you have seen.
– muru
Oct 4 at 4:17





@supmethods you can change the quote characters in M4, and it seems [ ] are set as quote characters in autoconf scripts. The default quote characters in M4 are `' , as you have seen.
– muru
Oct 4 at 4:17


















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f473123%2fbash-code-snippet-with-double-square-bracket-i345686%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

How to check contact read email or not when send email to Individual?

Bahrain

Postfix configuration issue with fips on centos 7; mailgun relay