#if ENABLE_ASH_BASH_COMPAT
case VSSUBSTR:
+//TODO: support more general format ${v:EXPR:EXPR},
+// where EXPR follows $(()) rules
loc = str = stackblock() + strloc;
/* Read POS in ${var:POS:LEN} */
pos = atoi(loc); /* number(loc) errors out on "1:4" */
STPUTC('=', out);
flags = 0;
if (subtype == 0) {
+ static const char types[] ALIGN1 = "}-+?=";
/* ${VAR...} but not $VAR or ${#VAR} */
/* c == first char after VAR */
switch (c) {
case ':':
c = pgetc();
#if ENABLE_ASH_BASH_COMPAT
- if (c == ':' || c == '$' || isdigit(c)) {
-//TODO: support more general format ${v:EXPR:EXPR},
-// where EXPR follows $(()) rules
+ /* This check is only needed to not misinterpret
+ * ${VAR:-WORD}, ${VAR:+WORD}, ${VAR:=WORD}, ${VAR:?WORD}
+ * constructs.
+ */
+ if (!strchr(types, c)) {
subtype = VSSUBSTR;
pungetc();
break; /* "goto do_pungetc" is bigger (!) */
flags = VSNUL;
/*FALLTHROUGH*/
default: {
- static const char types[] ALIGN1 = "}-+?=";
const char *p = strchr(types, c);
if (p == NULL)
goto badsub;
--- /dev/null
+parameter=abcdef
+offset=2
+noffset=-2
+echo "parameter '${parameter}'"
+echo "varoffset2 '${parameter:${offset}}'"
+echo "varoffset-2 '${parameter:${noffset}}'"
+echo "literal '2' '${parameter:2}'"
+# This is not inrpreted as ${VAR:POS{:LEN}},
+# but as ${VAR:=WORD} - if VAR is unset or null, substitute WORD
+echo "literal '-2' '${parameter:-2}'"
+echo "literal ' -2' '${parameter: -2}'"