All quotes passed on the command line are handled by the shell (in this case bash
). After all variables got expanded the resulting strings are passed as arguments to the executed program, where arguments are delimited by (unquoted) space on the command line.
Therefore in your examples
cqlsh "${CASSANDRA_IP}" -u "${CASSANDRA_USER}" -p "${CASSANDRA_PASSWORD}" -f command.cql
is equivalent to
cqlsh 00.000.00.00 -u user -p 'password' -f command.cql
which is equivalent to
cqlsh 00.000.00.00 -u user -p password -f command.cql
i.e. the shell will call the program cqlsh
passing it 7 arguments
00.000.00.00
-u
user
-p
password
-f
command.cql
Quoting correctly is very important nevertheless because of how shells handle variable expansion, most importantly by this rules (this is for bash
but should be applicable for most other shells too):
- strings in single quotes
'
are not subject to any variable expansion and get passed literally
- strings in double quotes
"
are subject to variable expansion: the final result of the expansion will be passed as one string to the program
- unquoted variables will be expanded on the command line and the resulting command line then gets executed
Most of the time the end result of enclosing variables in double quotes and passing them unquoted is the same, but it may be different depending on the value of the variable, especially when containing space characters: in this case the result will be passed as multiple strings to the program instead of a single string. E.g.
CASSANDRA_PASSWORD="my password" # password contains a space
cqlsh $CASSANDRA_IP -u $CASSANDRA_USER -p $CASSANDRA_PASSWORD -f command.cql
would be evaluated to
cqlsh 00.000.00.00 -u user -p my password -f command.cql
i.e. cqlsh
would be called with 8 arguments instead of the intended 7:
00.000.00.00
-u
user
-p
my
password
-f
command.cql
whereas
cqlsh "$CASSANDRA_IP" -u "$CASSANDRA_USER" -p "$CASSANDRA_PASSWORD" -f command.cql
would correctly evaluate to
cqlsh 00.000.00.00 -u user -p "my password" -f command.cql