|
From: | Adam Danischewski |
Subject: | Re: Bash $@ Parameter Variable Breaking Out of Strings |
Date: | Tue, 22 Mar 2016 11:13:14 -0400 |
I noticed another interesting occurrence as well, I'm not sure if they are related, to variable names:I noticed an issue using the parameter built-in variable $@ breakingout of contained strings when utilized in functions.
For example, consider the following bash script: m.bsh
#!/bin/bash
echo "$#"
while getopts d: OPTION "$@"; do
case "$OPTION" in
d)
echo "the optarg is ${OPTARG##*=}, optind is ${OPTIND}"
[[ -z "${OPTARG}" ]] && echo "Let's set the null byte as the delim."
;;
esac
done
exit 0
$ alias t1='_() { var=$@; /tmp/m.bsh -d "clarify ${var}"; }; _'
$ t1 hi there
2
the optarg is clarify hi there, optind is 3
### Correctly considers the text as a single string argument
containing a space.
$ alias t2='_() { /tmp/m.bsh -d "clarify $@"; }; _'
$ t2 hi there
3
the optarg is clarify hi, optind is 3
### Incorrectly breaks the argument array variable out as separate
single string arguments.
function update() {
local -i VAR=45
VAR+=-1
VAR+=$1
echo $VAR
}
$ VAR2=2
$ update VAR2
47
$ VAR=3
$ update VAR
88 ### !?
[Prev in Thread] | Current Thread | [Next in Thread] |