metacharacter processing for default value in variable expansion
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
When a default value is provided for a variable expansion (e.g. $varname-default
), how are the characters that make up default
meant to be interpreted?
Naively, I would have guessed that variable substitution (e.g. $b), and expansion of quote-like characters '
, "
, would occur in this context ... but it appears that expansion of quote-like characters does not always occur.
For example,
> echo x$a-''y
xy
but
> echo x"$a-''"y
x''y
I expected xy
in both cases, since construction that expand variables like $a-$PATH
and ( BRACE='}'; echo $a-$BRACE )
, work fine.
However GNU bash, version 4.4.19(1)-release
and ... the latest version of mksh
both have the same behavior where ''
is included literally in the output for the second example.
shell
add a comment |Â
up vote
0
down vote
favorite
When a default value is provided for a variable expansion (e.g. $varname-default
), how are the characters that make up default
meant to be interpreted?
Naively, I would have guessed that variable substitution (e.g. $b), and expansion of quote-like characters '
, "
, would occur in this context ... but it appears that expansion of quote-like characters does not always occur.
For example,
> echo x$a-''y
xy
but
> echo x"$a-''"y
x''y
I expected xy
in both cases, since construction that expand variables like $a-$PATH
and ( BRACE='}'; echo $a-$BRACE )
, work fine.
However GNU bash, version 4.4.19(1)-release
and ... the latest version of mksh
both have the same behavior where ''
is included literally in the output for the second example.
shell
1
It's expected behavior and in POSIX spec pubs.opengroup.org/onlinepubs/9699919799/utilities/â¦
â cuonglm
Aug 17 at 3:30
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
When a default value is provided for a variable expansion (e.g. $varname-default
), how are the characters that make up default
meant to be interpreted?
Naively, I would have guessed that variable substitution (e.g. $b), and expansion of quote-like characters '
, "
, would occur in this context ... but it appears that expansion of quote-like characters does not always occur.
For example,
> echo x$a-''y
xy
but
> echo x"$a-''"y
x''y
I expected xy
in both cases, since construction that expand variables like $a-$PATH
and ( BRACE='}'; echo $a-$BRACE )
, work fine.
However GNU bash, version 4.4.19(1)-release
and ... the latest version of mksh
both have the same behavior where ''
is included literally in the output for the second example.
shell
When a default value is provided for a variable expansion (e.g. $varname-default
), how are the characters that make up default
meant to be interpreted?
Naively, I would have guessed that variable substitution (e.g. $b), and expansion of quote-like characters '
, "
, would occur in this context ... but it appears that expansion of quote-like characters does not always occur.
For example,
> echo x$a-''y
xy
but
> echo x"$a-''"y
x''y
I expected xy
in both cases, since construction that expand variables like $a-$PATH
and ( BRACE='}'; echo $a-$BRACE )
, work fine.
However GNU bash, version 4.4.19(1)-release
and ... the latest version of mksh
both have the same behavior where ''
is included literally in the output for the second example.
shell
shell
asked Aug 17 at 3:22
Gregory Nisbet
1,222818
1,222818
1
It's expected behavior and in POSIX spec pubs.opengroup.org/onlinepubs/9699919799/utilities/â¦
â cuonglm
Aug 17 at 3:30
add a comment |Â
1
It's expected behavior and in POSIX spec pubs.opengroup.org/onlinepubs/9699919799/utilities/â¦
â cuonglm
Aug 17 at 3:30
1
1
It's expected behavior and in POSIX spec pubs.opengroup.org/onlinepubs/9699919799/utilities/â¦
â cuonglm
Aug 17 at 3:30
It's expected behavior and in POSIX spec pubs.opengroup.org/onlinepubs/9699919799/utilities/â¦
â cuonglm
Aug 17 at 3:30
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
The $parameter:-[word]
expansion is subject to several expansions:
From man bash:
In each of the cases below, word is subject to tilde expansion, parameter expansion, command substitution, and arithmetic expansion.
From POSIX spec:
word shall be subjected to tilde expansion, parameter expansion, command substitution, and arithmetic expansion.
Both report the same, and both do not include "quote removal".
The quotes get removed after the expansion of the whole variable has already been done. However, it is difficult to show exactly how that is done:
$ sh
$ unset b
$ set -x
+ set -x
$ echo 1 $b-e
+ echo 1 e
1 e
$ echo 2 $b-"e"
+ echo 2 e
2 e
$ echo 3 $b-'e'
+ echo 3 e
3 e
$ echo 4 "$b-e"
+ echo 4 e
4 e
$ echo 5 "$b-"e""
+ echo 5 e
5 e
$ echo 6 "$b-'e'"
+ echo 6 'e'
6 'e'
$ set +x
+ set +x
Or:
$ $ set -x; echo 1 x$b-ey; echo 2 x$b-"e"y; echo 3 x$b-'e'y; echo 4 "x$b-ey"; echo 5 "x$b-"e"y"; echo 6 "x$b-'e'y"; set +x
+ echo 1 xey
1 xey
+ echo 2 xey
2 xey
+ echo 3 xey
3 xey
+ echo 4 xey
4 xey
+ echo 5 xey
5 xey
+ echo 6 x'e'y
6 x'e'y
+ set +x
$
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
The $parameter:-[word]
expansion is subject to several expansions:
From man bash:
In each of the cases below, word is subject to tilde expansion, parameter expansion, command substitution, and arithmetic expansion.
From POSIX spec:
word shall be subjected to tilde expansion, parameter expansion, command substitution, and arithmetic expansion.
Both report the same, and both do not include "quote removal".
The quotes get removed after the expansion of the whole variable has already been done. However, it is difficult to show exactly how that is done:
$ sh
$ unset b
$ set -x
+ set -x
$ echo 1 $b-e
+ echo 1 e
1 e
$ echo 2 $b-"e"
+ echo 2 e
2 e
$ echo 3 $b-'e'
+ echo 3 e
3 e
$ echo 4 "$b-e"
+ echo 4 e
4 e
$ echo 5 "$b-"e""
+ echo 5 e
5 e
$ echo 6 "$b-'e'"
+ echo 6 'e'
6 'e'
$ set +x
+ set +x
Or:
$ $ set -x; echo 1 x$b-ey; echo 2 x$b-"e"y; echo 3 x$b-'e'y; echo 4 "x$b-ey"; echo 5 "x$b-"e"y"; echo 6 "x$b-'e'y"; set +x
+ echo 1 xey
1 xey
+ echo 2 xey
2 xey
+ echo 3 xey
3 xey
+ echo 4 xey
4 xey
+ echo 5 xey
5 xey
+ echo 6 x'e'y
6 x'e'y
+ set +x
$
add a comment |Â
up vote
1
down vote
accepted
The $parameter:-[word]
expansion is subject to several expansions:
From man bash:
In each of the cases below, word is subject to tilde expansion, parameter expansion, command substitution, and arithmetic expansion.
From POSIX spec:
word shall be subjected to tilde expansion, parameter expansion, command substitution, and arithmetic expansion.
Both report the same, and both do not include "quote removal".
The quotes get removed after the expansion of the whole variable has already been done. However, it is difficult to show exactly how that is done:
$ sh
$ unset b
$ set -x
+ set -x
$ echo 1 $b-e
+ echo 1 e
1 e
$ echo 2 $b-"e"
+ echo 2 e
2 e
$ echo 3 $b-'e'
+ echo 3 e
3 e
$ echo 4 "$b-e"
+ echo 4 e
4 e
$ echo 5 "$b-"e""
+ echo 5 e
5 e
$ echo 6 "$b-'e'"
+ echo 6 'e'
6 'e'
$ set +x
+ set +x
Or:
$ $ set -x; echo 1 x$b-ey; echo 2 x$b-"e"y; echo 3 x$b-'e'y; echo 4 "x$b-ey"; echo 5 "x$b-"e"y"; echo 6 "x$b-'e'y"; set +x
+ echo 1 xey
1 xey
+ echo 2 xey
2 xey
+ echo 3 xey
3 xey
+ echo 4 xey
4 xey
+ echo 5 xey
5 xey
+ echo 6 x'e'y
6 x'e'y
+ set +x
$
add a comment |Â
up vote
1
down vote
accepted
up vote
1
down vote
accepted
The $parameter:-[word]
expansion is subject to several expansions:
From man bash:
In each of the cases below, word is subject to tilde expansion, parameter expansion, command substitution, and arithmetic expansion.
From POSIX spec:
word shall be subjected to tilde expansion, parameter expansion, command substitution, and arithmetic expansion.
Both report the same, and both do not include "quote removal".
The quotes get removed after the expansion of the whole variable has already been done. However, it is difficult to show exactly how that is done:
$ sh
$ unset b
$ set -x
+ set -x
$ echo 1 $b-e
+ echo 1 e
1 e
$ echo 2 $b-"e"
+ echo 2 e
2 e
$ echo 3 $b-'e'
+ echo 3 e
3 e
$ echo 4 "$b-e"
+ echo 4 e
4 e
$ echo 5 "$b-"e""
+ echo 5 e
5 e
$ echo 6 "$b-'e'"
+ echo 6 'e'
6 'e'
$ set +x
+ set +x
Or:
$ $ set -x; echo 1 x$b-ey; echo 2 x$b-"e"y; echo 3 x$b-'e'y; echo 4 "x$b-ey"; echo 5 "x$b-"e"y"; echo 6 "x$b-'e'y"; set +x
+ echo 1 xey
1 xey
+ echo 2 xey
2 xey
+ echo 3 xey
3 xey
+ echo 4 xey
4 xey
+ echo 5 xey
5 xey
+ echo 6 x'e'y
6 x'e'y
+ set +x
$
The $parameter:-[word]
expansion is subject to several expansions:
From man bash:
In each of the cases below, word is subject to tilde expansion, parameter expansion, command substitution, and arithmetic expansion.
From POSIX spec:
word shall be subjected to tilde expansion, parameter expansion, command substitution, and arithmetic expansion.
Both report the same, and both do not include "quote removal".
The quotes get removed after the expansion of the whole variable has already been done. However, it is difficult to show exactly how that is done:
$ sh
$ unset b
$ set -x
+ set -x
$ echo 1 $b-e
+ echo 1 e
1 e
$ echo 2 $b-"e"
+ echo 2 e
2 e
$ echo 3 $b-'e'
+ echo 3 e
3 e
$ echo 4 "$b-e"
+ echo 4 e
4 e
$ echo 5 "$b-"e""
+ echo 5 e
5 e
$ echo 6 "$b-'e'"
+ echo 6 'e'
6 'e'
$ set +x
+ set +x
Or:
$ $ set -x; echo 1 x$b-ey; echo 2 x$b-"e"y; echo 3 x$b-'e'y; echo 4 "x$b-ey"; echo 5 "x$b-"e"y"; echo 6 "x$b-'e'y"; set +x
+ echo 1 xey
1 xey
+ echo 2 xey
2 xey
+ echo 3 xey
3 xey
+ echo 4 xey
4 xey
+ echo 5 xey
5 xey
+ echo 6 x'e'y
6 x'e'y
+ set +x
$
edited Aug 17 at 5:34
answered Aug 17 at 4:48
Isaac
7,1241835
7,1241835
add a comment |Â
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%2f463104%2fmetacharacter-processing-for-default-value-in-variable-expansion%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
1
It's expected behavior and in POSIX spec pubs.opengroup.org/onlinepubs/9699919799/utilities/â¦
â cuonglm
Aug 17 at 3:30