Changing volume at command line using script/function

applescriptbashterminal

I found this nifty command which sets volume to 50% (or any level desired):

osascript -e 'set volume output volume 50'

I'd love to have a script or function that lets me type v 50 or v 30, etc. to easily change the volume levels.

I tried making this bash function:

v() {
# adjust volume function
osascript -e 'set volume output volume $1'
}

However this gives:

25:26: syntax error: Expected expression but found unknown token. (-2741)

How can I make this work?

Best Answer

Single quotes ('') suppress parameter expansion so you have to use double quotes:

v() {
    osascript -e "set volume output $1"
}