Possum Gallo Possum Gallo - 4 months ago 12
Bash Question

Bash error with '+' operand // Quotes misuse

☿[~]$ alias hdd='echo Σ= $(($(df -BMB /dev/sdb1 --output=used | tail -1 | grep -o '[0-9]*')+$(df -BMB /dev/sdc1 --output=used | tail -1 | grep -o '[0-9]*'))) Mb'


This alias suddenly stopped doing its job:

☿[~]$ hdd
bash: +: syntax error: operand expected (error token is "+")


But the command still works:

☿[~]$ echo Σ= $(($(df -BMB /dev/sdb1 --output=used | tail -1 | grep -o '[0-9]*')+$(df -BMB /dev/sdc1 --output=used | tail -1 | grep -o '[0-9]*'))) Mb
Σ= 3782845 Mb

Answer

Don't use an alias; define some functions instead.

get_space_used () {
    df -BMB "$1" --output=used | tail -1 | grep -o '[0-9]*'
}
hdd () {
    sdb1=$(get_space_used /dev/sdb1)
    sdc1=$(get_space_used /dev/sdc1)
    echo "$(( sdb1 + sdc1 ))"
}

This makes quoting easier, refactors duplicate code, and makes it much easier to pinpoint what the problem is in the event of an error. In your case, there was a problem with the second df pipe, since bash was attempting to execute something like echo $(( foo + )).