The Bourne Identity - shell explained again

Krzysztof Kwieciński
3 min readOct 21, 2018

--

bash = Bourne Again shell (image: source)

Bourne-again shell, or simply bash, is a Unix shell and command language. Since 1989 it has been widely distributed as the default shell for most Linux and macOS distributions. Recently, it has been available also for Windows.

Effectively using shell enormously impacts our productivity. The short commands are easy to learn and have a huge potential because we can pipe them freely. Let’s dive into the command line!

Set up bash profile

It is important that our console has a style that we want. The .bashrccan be created and edited in plain text, however, there exists a great website that allows us to create our favorite prompt with drag&drop. Such a generated file can be downloaded and then further adjusted.

Examples

In this section some examples are presented. Various commands (grep, find, git, sed, cat, wc, zip) are pipelined in order to achieve more complicated behavior.

My examples

  • Count classes (or enums/interfaces, if we change the pattern) in a project directory (not only separate .java files, but also inner classes):
    $ grep -roh --include=*.java 'class ' . | wc -w
  • Find lines containing a <ns1:Foo> tag in input.xml and save only the values of the elements to the output.txt:
    $ grep 'ns1:Foo' file.xml | sed -e 's/<ns1:Foo>\(.*\)<\/ns1:Foo>/\1/’ | cat > output.txt
  • Find commits containing [JIRA] tag and display one line before the tag and two lines after it:
    $ git log | grep -B1 -A2 '[JIRA]'
  • Find all commits in which Controller files were modified and display theirs authors, without inserting any group separators:
    $ git log --all -- '*Controller*' | grep 'Author' --group-separator=''

The Pragmatic Programmer’s examples

  • Find all .kt files that were modified later than build.gradle:
    find . -name '*.kt' -newer build.gradle -print
  • Create an archive (tar or zip) with the Java and Kotlin source code:
    zip sources.zip *.java *.kt
    tar cvf sources.tar *.java *.kt
  • Show Java files that have been changed during the last week:
    find . -name '*.java' -mtime +7 -print
  • Show Java files which have been changed during the last week, and use RxJava:
    find . -name '*.java' -mtime +7 -print | xargs grep 'import io.reactivex'
FAQ (image: source)

Difference between semicolon and double ampersand (; vs. &&)

“;” just separates two commands, “&&” means logical AND (so, like in most programming languages, there is also OR “||”). Consider the following example:

$ false ; echo "OK"
OK
$ true ; echo "OK"
OK
$ false && echo "OK"
$ true && echo "OK"
OK
$ false || echo "OK"
OK
$ true || echo "OK"
$

echo "OK" is always run for “:”. When it comes to logical operators, the second part is executed only when it makes sense to evaluate the right operand.

Difference between single and double quotes (‘…‘ vs. “…”’)

Double quotes interpolate strings between them, single quotes do not (Bash Reference Manual). The following example illustrates that perfectly:

$ echo "$(echo "qoute")"
qoute
$ echo '$(echo "qoute")'
$(echo "qoute")

Shell workshop

If you want to do an online course, I recommend a short introduction from Udacity. A couple of basic commands are described (ls, mv etc.) as well as some more advanced, like grep, curl or less.

--

--

Krzysztof Kwieciński
Krzysztof Kwieciński

Written by Krzysztof Kwieciński

Software Craftsman who loves learning new things and is always eager to share his knowledge with others

No responses yet