So, what is the purpose of allowing arbitrary name in form env var=value
and was it allowed by POSIX?
Quoting from POSIX: Environment Variables:
Environment variable names used by the utilities in the Shell and Utilities volume of POSIX.1-2008 consist solely of uppercase letters, digits, and the ( '_' ) from the characters defined in Portable Character Set and do not begin with a digit. Other characters may be permitted by an implementation; applications shall tolerate the presence of such names.
Note: Other applications may have difficulty dealing with environment variable names that start with a digit. For this reason, use of such names is not recommended anywhere.
So implementations of env
may permit arbitrary environment variable names - and most, if not all, implementations do so, accepting every non-NUL character to the left of an '='
- and implementations of other utilities (such as the shell) may or may not permit arbitrary names.
The statement that name=value ... utility
is equivalent to env var="value" utility
will only be true if the implementation of env
and the shell both permit name
to be an environment variable.
There's an interesting Austin Group thread about this issue here: Invalid shell assignments in environment. One point mentioned is that shells generally only allow environment variables whose names can be represented as shell variables. Several participants in that thread participate in unix.stackexchange.com and can hopefully add some more info about the issue.
From man ksh
:
An arithmetic expression uses the same syntax, precedence, and associativity of expression as the C language. All the C language operators that apply to floating point quantities can be used... Variables can be referenced by name within an arithmetic expression without using the parameter expansion syntax. When a variable is referenced, its value is evaluated as an arithmetic expression...
A conditional expression is used with the [[
compound command to test attributes of files and to compare strings. Field splitting and file name generation are not performed on the words between [[
and ]]
. Each expression can be constructed from one or more of the following unary or binary expressions...
The following obsolete arithmetic comparisons are also permitted:
exp1
-eq
exp2
- True, if
exp1
is equal to exp2
.
exp1
-ne
exp2
- True, if
exp1
is not equal to exp2
.
exp1
-lt
exp2
- True, if
exp1
is less than exp2
.
exp1
-gt
exp2
- True, if
exp1
is greater than exp2
.
exp1
-le
exp2
- True, if
exp1
is less than or equal to exp2
.
exp1
-ge
exp2
- True, if
exp1
is greater than or equal to exp2
.
The documentation there is consistent where references to arithmetic expressions are concerned, and (apparently carefully) avoids any self-contradictions surrounding the definition of the [[
compound command ]]
pertaining to string comparison by explicitly also permitting some obsolete arithmetic comparisons in the same context.
From man bash
:
[[
expression
]]
- Return a status of
0
or 1
depending on the evaluation of the condi‐ tional expression expression
. Expressions are composed of the primaries described below... Word splitting and pathname expansion are not performed on the words between the [[
and ]]
; ~
tilde expansion, ${
parameter}
and $
variable expansion, $((
arithmetic expansion))
, $(
command substitution)
, <(
process substitution)
, and "\'
quote removal are performed. Conditional operators such as -f
must be unquoted to be recognized as primaries...
A variable may be assigned to by a statement of the form:
If value
is not given, the variable is assigned the null string. All values undergo ~
tilde expansion, ${
parameter}
and $
variable expansion, $(
command substitution)
, $((
arithmetic expansion))
, and "\'
quote removal... If the variable has its integer attribute set, then value
is evaluated as an $((
arithmetic expression))
even if the $((
...
))
expansion is not used...
The shell allows arithmetic expressions
to be evaluated, under certain circumstances... Evaluation is done in fixed-width integers with no check for overflow, though division by 0 is trapped and flagged as an error. The operators and their precedence, associativity, and values are the same as in the C language.
Shell variables are allowed as operands; parameter expansion is performed before the expression is evaluated. Within an expression
, shell variables may also be referenced by name without using the parameter expansion syntax. The value of a variable is evaluated as an arithmetic expression
when it is referenced, or when a variable which has been given the integer attribute using declare -i
is assigned a value... A shell variable need not have its integer attribute turned on to be used in an expression.
Conditional expressions
are used by the [[
compound command and the test
and [
builtin commands to test file attributes and perform string and arithmetic comparisons...
arg1
OP
arg2
OP
is one of -eq
, -ne
, -lt
, -le
, -gt
, or -ge
. These arithmetic binary operators return true if arg1
is equal to, not equal to, less than, less than or equal to, greater than, or greater than or equal to arg2
, respectively. arg1
and arg2
may be positive or negative integers.
I think, given all that context, the behavior you observe stands to reason, even if it is not explicitly spelled out as a possibility in the documentation there. The docs do point to special treatment of parameters with integer attributes, and clearly denote a difference between a compound command and a builtin command.
The [[
comparison is syntax in the same sense that the assignment name
=
value
is syntax or case
word
in...
is syntax. test
and [
, however, are not as such, and are rather separate procedures which take arguments. As I think, the best way to really get a feel for the differences is to have a look at shell error output:
set '[[ \\ -eq 0 ]]' '[ \\ -eq 0 ]'
for sh in bash ksh
do for exp
do "$sh" -c "$1||$2"
set "$2" "$1"
done; done
bash: [[: \: syntax error: operand expected (error token is "\")
bash: line 0: [: \: integer expression expected
bash: line 0: [: \: integer expression expected
bash: [[: \: syntax error: operand expected (error token is "\")
ksh: \: arithmetic syntax error
ksh: [: \: arithmetic syntax error
ksh: \: arithmetic syntax error
The two shells handle the exceptions differently, but the underlying reasons for the differences in both cases for both shells are very similar.
bash
directly calls the [[ \\
case a syntax error - in the same way it might for a redirect from a non-existent file, for example - though it goes on from that point (as I believe, incorrectly) to evaluate the other side of the ||
or expression. bash
does give the [[
expression a command name in error output, but note that it doesn't bother discussing the line number on which you call it as it does for the [
command. bash
's [
complains about not receiving what it expects to be an integer expression as an argument, but [[
need not complain in that way because it doesn't really take arguments, and never needs to expect anything at all when it is parsed alongside the expansions themselves.
ksh
halts altogether when the [[
syntax error and doesn't bother with [
at all. It writes the same error message for both, but note that [
is assigned a command name there where [[
is just ksh
. The [
is only called after the command-line has been successfully parsed and expansions have already occurred - it will do its own little getopts
routine and get its own arg[0c]
and the rest, but [[
is handled as underlying shell syntax once again.
I consider the bash
docs slightly less clear than the ksh
version in that they use the terms arg[12]
rather than expression
regarding integer comparisons, but I think it is done merely because [[
, [
, and test
are all lumped together at that juncture, and the latter two do take arguments whereas the former only ever receives an expression
.
In any case, where the integer comparison is not ambiguous in the syntax context, you can basically do any valid math operation mid-expression
:
m=5+5 a[m]=10
[[ m -eq 10 ]] &&
[[ m++ -eq 10 ]] &&
[[ m-- -gt 10 ]] &&
[[ ${a[m]} == 10 ]] &&
echo "math evals"
math evals
Best Answer
When they are not quoted,
$*
and$@
are the same. You shouldn't use either of these, because they can break unexpectedly as soon as you have arguments containing spaces or wildcards."$*"
expands to a single word"$1c$2c..."
. Usuallyc
is a space, but it's actually the first character ofIFS
, so it can be anything you choose.The only good use I've ever found for it is:
join arguments with comma (simple version)
join arguments with the specified delimiter (better version)
"$@"
expands to separate words:"$1"
"$2"
...
This is almost always what you want. It expands each positional parameter to a separate word, which makes it perfect for taking command line or function arguments in and then passing them on to another command or function. And because it expands using double quotes, it means things don't break if, say,
"$1"
contains a space or an asterisk (*
).Let's write a script called
svim
that runsvim
withsudo
. We'll do three versions to illustrate the difference.svim1
svim2
svim3
All of them will be fine for simple cases, e.g. a single file name that doesn't contain spaces:
But only
$*
and"$@"
work properly if you have multiple arguments.And only
"$*"
and"$@"
work properly if you have arguments containing spaces.So only
"$@"
will work properly all the time.typeset
is how to make a local variable inksh
(bash
andash
uselocal
instead). It meansIFS
will be restored to its previous value when the function returns. This is important, because the commands you run afterward might not work properly ifIFS
is set to something non-standard.