Syntax With Pound and Percent Sign After Shell Parameter Name

Syntax with pound and percent sign after shell parameter name

See Parameter Expansion in man bash:

   ${parameter#word}
${parameter##word}

Remove matching prefix pattern. The word is expanded to produce a pattern just
as in pathname expansion. If the pattern matches the beginning of the value of
parameter, then the result of the expansion is the expanded value of parameter
with the shortest matching pattern (the # case) or the longest matching pattern (the ## case) deleted. If parameter is @ or *, the pattern removal
operation is applied to each positional parameter in turn, and the expansion is
the resultant list. If parameter is an array variable subscripted with @ or *,
the pattern removal operation is applied to each member of the array in turn,
and the expansion is the resultant list.

  ${parameter%word}
${parameter%%word}

Remove matching suffix pattern. The word is expanded to produce a pattern just
as in pathname expansion. If the pattern matches a trailing portion of the
expanded value of parameter, then the result of the expansion is the expanded
value of parameter with the shortest matching pattern (the % case) or the
longest matching pattern (the %% case) deleted. If parameter is @ or *,
the pattern removal operation is applied to each positional parameter in turn,
and the expansion is the resultant list. If parameter is an array variable
subscripted with @ or *, the pattern removal operation is applied to each mem-
ber of the array in turn, and the expansion is the resultant list.

In short, # removes the pattern from the left, % from the right, doubling the symbol makes the matching greedy. (Mnemonic: # is to the left of % on most keyboards).

percent symbol in Bash, what's it used for?

Delete the shortest match of string in $var from the beginning:

${var#string}

Delete the longest match of string in $var from the beginning:

${var##string}

Delete the shortest match of string in $var from the end:

${var%string}

Delete the longest match of string in $var from the end:

${var%%string}

Try:

var=foobarbar
echo "${var%b*r}"
> foobar
echo "${var%%b*r}"
> foo

What is the meaning of the ${0##...} syntax with variable, braces and hash character in bash?

See the section on Substring removal in the Advanced Bash-Scripting Guide‡:

${string#substring}

Deletes shortest match of substring from front of $string.

${string##substring}

Deletes longest match of substring from front of $string.

The substring may include a wildcard *, matching everything. The expression ${0##/*} prints the value of $0 unless it starts with a forward slash, in which case it prints nothing.

‡ The guide, as of 3/7/2019, mistakenly claims that the match is of $substring, as if substring was the name of a variable. It's not: substring is just a pattern.

What means #! in ${variable#!} ? (bash)

it removes the leading ! from the value of the variable.

an example would be:

kent$  foo='!!hello'   

kent$ echo ${foo#!}
!hello

${0%/*} and ${0##*/} in sh

These are shell parameter expansions:

  • ${var%/*} - remove everything after the last occurrence of /.
  • ${var##*/} - remove everything up to the last occurrence of /.

Since you are in a script, $0 refers to the name of the script itself.

All together, this is returning either the path or the name of the script that you are running. So your are kind of doing:

BREW_FILE_DIRECTORY=$(chdir <path_of_script> && pwd -P)
export HOMEBREW_BREW_FILE="$BREW_FILE_DIRECTORY/<script_name>"

Test

$ r="hello/how/are/you"
$ echo ${r%/*}
hello/how/are
$ echo ${r##*/}
you

From the above-mentioned link (edited versions to make them shorter):

${parameter##word}

The word is expanded to produce a pattern just as in filename
expansion. If the pattern matches the beginning of the expanded value
of parameter, then the result of the expansion is the expanded value
of parameter with the longest matching pattern (the ‘##’ case)
deleted. If parameter is ‘@’ or ‘*’, the pattern removal operation is
applied to each positional parameter in turn, and the expansion is the
resultant list.

${parameter%word}

The word is expanded to produce a pattern just as in filename
expansion. If the pattern matches a trailing portion of the expanded
value of parameter, then the result of the expansion is the value of
parameter with the shortest matching pattern (the ‘%’ case) deleted.
If parameter is ‘@’ or ‘*’, the pattern removal operation is applied
to each positional parameter in turn, and the expansion is the
resultant list.

And regarding $0 itself, see Bash reference manual -> 6.1 Invoking bash:

If arguments remain after option processing, and neither the -c nor
the -s option has been supplied, the first argument is assumed to be
the name of a file containing shell commands (see Shell Scripts). When
Bash is invoked in this fashion, $0 is set to the name of the file,
and the positional parameters are set to the remaining arguments. Bash
reads and executes commands from this file, then exits.

What are the special dollar sign shell variables?

  • $1, $2, $3, ... are the positional parameters.
  • "$@" is an array-like construct of all positional parameters, {$1, $2, $3 ...}.
  • "$*" is the IFS expansion of all positional parameters, $1 $2 $3 ....
  • $# is the number of positional parameters.
  • $- current options set for the shell.
  • $$ pid of the current shell (not subshell).
  • $_ most recent parameter (or the abs path of the command to start the current shell immediately after startup).
  • $IFS is the (input) field separator.
  • $? is the most recent foreground pipeline exit status.
  • $! is the PID of the most recent background command.
  • $0 is the name of the shell or shell script.

Most of the above can be found under Special Parameters in the Bash Reference Manual. There are all the environment variables set by the shell.

For a comprehensive index, please see the Reference Manual Variable Index.

pattern matching in Bash

The manpage for bash says:

${parameter#word}

${parameter##word}

Remove matching prefix pattern. The word is expanded to produce a pattern just as
in pathname expansion. If the pattern matches the beginning of the value of parameter, then the result of the expansion is the expanded value of parameter with the
shortest matching pattern (the # case) or the longest matching pattern (the
## case) deleted. If parameter is @ or *, the pattern removal operation is
applied to each positional parameter in turn, and the expansion is the resultant
list. If parameter is an array variable subscripted with @ or *, the pattern
removal operation is applied to each member of the array in turn, and the expansion
is the resultant list.


${parameter%word}

${parameter%%word}

Remove matching suffix pattern. The word is expanded to produce a pattern just as
in pathname expansion. If the pattern matches a trailing portion of the expanded
value of parameter, then the result of the expansion is the expanded value of
parameter with the shortest matching pattern (the % case) or the longest matching pattern (the %% case) deleted. If parameter is @ or *, the pattern removal
operation is applied to each positional parameter in turn, and the expansion is the
resultant list. If parameter is an array variable subscripted with @ or *, the
pattern removal operation is applied to each member of the array in turn, and the
expansion is the resultant list.



Related Topics



Leave a reply



Submit