Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does “${x%% *}” mean in sh?

Tags:

shell

sh

I just saw "$${x%% *}" in a makefile, which means "${x%% *}" in sh. Why it is written in 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 ${x%% *} in Google ?

like image 648
Galaxy Avatar asked May 07 '13 08:05

Galaxy


People also ask

What does ${ x mean in shell scripting?

Invoking a Bash shell with the -x option causes each shell command to be printed before it is executed. This is especially useful for diagnosing problems with installation shell scripts.

What does %% mean in Bash?

The operator "%" will try to remove the shortest text matching the pattern, while "%%" tries to do it with the longest text matching.

What does ${ 1 mean in shell script?

$1 is the first command-line argument passed to the shell script. Also, know as Positional parameters. For example, $0, $1, $3, $4 and so on. If you run ./script.sh filename1 dir1, then: $0 is the name of the script itself (script.sh)

What is ${ 0 * in shell script?

${0} is the first argument of the script, i.e. the script name or path. If you launch your script as path/to/script.sh , then ${0} will be exactly that string: path/to/script.sh . The %/* part modifies the value of ${0} . It means: take all characters until / followed by a file name.


1 Answers

The ${x%% *} substitutes the value of x variable with the leftmost space and everything to the right of it removed.

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

To the second question I have no answer.

like image 141
spbnick Avatar answered Oct 22 '22 15:10

spbnick