Shell Script – How to Use ‘ in Alias?

aliasescape-charactersquotingshell-script

I have one-line that I want to call using alias.

while printf '%s ' "$(df -P / | awk 'NR==2 { print $(NF-1) }')"; do sleep 30; done

I tried to escape ' like \' but it didn't work.

What is the correct syntax to use above one-liner in alias?

Best Answer

alias my_du=$'while printf \'%s \' "$(df -P / | awk \'NR==2 { print $(NF-1) }\')"; do sleep 3; done'

You can check the result with

alias my_du

If $() is quoted by " instead of ' or \ then it is substituted and the result rather than the intended program call becomes part of the alias definition.

Related Question