3

I just tried to pass a search pattern starting with $ to less:

less +/'\$l' myfile.pl

and I got

There is no -l option ("less --help" for help)

After reading the manual, I tried the alternate -p-option

less -p'\$l' myfile.pl

and got the same error.

Then I found, that (n.b. the space after -p)

less -p '\$l' myfile.pl

works, i.e. opens the file without error message and jumps to first occurrence of $l.

The long option variant --pattern=$l does not work

less --pattern='\$l' myfile.pl
There is no -l option ("less --help" for help)

whereas space instead of = works

less --pattern '\$l' myfile.pl

This is

less -V
less 458 (POSIX regular expressions)
Copyright (C) 1984-2012 Mark Nudelman

on openSUSE Leap 42.3

Now the question: is this a bug?

0

That's caused by a feature. From man less:

Some options like -k or -D require a string to follow the option letter. The string for that option is considered to end when a dollar sign ($) is found. For example, you can set two -D options on MS-DOS like this:

LESS="Dn9.1$Ds4.1"

So you could do:

LESS='ppattern$i' PAGER=less man less

To start less, looking for pattern case-insensitively.

--use-backslash may be used to allow \ to escape that $.

Now, while it makes sense for the $LESS variable, it makes little sense for arguments to options on the command line. In that, it could be seen as a bug which you may want to report.

Also note that that $ processing only occurs when that $ occurs in the same argument as that containing the option which explains the discrepancy between your -p '\$l' vs -p'\$l' and --pattern '\$l' vs --pattern='\$l' causing confusion and more justification that it may be seen as a bug.

Note that you'll also have problems for:

less -p' foo'

Where less looks for "foo" instead of " foo" (again use -p ' foo' instead). Look at the scan_option() function in options.c in the source for details.

  • but this does not really explain the different behaviour when called with a space or not for the short -p resp. space or = for --pattern – mararm Apr 16 '18 at 15:34
  • @mararm, see edit. – Stéphane Chazelas Apr 16 '18 at 15:58
0

From man less:

   --use-backslash
          This option changes the interpretations of options which  follow
          this one.  After the --use-backslash option, any backslash in an
          option string is removed and the following  character  is  taken
          literally.   This  allows a dollar sign to be included in option
          strings.

We then need to escape both the backslash and the dollar, so

less --use-backslash +'/\\\$l' myfile.pl

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.