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.

  • 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 – steeldriver Sep 24 at 12:27
10

Use a function instead; the quoting is simpler:

ios () {
  iostat -xk 2 $(findmnt -T ~ | awk 'END {print $2}')
}
| improve this answer | |
  • Thank you so much @chepner ! – assilmanss Oct 3 at 9:29
5
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}'"'"')'
$
| improve this answer | |
  • It's working @Rinzwind , thank you ! – assilmanss Sep 24 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 – steeldriver Sep 24 at 12:24
  • Understood, thank you @steeldriver ! – assilmanss Sep 24 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 at 22:35
0

Nested quotes are always trouble.

You can do it like this:

echo 'Hello '\'~\'' world'
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.