Skip to content

Instantly share code, notes, and snippets.

@thealmightygrant
Last active March 27, 2018 20:24
Show Gist options
  • Save thealmightygrant/404fd4bfe4084fe657a4e628d6c60ac7 to your computer and use it in GitHub Desktop.
Save thealmightygrant/404fd4bfe4084fe657a4e628d6c60ac7 to your computer and use it in GitHub Desktop.
Bash to the Future
<section>
<h1>Bash to the Future</h1>
<h2>Grant Sherrick</h2>
</section>
<section id="some-bash-history-pt1">
<p id="slide-category" class="callouts">Bash History</p>
<h2>Bash is a product of the GNU project.</h2>
<br>
<ul>
<li class="fragment">Bash is covered under a GPL license.</li>
<li class="fragment">Bash is inherently open source.</li>
<li class="fragment">Any software that is sold for profit should not be bundled with a bash script.</li>
<li class="fragment">See <a href="https://www.gnu.org/philosophy/pragmatic.en.html">this post</a> or <a href="https://www.gnu.org/licenses/gpl-faq.html#GPLInProprietarySystem">this post</a> for more info.</li>
</ul>
</section>
<section id="some-bash-history-pt2">
<p id="slide-category" class="callouts">Bash History</p>
<h2>Unics makes an appearance.</h2>
<br>
<ul>
<li class="fragment">Based on the Multics project, the name <a href="http://www.linuxjournal.com/article/7035">Unics was a pun</a>.</li>
<li class="fragment">Unix was distributed as source code for research purposes.</li>
<li class="fragment">C was developed for Unix and Unix was rewritten using it in 1973.</li>
<a href="https://www.bell-labs.com/usr/dmr/www/hist.html"><pre class="fragment"><code>What we wanted to preserve was not just a good environment in which to do
programming, but a system around which a fellowship could form. We knew
from experience that the essence of communal computing ... is not just
to type programs into a terminal instead of a keypunch, but to encourage
close communication. – Dennis Ritchie</code></pre></a>
</ul>
</section>
<section id="some-bash-history-pt3">
<p id="slide-category" class="callouts">Bash History</p>
<h2>GNU = GNU is not Unix.</h2>
<br>
<ul>
<li class="fragment">Unix source code was sold via licenses starting in 1975</li>
<li class="fragment">GNU software is Unix-like in philosophy but 100% free.</li>
<li class="fragment">GNU software + Linux kernel = Linux OS.</li>
<li class="fragment">Linux follows many of the POSIX standards (one of these is <a href="http://pubs.opengroup.org/onlinepubs/9699919799/"><code>sh</code></a>)</li>
</ul>
</section>
<section id="some-bash-history-pt4">
<p id="slide-category" class="callouts">Bash History</p>
<h2>There's a wide variety of shells.</h2>
<br>
<ul>
<li class="fragment">"The shell is a command language interpreter".</li>
<li class="fragment"><code>sh</code> is a utility that implements the command language described by the POSIX standard.</li>
<li class="fragment"><code>bash</code> predates the <a href="https://unix.stackexchange.com/questions/11983/what-exactly-is-posix">POSIX</a> standard.</li>
<li class="fragment"><code>bash</code> has many extensions that do not conform to this standard.
<ul>
<li>function, arrays, ==, [[]], <a href="https://mywiki.wooledge.org/Bashism">and many more</a>.</li>
</ul>
</li>
<li class="fragment">Debian Linux now uses <a href="https://en.wikipedia.org/wiki/Almquist_shell">dash</a>, a posix compliant shell, as the default shell.</li>
</ul>
</section>
<section id="start-with-a-she-bang-pt1">
<p id="slide-category" class="callouts">Start with a She-Bang!</p>
<h2>She-bangs are magic.</h2>
<br>
<ul>
<li class="fragment">Begin a line with #!, the rest is assumed to be an "interpreter".</li>
<li class="fragment">Originally, the shell executed itself on a text file, recursively.</li>
<li class="fragment">She-bangs allow for:</li>
<ol>
<li class="fragment">ps to show the script name with arguments instead of sh.</li>
<li class="fragment">the use of non-default shells.</li>
<li class="fragment">enforcing the use of a particular shell.</li>
<li class="fragment">and <a href="https://www.in-ulm.de/~mascheck/various/shebang/4.0BSD_newsys_sys1.c.html">several other</a> more obscure reasons</li>
</ol>
</ul>
</section>
<section id="start-with-a-she-bang-pt2">
<p id="slide-category" class="callouts">Start with a She-Bang!</p>
<h2><a href="https://www.in-ulm.de/~mascheck/various/shebang/">She-bangs</a> are magic.</h2>
<br>
<ul>
<li class="fragment">The kernel peeks at the first 16 bits of a file before execution.</li>
<li class="fragment">Those 16 bits are called a magic number.</li>
<li class="fragment">If the magic number is not recognized, it is not executed.</li>
<li class="fragment">This was extended so that the kernel knew not only if a file could be executed but also how to execute the file.</li>
</ul>
</section>
<section id="start-with-a-she-bang-pt3">
<p id="slide-category" class="callouts">Start with a She-Bang!</p>
<h2><a href="https://www.in-ulm.de/~mascheck/various/shebang/">She-bangs</a> are executed, not run in the shell.</h2>
<br>
<ul>
<li class="fragment">$PATH is not searched for the interpreter.</li>
<li class="fragment">However, relative paths are okay.</li>
<li class="fragment">The handling of arguments in the #! line itself is varying.</li>
<li class="fragment">She-Bang is thought to originate from the musical "sharp" + bang</li>
</ul>
</section>
<section id="word-splitting-pt1">
<p id="slide-category" class="callouts"></p>
<h2>How is a function executed?</h2>
<br>
<ul>
<li><a href="https://www.gnu.org/software/bash/manual/html_node/Brace-Expansion.html">brace expansion</a></li>
<li>tilde expansion, "~/apps/gainsight-poster"</li>
<li>parameter, "$some_var", "${some_var:-"non-null-default"}"</li>
<li>arithmetic expansion (not covered here)</li>
<li>command substitution, "$()".</li>
<li>word splitting and pathname expansion.</li>
</ul>
</section>
<section id="bash-tips-pt1">
<p id="slide-category" class="callouts">Bash Best Practices</p>
<h2>Things that always apply:</h2>
<br>
<pre><code>a_variable="some value"
### Quote everything, no naked $ signs. ###
echo $a_variable #bad
echo "$a_variable" #good
$(echo $a_variable) #bad
$(echo "$a_variable") #better
"$(echo "$a_variable")" #good
echo $? #bad
echo "$?" #good
echo "$a_variable_plus_some_data #bad
echo "${a_variable}_plus_some_data #good
### All variables and function invocations go in a function. ###
### Use one "main" function and one call to main "$@". ###
some_func() {
echo "$1"
}
main() {
some_func "lalalala"
}
main "$@"
### Use local when setting variables ###
### NOTE: local only works within functions. ###
main() {
local some_variable="lalalala"
some_func "$some_variable"
}
### Things that aren't exported should be lowercased. ###
export SOME_GLOBAL_VARIABLE="im so global"
main() {
local some_local_variable="im so local"
echo "$some_local_variable"
}</pre></code>
</section>
<section id="bash-tips-pt2">
<p id="slide-category" class="callouts">Bash Best Practices</p>
<h2>Some good ideas:</h2>
<br>
<ul>
<li class="fragment">Never use <a href="http://wiki.bash-hackers.org/scripting/obsolete">deprecated style</a>.
<pre><code style="max-height: 120px;">function some_func() { } #bad
some_func() { } #good
if [ "a" = "$some_var" ]; then #bad
echo "so awesome"
fi
if [[ "a" = "$some_var" ]]; then #good
echo "so awesome"
fi
`echo "this is in a subprocess"` #bad
"$(echo "this is in a subprocess")" #good</pre></code>
</li>
<li class="fragment">Prefer absolute paths, use <code>$PWD</code> to get your current directory.</li>
<li class="fragment">Prefer the long forms of options <code>logger --priority vs logger -p</code>.</li>
<li class="fragment">Always cleanup with <code>trap</code>.
<pre><code style="max-height: 150px;">exit_script() {
exit_code="$?"
clean_up_everything
if [[ "$exit_code" = "0" ]]; then
echo "!!!!!!! SCRIPT PASSED !!!!!!!"
else
echo "!!!!!!! SCRIPT FAILED !!!!!!!"
fi
exit "$exit_code" #NOTE: exit code is independent of cleanup action
}
main () {
trap exit_script SIGHUP SIGINT SIGKILL SIGSTOP SIGTERM EXIT
}</pre></code></li>
</ul>
</section>
<section id="conclusion">
<h2>Thanks!</h2>
</section>
<section id="useful-links">
<h2>Some Useful Links:</h2>
<ul>
<li><a href="http://www.faqs.org/faqs/unix-faq/faq/part3/section-16.html">All about she-bangs</a></li>
<li><a href="https://google.github.io/styleguide/shell.xml">The Google Shell Style Guide</a></li>
<li><a href="https://github.com/progrium/bashstyle">A nice bash style guide</a></li>
<li><a href="https://www.tldp.org/LDP/abs/html/abs-guide.html">Advanced Bash Scripting Guide</a></li>
<li><a href="https://sap1ens.com/blog/2017/07/01/bash-scripting-best-practices/">Bash best practices</a></li>
<li><a href="https://github.com/awesome-lists/awesome-bash">Awesome Bash (a curated list)</a></li>
</ul>
</section>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment