Which character to escape for this to work in ~/.bashrc

I want to add this iostat command to my .bashrc and create an alias for it:

iostat -xk 2 $(findmnt -T ~ | awk 'END {print $2}')

I'm adding this to my .bashrc:

alias ios='iostat -xk 2 $(findmnt -T ~ | awk 'END {print $2}')'

Unfortunately, it doesn't work.

If I run the above iostat command in a terminal it works, but when I run the ios alias it no longer works.

And yes, I restart my shell every time.


Use a function instead; the quoting is simpler:

ios () {
  iostat -xk 2 $(findmnt -T ~ | awk 'END {print $2}')
}

alias ios='iostat -xk 2 $(findmnt -T ~ | awk 'END {print $2}')'

you can not use quotes inside quotes. Alternate between quote and doublequote is the easiest method. So this works:

$ alias ios="iostat -xk 2 $(findmnt -T ~ | awk 'END {print $2}')"
$

From edit:

Note that although it works in this case (because ~ isn't going to move to a different block device in the meantime), the "soft" outer quotes cause the command substitution to be evaluated at definition time rather than dynamically on alias invocation

If you need single quotes you can glue them together using '"'"':

$ alias ios='iostat -xk 2 $(findmnt -T ~ | awk '"'"'END {print $2}'"'"')'
$