aboutsummaryrefslogtreecommitdiffhomepage
path: root/tools/bash.html
diff options
context:
space:
mode:
Diffstat (limited to 'tools/bash.html')
-rw-r--r--tools/bash.html47
1 files changed, 30 insertions, 17 deletions
diff --git a/tools/bash.html b/tools/bash.html
index 580e941..91d44cb 100644
--- a/tools/bash.html
+++ b/tools/bash.html
@@ -192,7 +192,7 @@
bar=${foo:-some_val} # if $foo set, then bar=$foo else bar=some_val
# alternate value
-bar=${foo:+bla $foo} # if $foo set, then bar="bla $foo" else bar=""
+bar=${foo:+bla $foo} # if $foo set, then bar="bla $foo" else bar=""
# check param set
bar=${foo:?msg} # if $foo set, then bar=$foo else exit and print msg
@@ -274,6 +274,19 @@ contains some nice visualization to explain bash redirections.</p>
once.</p>
<pre><code class="language-bash">vim -d &lt;(grep foo bar) &lt;(grep foo moose)
</code></pre>
+<h2 id="command-grouping"><a class="header" href="#command-grouping">Command grouping</a></h2>
+<p>Execute commands in a group with or without subshell. Can be used to easily
+redirect stdout/stderr of all commands in the group into one file.</p>
+<pre><code class="language-bash"># Group commands without subshell.
+v=abc ; { v=foo; echo $v; } ; echo $v
+# foo
+# foo
+
+# Group commands with subshell.
+v=abc ; ( v=foo; echo $v; ) ; echo $v
+# foo
+# abc
+</code></pre>
<h2 id="argument-parsing-with-getopts"><a class="header" href="#argument-parsing-with-getopts">Argument parsing with <code>getopts</code></a></h2>
<p>The <code>getopts</code> builtin uses following global variables:</p>
<ul>
@@ -296,11 +309,11 @@ once.</p>
<h3 id="example"><a class="header" href="#example">Example</a></h3>
<pre><code class="language-bash">#!/bin/bash
function parse_args() {
- while getopts &quot;f:c&quot; PARAM; do
+ while getopts "f:c" PARAM; do
case $PARAM in
- f) echo &quot;GOT -f $OPTARG&quot;;;
- c) echo &quot;GOT -c&quot;;;
- *) echo &quot;ERR: print usage&quot;; exit 1;;
+ f) echo "GOT -f $OPTARG";;
+ c) echo "GOT -c";;
+ *) echo "ERR: print usage"; exit 1;;
esac
done
# users responsibility to reset OPTIND
@@ -320,14 +333,14 @@ The match results can be accessed via the <code>$BASH_REMATCH</code> variable:</
<pre><code class="language-bash">INPUT='title foo : 1234'
REGEX='^title (.+) : ([0-9]+)$'
if [[ $INPUT =~ $REGEX ]]; then
- echo &quot;${BASH_REMATCH[0]}&quot; # title foo : 1234
- echo &quot;${BASH_REMATCH[1]}&quot; # foo
- echo &quot;${BASH_REMATCH[2]}&quot; # 1234
+ echo "${BASH_REMATCH[0]}" # title foo : 1234
+ echo "${BASH_REMATCH[1]}" # foo
+ echo "${BASH_REMATCH[2]}" # 1234
fi
</code></pre>
<blockquote>
<p><strong>Caution</strong>: When specifying a <code>regex</code> in the <code>[[ ]]</code> block directly, quotes will be treated as part of the pattern.
-<code>[[ $INPUT =~ &quot;foo&quot; ]]</code> will match against <code>&quot;foo&quot;</code> not <code>foo</code>!</p>
+<code>[[ $INPUT =~ "foo" ]]</code> will match against <code>"foo"</code> not <code>foo</code>!</p>
</blockquote>
<h2 id="completion"><a class="header" href="#completion">Completion</a></h2>
<p>The <code>complete</code> builtin is used to interact with the completion system.</p>
@@ -358,11 +371,11 @@ against words generated by <code>option</code>.</p>
# -u generate list with users
# -e generate list with exported variables
-# compare &quot;f&quot; against words &quot;foo&quot; &quot;foobar&quot; &quot;bar&quot; and generate matches
-compgen -W &quot;foo foobar bar&quot; &quot;f&quot;
+# compare "f" against words "foo" "foobar" "bar" and generate matches
+compgen -W "foo foobar bar" "f"
-# compare &quot;hom&quot; against file/dir names and generate matches
-compgen -d -f &quot;hom&quot;
+# compare "hom" against file/dir names and generate matches
+compgen -d -f "hom"
</code></pre>
<h3 id="example-1"><a class="header" href="#example-1">Example</a></h3>
<p>Skeleton to copy/paste for writing simple completions.</p>
@@ -374,12 +387,12 @@ compgen -d -f &quot;hom&quot;
local curr=$2
local prev=$3
- local opts=&quot;-c -s -f -h&quot;
+ local opts="-c -s -f -h"
case $prev in
- -c) COMPREPLY=( $(compgen -W &quot;green red blue&quot; -- $curr) );;
- -s) COMPREPLY=( $(compgen -W &quot;low high&quot; -- $curr) );;
+ -c) COMPREPLY=( $(compgen -W "green red blue" -- $curr) );;
+ -s) COMPREPLY=( $(compgen -W "low high" -- $curr) );;
-f) COMPREPLY=( $(compgen -f -- $curr) );;
- *) COMPREPLY=( $(compgen -W &quot;$opts&quot; -- $curr) );;
+ *) COMPREPLY=( $(compgen -W "$opts" -- $curr) );;
esac
}