mirror of
https://github.com/xmengnet/the-art-of-command-line.git
synced 2024-12-25 01:36:29 +08:00
brace expansion example.
This commit is contained in:
parent
74943cbd3b
commit
b5cdd0f700
1 changed files with 4 additions and 0 deletions
|
@ -107,6 +107,10 @@ 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}`. 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`.
|
- 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}`. 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`.
|
||||||
|
|
||||||
|
- Shell brace expansion can help reduce having to re-type similar text. The command `echo foo{,bar,baz}` will expand to
|
||||||
|
`echo foo foobar foobaz`. This is helpful when copying/renaming files such as `cp somefile{,.bak}` which expands to
|
||||||
|
`cp somefile somefile.bak` or `mv some_{,absurdly_long_}filename` which expands to `mv some_filename some_absurdly_long_filename`
|
||||||
|
|
||||||
- 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)`. For example, compare local `/etc/hosts` with a remote one:
|
||||||
```sh
|
```sh
|
||||||
diff /etc/hosts <(ssh somehost cat /etc/hosts)
|
diff /etc/hosts <(ssh somehost cat /etc/hosts)
|
||||||
|
|
Loading…
Reference in a new issue