From 49918189ad7daceda878fd81a62d3c9a8c387c5e Mon Sep 17 00:00:00 2001 From: Andreas Hofmann Date: Sat, 9 Jul 2016 10:37:20 +0200 Subject: [PATCH 1/2] README.md: Added default value variable expansion example Signed-off-by: Andreas Hofmann --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index ff8d805..8e93a9a 100644 --- a/README.md +++ b/README.md @@ -136,7 +136,7 @@ Notes: # continue in original dir ``` -- 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}`. 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). From a2d687b2fceef584872c8b9899c1da7d1f87dccf Mon Sep 17 00:00:00 2001 From: Andreas Hofmann Date: Sat, 9 Jul 2016 13:22:20 +0200 Subject: [PATCH 2/2] Added curly-braces-in-script hint Signed-off-by: Andreas Hofmann --- README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/README.md b/README.md index 8e93a9a..85ad1bc 100644 --- a/README.md +++ b/README.md @@ -145,6 +145,14 @@ Notes: diff /etc/hosts <(ssh somehost cat /etc/hosts) ``` +- When writing scripts you may want to put all of your code in curly braces: +```sh +{ + # Your code here +} +``` +If the closing brace is missing, your script will be prevented from executing due to a syntax error. This makes sense when your script is going to be downloaded from the web, since it prevents partially downloaded scripts from executing. + - Know about "here documents" in Bash, as in `cat <logfile 2>&1` or `some-command &>logfile`. Often, to ensure a command does not leave an open file handle to standard input, tying it to the terminal you are in, it is also good practice to add `