From 3d68a6159202bffcff356a1c40700a2faa44c6db Mon Sep 17 00:00:00 2001 From: Mohit Agarwal Date: Wed, 12 Oct 2016 19:20:42 +0530 Subject: [PATCH] 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. --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 42f45d9..9629b70 100644 --- a/README.md +++ b/README.md @@ -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):