Reputation: 4523
I am having a problem in defining an alias in bash.
I am writing the alias as:
alias mypr="enscript -jC -p output.ps -b '$n %W Page $% of $='"
But when I type alias mypr, I am getting this (the $n is vanishing, and some extra quotes are coming in):
alias mypr='enscript -jC -p output.ps -b '\'' %W Page $% of $='\'''
I tried looking at the answer at 53398792, but cant figure out what mistake I am making. Suggestions please?
Upvotes: 0
Views: 291
Reputation: 364
try something like this
alias mypr="enscript -jC -p output.ps -b \'"'\$n %W Page \$% of \$='"\'"
I tested with this more simple case:
# this outputs '$2'
alias tt="echo \'"'\$2'"\'"
So what I did is I escaped all the dollar signs and put them inside single quoted string and the quotes escaped them also but put them inside double quoted string
The idea is you can glue sequences together, so you can have something like "lala"'blabla'
and it will work just fine
Upvotes: 0
Reputation: 85570
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 $=\'"
Upvotes: 2
Reputation: 140970
$n
is expanded in "
quotes. Because most probably variable n
is empty, it expands to nothing. Escape it.
alias mypr="enscript -jC -p output.ps -b '\$n %W Page \$% of \$='"
Or use single quotes:
alias mypr='enscript -jC -p output.ps -b '\''$n %W Page $% of $='\'
Or just use a function:
mypr() { enscript -jC -p output.ps -b '$n %W Page $% of $=' "$@"; }
Upvotes: 1