See bash-examples.sh.
In examples of shell command usage, there tend to be two different styles: the commandline typescript style [1] and the inline output style. [2]
The typescript style looks like this:
$ echo {01..09}
01 02 03 04 05 06 07 08 09
It is intended to resemble the actual text in a terminal.
The inline output style looks like this:
echo {01..09} # 01 02 03 04 05 06 07 08 09
It is intended to resemble comments in a shell script.
They both have advantages and disadvantages; the typescript style is usually used for one-liners, the inline tends to be used for longer scripts.
However, for saving examples to work from later, neither of these options are ideal. The typescript style is verbose and varies with the environment, and the inline has to be done by hand.
To get around this,
I wrote a bunch of bash examples as functions,
showing the code via declare -f
run it with another function and "$@",
and finally display the output as commented text.
This method has the undesirable side-effect
of stripping comments from the function,
but comments before and after the example
are passed through
and differentiated from the output
by indenting with a tab character (\t).
See examples-generator.sh for how this is accomplished.
| Q: | Wouldn't the IPython bash kernel accomplish roughly the same thing? |
|---|---|
| A: | Probably, but it would also store everything as JSON, which requires escaping double quotes and newlines. I wanted to keep this a simple text file that could be used as raw material in larger bash scripts. |
| Q: | Wouldn't xiki do a better job of this? |
| A: | Possibly, but this doesn't require specialized software, just bash. |
| [1] |
Examples of the typescript style: Official Bash documentation: bash$ echo a{d,c,b}e
ade ace abe
https://www.gnu.org/software/bash/manual/html_node/Brace-Expansion.html The Linux Documentation Project: franky ~> echo sp{el,il,al}l
spell spill spall
http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_03_04.html nixCraft: To view $RANDOM, enter::
$ echo $RANDOM
Sample outputs::
11799
CSpace::
$ cat repro
#!/bin/bash
function cleanup() {
r1=$(/bin/echo one)
r2=$(echo two)
#echo $r1 '!' $r2 >>/tmp/logfile
echo $r1 '!' $r2 1>&2
}
trap cleanup EXIT
sleep 1
echo final
$ ./repro | false
final
one final ! final two
$
https://utcc.utoronto.ca/~cks/space/blog/unix/BashBufferingForkBug
|
| [2] | Examples of the inline style: Wikipedia Bash page: echo {1..10} # expands to 1 2 3 4 5 6 7 8 9 10
Linux Journal: {aa,bb,cc,dd} => aa bb cc dd
{0..12} => 0 1 2 3 4 5 6 7 8 9 10 11 12
{3..-2} => 3 2 1 0 -1 -2
{a..g} => a b c d e f g
{g..a} => g f e d c b a
|
This project is licensed under the terms of the MIT license.