Bash – Does command substitution within arithmetic substitution get word split

arithmeticbashwords

I seem to recall from comments on this site that the contents of arithmetic expansion may be word split, but I can't find the comment again.

Consider the following code:

printf '%d\n' "$(($(sed -n '/my regex/{=;q;}' myfile)-1))"

If the sed command outputs a multi-digit number and $IFS contains digits, will the command substitution get word split before the arithmetic occurs?

(I've already tested using extra double quotes:

printf '%d\n' "$(("$(sed -n '/my regex/{=;q;}' myfile)"-1))"

and this doesn't work.)


Incidentally the example code above is a reduced-to-simplest-form alteration of this function that I just posted on Stack Overflow.

Best Answer

No, it doesn't.

In $((expression)), expression is treated as it was in double quote, as POSIX specified.

But beware that the expression inside command substitution still be subjected to split+glob:

$ printf '%d\n' "$(( $(IFS=0; a=10; echo $a) + 1 ))"
2

With double quote:

$ printf '%d\n' "$(( $(IFS=0; a=10; echo "$a") + 1 ))"
11

Like other expansions, arithmetic expansion, if not inside double quote, undergo split+glob:

$ IFS=0
$ echo $((10))
1
Related Question