arithmetic expression: expecting primary
Mia Lopez
I'm trying to add a function to the .bashrc file.
The function is supposed to change any Application window's opacity according to the command line argument.
opa(){ sh -c 'xprop -f _NET_WM_WINDOW_OPACITY 32c -set _NET_WM_WINDOW_OPACITY $(printf 0x%x $((0xffffffff * $1 / 100)))'
}I tried the above however it throws this error:
sh: 1: arithmetic expression: expecting primary: "0xffffffff * / 100"
usage: xprop [-options ...] [[format [dformat]] atom] ...How do I fix this? Also, is there any way to set a default value for the $1 in case the user doesn't pass any command line argument?
1 Answer
It makes no sense (to me at least) to start a subshell within a shell function - particularly not a sh shell inside .bashrc
Instead, you could just do
opa(){ xprop -f _NET_WM_WINDOW_OPACITY 32c -set _NET_WM_WINDOW_OPACITY $(printf 0x%x $((0xffffffff * $1 / 100)))
}If you want to supply a default value, you can do so using the ${parameter:-word} or ${parameter-word} syntax (depending on whether you want the default to take effect when $1 is undefined or empty, or only when it is undefined)
i.e.
opa(){ xprop -f _NET_WM_WINDOW_OPACITY 32c -set _NET_WM_WINDOW_OPACITY $(printf 0x%x $((0xffffffff * ${1:-60} / 100)))
}If you really want to use a sh -c '...' subshell, you'd need to pass $1 from the parent bash shell into the child sh shell
opa(){ sh -c 'xprop -f _NET_WM_WINDOW_OPACITY 32c -set _NET_WM_WINDOW_OPACITY $(printf 0x%x $((0xffffffff * $1 / 100)))' sh "${1:-60}"
} 0