3

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.

1
  • FYI you can likely avoid the issue by telling findmnt to only output the information you actually want ex. findmnt -no SOURCE -T ~ and dropping the pipe to awk altogether Sep 24, 2020 at 12:27

3 Answers 3

9

Use a function instead; the quoting is simpler:

ios () {
  iostat -xk 2 $(findmnt -T ~ | awk 'END {print $2}')
}
0
6
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}'"'"')'
$
4
  • It's working @Rinzwind , thank you !
    – assilmanss
    Sep 24, 2020 at 12:22
  • 5
    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 Sep 24, 2020 at 12:24
  • Understood, thank you @steeldriver !
    – assilmanss
    Sep 24, 2020 at 12:29
  • 3
    With the double quotes, the command substitution will run when you define the alias, not when you use it.
    – chepner
    Sep 24, 2020 at 22:35
0

Nested quotes are always trouble.

You can do it like this:

echo 'Hello '\'~\'' world'

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .