BASH: Code snippet with double square bracket i[[3456]]86
Clash 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?
bash
add a comment |Â
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?
bash
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
add a comment |Â
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?
bash
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
bash
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
add a comment |Â
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
add a comment |Â
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.
1
Indeed - as one can verify by adding a minimalAC_INIT([foo])
to the top of the snippet and then executingautoconf 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
add a comment |Â
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.
1
Indeed - as one can verify by adding a minimalAC_INIT([foo])
to the top of the snippet and then executingautoconf 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
add a comment |Â
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.
1
Indeed - as one can verify by adding a minimalAC_INIT([foo])
to the top of the snippet and then executingautoconf 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
add a comment |Â
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.
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.
answered Oct 4 at 2:16
muru
33.9k578147
33.9k578147
1
Indeed - as one can verify by adding a minimalAC_INIT([foo])
to the top of the snippet and then executingautoconf 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
add a comment |Â
1
Indeed - as one can verify by adding a minimalAC_INIT([foo])
to the top of the snippet and then executingautoconf 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
add a comment |Â
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%2f473123%2fbash-code-snippet-with-double-square-bracket-i345686%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
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