Skip to main content
Commonmark migration
Source Link

The tutorial's use of the word "substring" is slightly misleading. When using ${variable#pattern}, we're dealing with matching and deleting a prefix string (and with ${variable%pattern} a suffix string).

You removed the shortest prefix string matching *. from the two strings bash.string.txt and bashshell.string.txt. The result for both strings is the same, string.txt, because the pattern *. matches up to and including the first dot in the string.

The POSIX standard defines this particular parameter expansion as

${parameter#[word]}

 

Remove Smallest Prefix Pattern. The word shall be expanded to produce a pattern. The parameter expansion shall then result in parameter, with the smallest portion of the prefix matched by the pattern deleted. If present, word shall not begin with an unquoted #.

Had you wanted to get the result bashshell.txt, you would have had to remove the string .string or string. from the middle of the string. This could be done in two steps with standard parameter expansions:

suffix=${filename##*.}          # remove everything to the *last* dot
echo "${filename%%.*}.$suffix"  # remove everything from the first dot and add suffix

The ## and %% variations of the parameter expansion removes the longest matching prefix and suffix strings respectively.

Alternatively with bash:

echo "${filename/string./}"

This removes the (first occurrence of the) given string anywhere within the value of $filename.

The tutorial's use of the word "substring" is slightly misleading. When using ${variable#pattern}, we're dealing with matching and deleting a prefix string (and with ${variable%pattern} a suffix string).

You removed the shortest prefix string matching *. from the two strings bash.string.txt and bashshell.string.txt. The result for both strings is the same, string.txt, because the pattern *. matches up to and including the first dot in the string.

The POSIX standard defines this particular parameter expansion as

${parameter#[word]}

 

Remove Smallest Prefix Pattern. The word shall be expanded to produce a pattern. The parameter expansion shall then result in parameter, with the smallest portion of the prefix matched by the pattern deleted. If present, word shall not begin with an unquoted #.

Had you wanted to get the result bashshell.txt, you would have had to remove the string .string or string. from the middle of the string. This could be done in two steps with standard parameter expansions:

suffix=${filename##*.}          # remove everything to the *last* dot
echo "${filename%%.*}.$suffix"  # remove everything from the first dot and add suffix

The ## and %% variations of the parameter expansion removes the longest matching prefix and suffix strings respectively.

Alternatively with bash:

echo "${filename/string./}"

This removes the (first occurrence of the) given string anywhere within the value of $filename.

The tutorial's use of the word "substring" is slightly misleading. When using ${variable#pattern}, we're dealing with matching and deleting a prefix string (and with ${variable%pattern} a suffix string).

You removed the shortest prefix string matching *. from the two strings bash.string.txt and bashshell.string.txt. The result for both strings is the same, string.txt, because the pattern *. matches up to and including the first dot in the string.

The POSIX standard defines this particular parameter expansion as

${parameter#[word]}

Remove Smallest Prefix Pattern. The word shall be expanded to produce a pattern. The parameter expansion shall then result in parameter, with the smallest portion of the prefix matched by the pattern deleted. If present, word shall not begin with an unquoted #.

Had you wanted to get the result bashshell.txt, you would have had to remove the string .string or string. from the middle of the string. This could be done in two steps with standard parameter expansions:

suffix=${filename##*.}          # remove everything to the *last* dot
echo "${filename%%.*}.$suffix"  # remove everything from the first dot and add suffix

The ## and %% variations of the parameter expansion removes the longest matching prefix and suffix strings respectively.

Alternatively with bash:

echo "${filename/string./}"

This removes the (first occurrence of the) given string anywhere within the value of $filename.

added 216 characters in body
Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k

The tutorial's use of the word "substring" is slightly misleading. When using ${variable#pattern}, we're dealing with matching and deleting a prefix string (and with ${variable%pattern} a suffix string).

You removed the shortest prefix string matching *. from the two strings bash.string.txt and bashshell.string.txt. The result for both strings is the same, string.txt, because the pattern *. matches up to and including the first dot in the string.

The POSIX standard defines this particular parameter expansion as

${parameter#[word]}

Remove Smallest Prefix Pattern. The word shall be expanded to produce a pattern. The parameter expansion shall then result in parameter, with the smallest portion of the prefix matched by the pattern deleted. If present, word shall not begin with an unquoted #.

Had you wanted to get the result bashshell.txt, you would have had to remove the string .string or string. from the middle of the string. This could be done in two steps with standard parameter expansions:

suffix=${filename##*.}          # remove everything to the *last* dot
echo "${filename%%.*}.$suffix"  # remove everything from the first dot and add suffix

The ## and %% variations of the parameter expansion removes the longest matching prefix and suffix strings respectively.

Alternatively with bash:

echo "${filename/string./}"

This removes the (first occurrence of the) given string anywhere within the value of $filename.

You removed the shortest prefix string matching *. from the two strings bash.string.txt and bashshell.string.txt. The result for both strings is the same, string.txt, because the pattern *. matches up to and including the first dot in the string.

The POSIX standard defines this particular parameter expansion as

${parameter#[word]}

Remove Smallest Prefix Pattern. The word shall be expanded to produce a pattern. The parameter expansion shall then result in parameter, with the smallest portion of the prefix matched by the pattern deleted. If present, word shall not begin with an unquoted #.

Had you wanted to get the result bashshell.txt, you would have had to remove the string .string or string. from the middle of the string. This could be done in two steps with standard parameter expansions:

suffix=${filename##*.}          # remove everything to the *last* dot
echo "${filename%%.*}.$suffix"  # remove everything from the first dot and add suffix

The ## and %% variations of the parameter expansion removes the longest matching prefix and suffix strings respectively.

Alternatively with bash:

echo "${filename/string./}"

This removes the (first occurrence of the) given string anywhere within the value of $filename.

The tutorial's use of the word "substring" is slightly misleading. When using ${variable#pattern}, we're dealing with matching and deleting a prefix string (and with ${variable%pattern} a suffix string).

You removed the shortest prefix string matching *. from the two strings bash.string.txt and bashshell.string.txt. The result for both strings is the same, string.txt, because the pattern *. matches up to and including the first dot in the string.

The POSIX standard defines this particular parameter expansion as

${parameter#[word]}

Remove Smallest Prefix Pattern. The word shall be expanded to produce a pattern. The parameter expansion shall then result in parameter, with the smallest portion of the prefix matched by the pattern deleted. If present, word shall not begin with an unquoted #.

Had you wanted to get the result bashshell.txt, you would have had to remove the string .string or string. from the middle of the string. This could be done in two steps with standard parameter expansions:

suffix=${filename##*.}          # remove everything to the *last* dot
echo "${filename%%.*}.$suffix"  # remove everything from the first dot and add suffix

The ## and %% variations of the parameter expansion removes the longest matching prefix and suffix strings respectively.

Alternatively with bash:

echo "${filename/string./}"

This removes the (first occurrence of the) given string anywhere within the value of $filename.

added 130 characters in body
Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k

You removed the shortest prefix string matching *. from the two strings bash.string.txt and bashshell.string.txt. The result for both strings is the same, string.txt, because the pattern *. matches up to and including the first dot in the string.

The POSIX standard defines this particular parameter expansion as

${parameter#[word]}

Remove Smallest Prefix Pattern. The word shall be expanded to produce a pattern. The parameter expansion shall then result in parameter, with the smallest portion of the prefix matched by the pattern deleted. If present, word shall not begin with an unquoted #.

Had you wanted to get the result bashshell.txt, you would have had to remove the string .string or string. from the middle of the string. This could be done in two steps with standard parameter expansions:

suffix=${filename##*.}          # remove everything to the *last* dot
echo "${filename%%.*}.$suffix"  # remove everything from the first dot and add suffix

The ## and %% variations of the parameter expansion removes the longest matching prefix and suffix strings respectively.

Alternatively with bash:

echo "${filename/string./}"

This removes the (first occurrence of the) given string anywhere within the value of $filename.

You removed the shortest prefix string matching *. from the two strings bash.string.txt and bashshell.string.txt. The result for both strings is the same, string.txt, because the pattern *. matches up to and including the first dot in the string.

The POSIX standard defines this particular parameter expansion as

${parameter#[word]}

Remove Smallest Prefix Pattern. The word shall be expanded to produce a pattern. The parameter expansion shall then result in parameter, with the smallest portion of the prefix matched by the pattern deleted. If present, word shall not begin with an unquoted #.

Had you wanted to get the result bashshell.txt, you would have had to remove the string .string or string. from the middle of the string. This could be done in two steps with standard parameter expansions:

suffix=${filename##*.}          # remove everything to the *last* dot
echo "${filename%%.*}.$suffix"  # remove everything from the first dot and add suffix

Alternatively with bash:

echo "${filename/string./}"

This removes the (first occurrence of the) given string anywhere within the value of $filename.

You removed the shortest prefix string matching *. from the two strings bash.string.txt and bashshell.string.txt. The result for both strings is the same, string.txt, because the pattern *. matches up to and including the first dot in the string.

The POSIX standard defines this particular parameter expansion as

${parameter#[word]}

Remove Smallest Prefix Pattern. The word shall be expanded to produce a pattern. The parameter expansion shall then result in parameter, with the smallest portion of the prefix matched by the pattern deleted. If present, word shall not begin with an unquoted #.

Had you wanted to get the result bashshell.txt, you would have had to remove the string .string or string. from the middle of the string. This could be done in two steps with standard parameter expansions:

suffix=${filename##*.}          # remove everything to the *last* dot
echo "${filename%%.*}.$suffix"  # remove everything from the first dot and add suffix

The ## and %% variations of the parameter expansion removes the longest matching prefix and suffix strings respectively.

Alternatively with bash:

echo "${filename/string./}"

This removes the (first occurrence of the) given string anywhere within the value of $filename.

added 1 character in body
Source Link
Jeff Schaller
  • 68.8k
  • 35
  • 122
  • 265
Loading
added 550 characters in body
Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k
Loading
Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k
Loading