$ somescript $var
#!/bin/bash
echo ${1
[*]}
ok, but is there any way to send an array as a single argument?
for example, if someone ran a script where $var is an array:
Code:$ somescript $var
Referencing an array variable without a subscript is equivalent to referencing element zero.
`man bash` said:Any element of an array may be referenced using ${name[subscript]}. The braces are required to avoid conflicts with pathname expansion. If subscript is @ or *, the word expands to all members of name. These subscripts differ only when the word appears within double quotes. If the word is double-quoted, ${name[*]} expands to a single word with the value of each array member separated by the first character of the IFS special variable, and ${name[@]} expands each element of name to a separate word. When there are no array members, ${name[@]} expands to nothing.
is there some way it could be accessed, for example, like this?
Code:#!/bin/bash echo ${1 [*]}
`man bash` said:Special Parameters
The shell treats several parameters specially. These parameters may
only be referenced; assignment to them is not allowed.* Expands to the positional parameters, starting from one. When
the expansion occurs within double quotes, it expands to a sin-
gle word with the value of each parameter separated by the first
character of the IFS special variable. That is, "$*" is equiva-
lent to "$1c$2c...", where c is the first character of the value
of the IFS variable. If IFS is unset, the parameters are sepa-
rated by spaces. If IFS is null, the parameters are joined
without intervening separators.
@ Expands to the positional parameters, starting from one. When
the expansion occurs within double quotes, each parameter
expands to a separate word. That is, "$@" is equivalent to "$1"
"$2" ... When there are no positional parameters, "$@" and $@
expand to nothing (i.e., they are removed).
#!/bin/bash
for i in $@; do
echo $i
done
#!/bin/bash
for i in "$@"; do
echo $i
done
`man bash` said:Arrays are assigned to using compound assignments of the form name=(value1 ... valuen), where each value is of the form [subscript]=string.
foo="abc def ghi jkl"
bar=($foo)
#!/bin/bash
foo=($@)
echo ${#foo[*]}
Just curious, what does the @ symbol represent/how is it used?
#!/bin/bash
#
# arraystr
# By: James Brumond
# Created: 6 February, 2009
#
# Compiles an array into a single string.
# Use: arraystr [ -d del ] ARRAY STRING
#
# If the -d option is passed, then the value `del`
# refers to the delimiter between the array indecies.
# If the -d option is not passed, indecies are not
# delimited at all.
#
# Exit Code Definitions:
# 161 - Too Few Args Passed.
# 162 - Too Many Args Passed.
#
helpmsg () {
echo 'USE: arraystr [-d del] ARRAY STRING
OPTIONS:
-h, --help Displays this help message.
-d del Sets the delimiter to del.
+d Does not delimit indecies. (default)'
exit $1
}
startup () {
if [ $# -eq 0 -o $1 = -h -o $1 = --help ]
then
helpmsg 0
elif [ $# -eq 1 ]
then
echo -e "Invalid Use!\n"
helpmsg 161
elif [ $# -eq 2 ]
then
if [ $1 = -d -o $1 = +d ]
then
echo -e "Invalid Use!\n"
helpmsg 161
else
x=0
count=${#1
[*]}
while [ $x -le $count ]
do
typeset $2[$x]=`echo $2`${1[$x]}
x=$((++x))
done
exit 0
fi
elif [ $# -eq 3 ]
then
if [ $1 = +d ]
then
x=0
count=${#2
[*]}
while [ $x -le $count ]
do
typeset $3[$x]=`echo $3`${2[$x]}
x=$((++x))
done
exit 0
else
echo -e "Invalid Use!"
helpmsg 161
fi
elif [ $# -eq 4 ]
then
if [ $1 = -d ]
then
: # this part under construction...
else
echo -e "Invalid Use!"
helpmsg 161
fi
else
echo -e "Invalid Use!"
helpmsg 162
fi
}
startup $@
arraystr ${var
[*]} other
because "${name[*]}" expands to a single word with the value of each array member separated by the first character of the IFS special variable (which is a space character by default).`man bash` said:Any element of an array may be referenced using ${name[subscript]}. The braces are required to avoid conflicts with pathname expansion. If subscript is @ or *, the word expands to all members of name. These subscripts differ only when the word appears within double quotes. If the word is double-quoted, ${name[*]} expands to a single word with the value of each array member separated by the first character of the IFS special variable, and ${name[@]} expands each element of name to a separate word.
That is, `somescript "${var[*]}"` will pass $var as a single argument to somescript [...]
regardless of if there is a reason for the script or not, i'm telling you that it sends the array as separate arguments, not a single one.