0

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?

R71
  • 4,283
  • 7
  • 32
  • 60
  • @codeforester/KamilCuk: I disagree with the close vote due to the difference in quotes was causing the problem. OP's attempt clearly shows using both quote types in their attempt – Inian Apr 17 '20 at 08:50
  • A more canonical dupe suggestion would be to link one that explains why quotes inside alias cause problems and also possibly if it explains an alternative of suggesting functions over aliases – Inian Apr 17 '20 at 08:51

3 Answers3

2

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 $=\'"
Inian
  • 80,270
  • 14
  • 142
  • 161
  • thanks for the detailed explanation. But I find @KamilCuk's aliases less complex – R71 Apr 17 '20 at 13:28
1

$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 $=' "$@"; }
KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • Your first and second option don't work as it loses the single quotes, after the `-b` arg – Inian Apr 17 '20 at 09:19
0

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

robert.baboi
  • 354
  • 1
  • 5