emacs/var/elfeed/db/data/57/578d18ea93f6a3de7bc020b101ce12b70a8a3abd
2022-01-03 12:49:32 -06:00

85 lines
1.9 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<p>Raw link: <a href="https://www.youtube.com/watch?v=S4D9KaW3ERw">https://www.youtube.com/watch?v=S4D9KaW3ERw</a></p>
<p>In this screen cast I showcase some of the features of BASH for
manipulating variables, aka “parameters”. Parameter expansion is a very
powerful feature. Mastering it should greatly improve your shell
scripting skills (see the <a href="https://www.gnu.org/software/bash/manual/">GNU Bash
manual</a>).</p>
<p>Started applying this knowledge to the scripts that are distributed with
<a href="https://gitlab.com/protesilaos.com/dotfiles">my dotfiles</a>, in
preparation of the next beta version of “Prots Dots For Debian” (which
I introduced in <a href="https://protesilaos.com/codelog/2019-04-28-beta-bspwm-pdfd/">the previous
entry</a>).</p>
<p>Below I include the code samples shown in this screen cast.</p>
<hr />
<p>First demo script:</p>
<pre><code>#!/bin/bash
name=protesilaos
# Replace parameter with its value
echo "$name"
# Get parameter length
echo "${#name}"
# Extract substring ${parameter:offset:length}
echo "${name:0:4}"
# Capitalise first character
echo "${name^}"
# All majuscules
echo "${name^^}"
</code></pre>
<p>Second example:</p>
<pre><code>#!/bin/bash
name=PROTESILAOS
# First character is lower case
echo "${name,}"
# All miniscules
echo "${name,,}"
# Use default value if parameter is unset/null
unset name
echo "$name"
echo "${name:-Protesilaos}"
echo "${name:-$USER}"
</code></pre>
<p>Third sample:</p>
<pre><code>#!/bin/bash
freedom='GNU Not UNIX ; GNU plus Linux'
# Replace first occurence of pattern with string
echo "${freedom/GNU/Gahnoo}"
# Replace all matches of pattern with string
echo "${freedom//GNU/Gahnoo}"
# Remove first match of pattern
echo "${freedom/* ; /}"
freedom='GNU Not UNIX ; GNU is Not Unix ; Gahnoo slash Linaks ; GNU/Linux'
echo "$freedom"
# Remove all occurences of pattern
echo "${freedom//* ; /}"
</code></pre>