What does the "$ {x %% *}" mean in sh?

I just saw "$$ {x %% *}" in the makefile, which means "$ {x %% *}" in sh. Why is it written this way?

https://unix.stackexchange.com/questions/28994/how-can-a-makefile-detect-whether-a-command-is-available-in-the-local-machine

determine_sum = \ sum=; \ for x in sha1sum sha1 shasum 'openssl dgst -sha1'; do \ if type "$${x%% *}" >/dev/null 2>/dev/null; then sum=$$x; break; fi; \ done; \ if [ -z "$$sum" ]; then echo 1>&2 "Unable to find a SHA1 utility"; exit 2; fi checksums.dat: FORCE $(determine_sum); \ $$sum *.org 

Also, how to search for ${x%% *} in Google?

+7
source share
2 answers

${x%% *} is a great way to remove everything except the first word in the $x variable. That is, given the text XXX YYY ZZZ , this syntax will delete everything after the first space.

Some examples:

 $ x="aaa abbb" $ echo ${x%% *} aaa $ x="aaa abbb bccc defasdfs" $ echo ${x%% *} aaa 

This is equivalent to:

 $ x="aaa abbb bccc defasdfs" $ echo $x | awk '{print $1}' aaa 

To make sure this is clear, let's give another example:

 $ x="aaa abbb_bccc_defasdfs" $ echo ${x%%_*} aaa abbb 

We look for the first character _ and delete it from this point to the end.

To learn more about these interesting commands, you can check out Reference Cards #String Operations .


Regarding the Google question, yes, these characters are not searchable through their search engine. Instead, I would advise you to use SymbolHound .

For example, the query $ {x %% *} shows a valuable result.

0
source

${x%% *} replaces the value of the variable x with the leftmost space and everything that is to the right of it.

See http://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html

In the second question I have no answer.

+2
source

All Articles