Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions concepts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ Redirection ||--|| "Here Documents" : ""

- option parsing with getopts

- advanced topics expanding on previous topics (see the "advanced-*.md" files):
- word splitting
- filename expansion
- globbing

- `set` command and "strict mode"

- pros and cons of
Expand Down
27 changes: 27 additions & 0 deletions concepts/advanced-filename-expansion.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Advanced Filename Expansion

(Note this material is for a future concept)

## Disable Filename Expansion

You can use the [`set`][set] command to disable filename expansion.

```bash
echo * # prints a list of files
set -f # disable filename expansion
echo * # prints a literal asterisk
set +f # enable filename expansion
```

## Controlling Filename Expansion

There are several settings for the builtin [`shopt`][shopt] command that control how filename expansion operates.
A couple of interesting ones are:

* `shopt -s nocaseglob` -- perform case insensitive matching
* `shopt -s extglob` -- enable extended patterns
* `shopt -s nullglob` -- if no files match, replace the pattern with nothing.
The default behaviour is to leave the pattern in place as a literal string.

[set]: https://www.gnu.org/software/bash/manual/bash.html#The-Set-Builtin
[shopt]: https://www.gnu.org/software/bash/manual/bash.html#The-Shopt-Builtin
67 changes: 67 additions & 0 deletions concepts/advanced-globbing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# Advanced Globbing

(Note this material is for a future concept)

## Character Sets

There are lots of details about character sets in glob patterns.

* `[abc]` matches exactly one character that is either an `a` or a `b` or a `c`.
* to include a literal close bracket in the expression, make it the first character after the opening bracket (after an optional `^`)
* `[][]` is a character set that will match either a close or an open bracket.
* or escape it with a backslash: `[[\]]`
* `[x-y]` matches exactly one character that is in the _range_ from `x` to `y`
* to include a literal hyphen character in a character set, it must be either the first character after the opening bracket (after an optional `^`) or the last character before the closing bracket.
* `[-xy]` or `[xy-]` each match exactly one character that is an `-` or a `x` or a `y`
* or escape it with a backslash: `[x\-y]`
* multiple ranges can be specified, for example `[0-9A-Fa-f]` matches a hexadecimal digit
* `[^abc]` matches exactly one character that is **NOT** an `a` or a `b` or a `c`,
* `[^x-y]` matches exactly one character that is **NOT** in the range from `x` to `y`
* `[[:character_class:]]` matches one character that is in the named "character_class"
* the available character classes are:
* `lower` -- lowercase letters
* `upper` -- uppercase letters
* `alpha` -- letters
* `digit` -- digits
* `alnum` -- letters and digits
* `space` -- whitespace
* `blank` -- horizontal whitespace (space and tab)
* `xdigit` -- hexadecimal digits
* `word` -- characters allowed in an identifier (letters, numbers, underscore)
* `punct` -- punctuation
* `cntrl` -- control characters
* `graph` -- "visible" characters
* `print` -- visible characters plus space
* they can be combined:
* the `[[:alpha:]]` class consists of `[[:lower:][:upper:]]`
* the `[[:alnum:]]` class consists of `[[:alpha:][:digit:]]`
* the `[[:word:]]` class consists of `[[:alnum:]_]`
* underscore (octal 137) is both `[[:word:]]` and `[[:punct:]]`
* the `[[:graph:]]` class consists of `[[:alnum:][:punct:]]`
* the `[[:print:]]` class consists of `[[:graph:] ]` -- just space (octal 040) not any other whitespace.

## Glob Patterns Are Not Regular Expressions

Glob patterns **are not** regular expressions.

A glob pattern must match the _entire_ filename, but a regular expression can match only part of a string.
For example, the glob `a` matches only a file named exactly `a`, while the regex `a` matches any string that contains an `a`.
To get the same result with a glob, use `*a*`.

* match exactly one character
* regex `.`
* glob `?`
* match zero or more characters
* regex `.*`
* glob `*`
* the glob pattern `.*` means "match a filename where the first character is a literal dot followed by zero or more of any characters".
* match one or more characters
* regex `.+`
* glob `?*`
* regex character sets and glob character sets act the same way.

## Extended Patterns

Bash provides [extended patterns][extended-patterns] that are closer to regular expressions.

[extended-patterns]: https://www.gnu.org/software/bash/manual/bash.html#Pattern-Matching
38 changes: 38 additions & 0 deletions concepts/advanced-word-splitting.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Advanced Word Splitting

(Note this material is for a future concept)
Comment thread
glennj marked this conversation as resolved.
Resolved

Any results of parameter expansion, command substitution or arithmetic expansion _that did not occur within double quotes_ are eligible for "word splitting".
The shell takes the expanded result, and splits it on any sequence of characters present in the value of `$IFS`.
By default, this is space, tab and newline.

Let's see it with a different value of `$IFS`:

```bash
csv="first,second,third"
IFS=","
for word in $csv; do echo "$word"; done
```
outputs
```none
first
second
third
```

## Disable Word Splitting

A technique to disable word splitting is to set `$IFS` to an empty string

```bash
sentence="The quick brown fox jumps."
IFS= # or explicitly, IFS=""
printf '%s\n' $sentence
```
outputs
```none
The quick brown fox jumps.
```

However, leaving variables unquoted is not recommended unless you also disable Filename Expansion.
See [Advanced Filename Expansion](advanced-filename-expansion.md) to learn how.
Loading
Loading