It is currently Wed Apr 25, 2018 10:32 am

parameter substitution "+ +"

All times are UTC - 6 hours

Post new topic Reply to topic  [ 3 posts ] 
Author Message
 PostPosted: Fri Jan 22, 2016 3:44 am   

Joined: Tue Nov 17, 2015 6:30 am
Posts: 70
Hi, I came across the following function
function sman() { #usage: sman cmd pattern
  LESS="$LESS${2:+ +/$2}" man "$1"

It highlights keywords on the given command man page, for example
$ sman ls sort
will highlight all the occurrences of sort for the ls man page.
I don't understand the following though?
="$LESS${2:+ +/$2}"

My understanding of the above code is that if $2 is NOT empty, then
is used instead, but I don't understand why the space and plus are prefixed to $2?

Could somebody help me understand how this works?

 PostPosted: Fri Jan 22, 2016 5:20 am   

Joined: Mon Oct 20, 2014 9:53 am
Posts: 574
less got a huge manpage, It is woth reading.
A very mighty command.

less can search for strings or regexes.
If you less a file and type /something the file is searched for the string "something" and each finding is highlighted.

And the + is a command within less.
You can use it to override options given in the $LESS environment variable or execute another less- command.

LESS="$LESS${2:+ +/$2}" reads like:

LESS=" we set the var LESS
$LESS to the value it already had. (Usually something like -M -I -R)
${2:+ concatenate with value of $2 IF set. (Yes, you read it correct.)
| +/ and add a blank and the + option, which triggers the search / command. The blank is needed for less syntax. (I prefixed the blank with | as this forum wouldn't display the blank otherwise.)
$2}" and finally is the search term we want to see highlighted.

Same like writing LESS="-M -I -R +/something" man ls something

man just searches the given manpage and decompresses it. For printing it to STDOUT it calls less

 PostPosted: Fri Jan 22, 2016 7:04 am   

Joined: Tue Nov 17, 2015 6:30 am
Posts: 70
ok, thanks, never realised less could be used in so many ways.

the man less page states
Options are also taken from the environment variable "LESS".
when I run
$ env | grep LESS
LESSOPEN=|/usr/bin/ %s
there is no $LESS variable, so no predefined options exist.
Also, running the following:
man ls | less +/sort
is the same as
sman ls sort

Anyway, point is, I understand now, many thanks... once again.

Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours

Who is online

Users browsing this forum: No registered users and 19 guests

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  

BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group