How to create alias with a command contains ' and "
Saying that the syntax of an alias is alias aliasname='command'
is a bit misleading, as it seems to imply that the single quotes are part of the syntax. They are not. The part after the equal sign is similar to variable assignments, in that it can be any shell word, composed either of plain characters (without quotes), or a quoted string, or a combination.
These are all valid, and the last three equivalent:
alias ks=ls
alias ls='ls -l'
alias ls="ls -l"
alias ls=ls\ -l
So, all you need to do is to escape the quotes properly to have them inside the alias value.
See, e.g. this answer and other answers to e.g. these question for discussion on that:
- How to enclose in quotes if both single and double quotes are already used?
- Print a string including single quotes and other special characters
Or, use function instead of a alias to get rid of quoting issues completely:
freemem() {
free | awk '/^Mem/ { printf("free: %.2f %\n", $4/$2 * 100.0) }'
}
You need:
alias aliasname="free | awk '/^Mem/ { printf(\"free: %.2f %\n\", \$4/\$2 * 100.0) }'"
Notice that you need to escape both "
and $
.