mirror of
https://github.com/jlevy/the-art-of-command-line.git
synced 2024-12-12 10:45:00 +02:00
Add a note about brace expansion
- Mention that brace expansion is performed before any other expansion. - Use `more *` instead of `head` for printing file contents with headings. - Specify that `<(command>` refers to process substitution.
This commit is contained in:
parent
1703740c0d
commit
3d68a61592
@ -140,9 +140,9 @@ Notes:
|
||||
|
||||
- In Bash, note there are lots of kinds of variable expansion. Checking a variable exists: `${name:?error message}`. For example, if a Bash script requires a single argument, just write `input_file=${1:?usage: $0 input_file}`. Using a default value if a variable is empty: `${name:-default}`. If you want to have an additional (optional) parameter added to the previous example, you can use something like `output_file=${2:-logfile}`. If $2 is omitted and thus empty, `output_file` will be set to `logfile`. Arithmetic expansion: `i=$(( (i + 1) % 5 ))`. Sequences: `{1..10}`. Trimming of strings: `${var%suffix}` and `${var#prefix}`. For example if `var=foo.pdf`, then `echo ${var%.pdf}.txt` prints `foo.txt`.
|
||||
|
||||
- Brace expansion using `{`...`}` can reduce having to re-type similar text and automate combinations of items. This is helpful in examples like `mv foo.{txt,pdf} some-dir` (which moves both files), `cp somefile{,.bak}` (which expands to `cp somefile somefile.bak`) or `mkdir -p test-{a,b,c}/subtest-{1,2,3}` (which expands all possible combinations and creates a directory tree).
|
||||
- Brace expansion using `{`...`}` can reduce having to re-type similar text and automate combinations of items. This is helpful in examples like `mv foo.{txt,pdf} some-dir` (which moves both files), `cp somefile{,.bak}` (which expands to `cp somefile somefile.bak`) or `mkdir -p test-{a,b,c}/subtest-{1,2,3}` (which expands all possible combinations and creates a directory tree). Brace expansion is performed before any other expansion.
|
||||
|
||||
- The output of a command can be treated like a file via `<(some command)`. For example, compare local `/etc/hosts` with a remote one:
|
||||
- The output of a command can be treated like a file via `<(some command)` (known as Process Substitution). For example, compare local `/etc/hosts` with a remote one:
|
||||
```sh
|
||||
diff /etc/hosts <(ssh somehost cat /etc/hosts)
|
||||
```
|
||||
@ -348,7 +348,7 @@ A few examples of piecing together commands:
|
||||
cat a b b | sort | uniq -u > c # c is set difference a - b
|
||||
```
|
||||
|
||||
- Use `grep . *` to quickly examine the contents of all files in a directory (so each line is paired with the filename), or `head -100 *` (so each file has a heading). This can be useful for directories filled with config settings like those in `/sys`, `/proc`, `/etc`.
|
||||
- Use `grep . *` to quickly examine the contents of all files in a directory (so each line is paired with the filename), or `more *` (so each file has a heading). This can be useful for directories filled with config settings like those in `/sys`, `/proc`, `/etc`.
|
||||
|
||||
|
||||
- Summing all numbers in the third column of a text file (this is probably 3X faster and 3X less code than equivalent Python):
|
||||
|
Loading…
Reference in New Issue
Block a user