Avoid using alias
definitions for anything other than really simple command shortcuts like e.g. alias cdw="cd /home/users/foo/bar/workspace"
. For anything involves more than one command construct or involving arguments, quotes always prefer functions over alias-es
Your function alternative could simply be written as below. This is 100% portable on any Unix shell and can be added to the startup file, your shell is using
mypr() {
enscript -jC -p output.ps -b '$n %W Page $% of $='
}
Your alias definition didn't work, because the shell didn't like the way your $
character is handled. Right when you define the alias to be under ".."
, when resolving the alias definition, it tries to expands the contents within the quotes as part of which it does variable expansion. So any token that contains a $
prefixed before is expanded, so shell tries to expand $n
and it does not see a value, so it keeps a empty string.
To avoid this, you should have either defined your alias to be inside single quotes '..'
or escaped all the $
tokens, so that they are not resolved during definition time. Since $%
and $=
are not any special shell variable or user defined variables, they don't get expanded, and are treated literally.
So, with our first stage of escaping, we add a single escape() to $n
and define it as below
alias mypr="enscript -jC -p output.ps -b '\$n %W Page $% of $='"
You will immediately see that now you have retained $n
but lost the outer quotes '..'
as part of the quote removal, so you need to escape that now
alias mypr="enscript -jC -p output.ps -b \'\$n %W Page $% of $=\'"
You will find that, even the above doesn't work, when calling your alias, because the shell has still managed to expand $n
. So introducing another level of escape and escaping that is what you would be needing.
alias mypr="enscript -jC -p output.ps -b \'\\\$n %W Page $% of $=\'"