<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Linux on dwmkerr.com</title><link>https://dwmkerr.com/categories/linux/</link><description>Recent content in Linux on dwmkerr.com</description><generator>Hugo -- gohugo.io</generator><language>en-uk</language><managingEditor>Dave Kerr</managingEditor><copyright>Copright &amp;copy; Dave Kerr</copyright><lastBuildDate>Tue, 25 Jun 2019 07:25:23 +0000</lastBuildDate><atom:link href="https://dwmkerr.com/categories/linux/index.xml" rel="self" type="application/rss+xml"/><item><title>Effective Shell Part 7: The Subtleties of Shell Commands</title><link>https://dwmkerr.com/effective-shell-7-shell-commands/</link><pubDate>Tue, 25 Jun 2019 07:25:23 +0000</pubDate><guid>https://dwmkerr.com/effective-shell-7-shell-commands/</guid><description>&lt;p&gt;In this chapter, we&amp;rsquo;ll take a look at the various different types of shell commands that exist and how this can affect your work.&lt;/p&gt;
&lt;p&gt;By the end of this chapter, you might even be able to make sense of the horrifying and perfectly syntactically valid code below:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-sh" data-lang="sh"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;which &lt;span style="color:#66d9ef"&gt;$(&lt;/span&gt;where &lt;span style="color:#66d9ef"&gt;$(&lt;/span&gt;what &lt;span style="color:#66d9ef"&gt;$(&lt;/span&gt;whence &lt;span style="color:#66d9ef"&gt;$(&lt;/span&gt;whereis who&lt;span style="color:#66d9ef"&gt;))))&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.dwmkerr.com/effective-shell-part-1-navigating-the-command-line/"&gt;Part 1: Navigating the Command Line&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.dwmkerr.com/effective-shell-part-2-become-a-clipboard-gymnast/"&gt;Part 2: Become a Clipboard Gymnast&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.dwmkerr.com/effective-shell-part-3-getting-hepl/"&gt;Part 3: Getting Help&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dwmkerr.com/effective-shell-4-moving-around/"&gt;Part 4: Moving Around&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dwmkerr.com/effective-shell-part-5-understanding-the-shell/"&gt;Part 5: Interlude - Understanding the Shell&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dwmkerr.com/effective-shell-6-job-control/"&gt;Part 6: Everything You Don&amp;rsquo;t Need to Know About Job Control&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;a href="https://dwmkerr.com/effective-shell-7-shell-commands/"&gt;Part 7: The Subtleties of Shell Commands&lt;/a&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="what-are-commands"&gt;What Are Commands?&lt;/h2&gt;
&lt;p&gt;This is &lt;em&gt;really&lt;/em&gt; important to understand! A &lt;em&gt;command&lt;/em&gt; in a shell is something you execute. It might take parameters. Generally it&amp;rsquo;ll have a form like this:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-sh" data-lang="sh"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;command param1 param2
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;We&amp;rsquo;ve already seen many commands during this series:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-sh" data-lang="sh"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;ls &lt;span style="color:#75715e"&gt;# Show the contents of the current directory&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;cd ~ &lt;span style="color:#75715e"&gt;# Move to the user&amp;#39;s home&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;cat file.txt &lt;span style="color:#75715e"&gt;# Output the contents of &amp;#39;file.txt&amp;#39; to stdout&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;But to be an effective shell user, you must understand that not all commands are created equal. The differences between the types of commands will affect how you use them.&lt;/p&gt;
&lt;p&gt;There are four types of commands in most shells:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Executables&lt;/li&gt;
&lt;li&gt;&amp;ldquo;Built-Ins&amp;rdquo; (which we&amp;rsquo;ll just call &lt;em&gt;builtins&lt;/em&gt; from now on)&lt;/li&gt;
&lt;li&gt;Functions&lt;/li&gt;
&lt;li&gt;Aliases&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Let&amp;rsquo;s quickly dig in and see a bit more.&lt;/p&gt;
&lt;h2 id="executables---programs"&gt;Executables - Programs&lt;/h2&gt;
&lt;p&gt;Executables are just files with the &amp;rsquo;executable&amp;rsquo; bit set&lt;sup id="fnref:1"&gt;&lt;a href="#fn:1" class="footnote-ref" role="doc-noteref"&gt;1&lt;/a&gt;&lt;/sup&gt;. If I execute the &lt;code&gt;cat&lt;/code&gt; command, the shell will search for an executable named &lt;code&gt;cat&lt;/code&gt; in my &lt;code&gt;$PATH&lt;/code&gt;. If it finds it, it will run the program.&lt;/p&gt;
&lt;pre tabindex="0"&gt;&lt;code&gt;$ cat file.txt
This is a simple text file
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;What is &lt;code&gt;$PATH&lt;/code&gt;? &lt;code&gt;$PATH&lt;/code&gt; is the standard environment variable used to define &lt;em&gt;where&lt;/em&gt; the shell should search for programs. If we temporarily &lt;em&gt;empty&lt;/em&gt; this variable, the shell won&amp;rsquo;t find the command:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-sh" data-lang="sh"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;$ PATH&lt;span style="color:#f92672"&gt;=&lt;/span&gt;&lt;span style="color:#e6db74"&gt;&amp;#34;&amp;#34;&lt;/span&gt; cat file.txt
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;bash: cat: No such file or directory
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Normally your &lt;code&gt;$PATH&lt;/code&gt; variable will include the standard locations for Linux programs - folders such as &lt;code&gt;/bin&lt;/code&gt;, &lt;code&gt;/sbin&lt;/code&gt;, &lt;code&gt;/usr/bin&lt;/code&gt; and so on&lt;sup id="fnref:2"&gt;&lt;a href="#fn:2" class="footnote-ref" role="doc-noteref"&gt;2&lt;/a&gt;&lt;/sup&gt;.&lt;/p&gt;
&lt;p&gt;If you were to print the variable, you&amp;rsquo;d see a bunch of paths (they are separated by colons; I&amp;rsquo;ve put them on separate lines for readability):&lt;/p&gt;
&lt;pre tabindex="0"&gt;&lt;code&gt;/usr/local/bin
/usr/bin
/bin
/usr/sbin
/sbin
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The shell will start with the &lt;em&gt;earlier&lt;/em&gt; locations and move to the later ones. This allows &lt;em&gt;local&lt;/em&gt; flavours of tools to be installed for users, which will take precedence over &lt;em&gt;general&lt;/em&gt; versions of tools.&lt;/p&gt;
&lt;p&gt;There will likely be other locations too - you might see Java folders, package manager folders and so on.&lt;/p&gt;
&lt;h2 id="executables---scripts"&gt;Executables - Scripts&lt;/h2&gt;
&lt;p&gt;Imagine we create a text file called &lt;code&gt;dog&lt;/code&gt; in the local folder:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-sh" data-lang="sh"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;#!/bin/sh
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;echo &lt;span style="color:#e6db74"&gt;&amp;#34;🐶 woof 🐶&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;If we make the file &lt;em&gt;executable&lt;/em&gt;, by running &lt;code&gt;chmod +x dog&lt;/code&gt;&lt;sup id="fnref:3"&gt;&lt;a href="#fn:3" class="footnote-ref" role="doc-noteref"&gt;3&lt;/a&gt;&lt;/sup&gt;, then we can run this just like any other program - as long as we tell the shell to look for programs in the current directory:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-sh" data-lang="sh"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;$ PATH&lt;span style="color:#f92672"&gt;=&lt;/span&gt;&lt;span style="color:#e6db74"&gt;&amp;#34;.&amp;#34;&lt;/span&gt; dog
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;🐶 woof 🐶
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;More common would be to run the program by giving a path:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-sh" data-lang="sh"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;$ ./dog
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;🐶 woof 🐶
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Or just move it to a standard location that the shell already checks for programs:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-sh" data-lang="sh"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;$ mv dog /usr/local/bin
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;$ dog
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;🐶 woof 🐶
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The point is that executables don&amp;rsquo;t &lt;em&gt;have&lt;/em&gt; to be compiled program code. If a file starts with &lt;code&gt;#!&lt;/code&gt; (the &amp;lsquo;shebang&amp;rsquo;), then the system will try to run the contents of the file with the program specified in the shebang.&lt;/p&gt;
&lt;p&gt;We will look at shebangs in greater detail in a later chapter.&lt;/p&gt;
&lt;h2 id="builtins"&gt;Builtins&lt;/h2&gt;
&lt;p&gt;OK, so we&amp;rsquo;ve seen executables. What about a command like this?&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-sh" data-lang="sh"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;local V&lt;span style="color:#f92672"&gt;=&lt;/span&gt;&lt;span style="color:#e6db74"&gt;&amp;#34;hello&amp;#34;&lt;/span&gt; echo $V
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;You will not find the &lt;code&gt;local&lt;/code&gt; executable anywhere on your system. It is a &lt;em&gt;builtin&lt;/em&gt; - a special command built directly into the shell program.&lt;/p&gt;
&lt;p&gt;Builtins are often highly specific to your shell. They might be used for programming (&lt;code&gt;local&lt;/code&gt; for example is used to declare a locally scoped variable), or they might be for very shell-specific features.&lt;/p&gt;
&lt;p&gt;This is where we need to take note. As soon as you are running a builtin, you are potentially using a feature that is specific to &lt;em&gt;your&lt;/em&gt; shell, rather than a program that is shared across the system and can be run by &lt;em&gt;any&lt;/em&gt; shell.&lt;/p&gt;
&lt;p&gt;Trying to programmatically execute &lt;code&gt;local&lt;/code&gt; as a process will fail - there is no executable with that name; it is purely a shell construct.&lt;/p&gt;
&lt;p&gt;So how do we know if a command is a builtin? The preferred method is to use the &lt;code&gt;type&lt;/code&gt; command:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-sh" data-lang="sh"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;$ type local
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;local is a shell builtin
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The &lt;code&gt;type&lt;/code&gt; command (which is &lt;em&gt;itself&lt;/em&gt; a builtin!) can tell you the exact type of shell command.&lt;/p&gt;
&lt;p&gt;Interestingly, you might be using more builtins than you think. &lt;code&gt;echo&lt;/code&gt; is a program, but most of the time you are not executing it when you are in a shell:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-sh" data-lang="sh"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;$ type -a echo
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;echo is a shell builtin
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;echo is /bin/echo
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;By using the &lt;code&gt;-a&lt;/code&gt; flag on &lt;code&gt;type&lt;/code&gt; to show &lt;em&gt;all&lt;/em&gt; commands that match the name, we see that &lt;code&gt;echo&lt;/code&gt; is actually both a builtin &lt;em&gt;and&lt;/em&gt; a program.&lt;/p&gt;
&lt;p&gt;Many simple programs have builtin versions. The shell can execute them much faster.&lt;/p&gt;
&lt;p&gt;Some commands are a builtin so that they can function in a sensible manner. The &lt;code&gt;cd&lt;/code&gt; command changes the current directory - if we executed it as a process, it would change only the directory for the &lt;code&gt;cd&lt;/code&gt; process itself, not the shell, making it much less useful.&lt;/p&gt;
&lt;p&gt;Builtins will vary from shell to shell, but many shells are &amp;lsquo;Bash-like&amp;rsquo; - meaning they will have a set very similar to the Bash builtins, which you can see here:&lt;/p&gt;
&lt;p&gt;&lt;a href="https://www.gnu.org/software/bash/manual/html_node/Bash-Builtins.html"&gt;https://www.gnu.org/software/bash/manual/html_node/Bash-Builtins.html&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;As should be familiar from &lt;a href="https://www.dwmkerr.com/effective-shell-part-3-getting-hepl/"&gt;Part 3: Getting Help&lt;/a&gt;, you can get help for builtins:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-sh" data-lang="sh"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;$ man source &lt;span style="color:#75715e"&gt;# source is a builtin&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;BUILTIN&lt;span style="color:#f92672"&gt;(&lt;/span&gt;1&lt;span style="color:#f92672"&gt;)&lt;/span&gt; BSD General Commands Manual BUILTIN&lt;span style="color:#f92672"&gt;(&lt;/span&gt;1&lt;span style="color:#f92672"&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;NAME
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; builtin, !, %, &lt;span style="color:#75715e"&gt;# ...snip...&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;SYNOPSIS
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; builtin &lt;span style="color:#f92672"&gt;[&lt;/span&gt;-options&lt;span style="color:#f92672"&gt;]&lt;/span&gt; &lt;span style="color:#f92672"&gt;[&lt;/span&gt;args ...&lt;span style="color:#f92672"&gt;]&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;However, the manual will &lt;em&gt;not&lt;/em&gt; show information on specific builtins, which is a pain. Your shell &lt;em&gt;might&lt;/em&gt; have an option to show more details - for example, in Bash you can use &lt;code&gt;help&lt;/code&gt;:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-sh" data-lang="sh"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;$ help source
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;source: source filename &lt;span style="color:#f92672"&gt;[&lt;/span&gt;arguments&lt;span style="color:#f92672"&gt;]&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; Read and execute commands from FILENAME and &lt;span style="color:#66d9ef"&gt;return&lt;/span&gt;. The pathnames
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; in $PATH are used to find the directory containing FILENAME. If any
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; ARGUMENTS are supplied, they become the positional parameters when
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; FILENAME is executed.
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;But remember: &lt;code&gt;help&lt;/code&gt; is a builtin; you might not find it in all shells (you won&amp;rsquo;t find it in &lt;code&gt;zsh&lt;/code&gt;, for example). This highlights again the challenges of builtins.&lt;/p&gt;
&lt;h2 id="functions"&gt;Functions&lt;/h2&gt;
&lt;p&gt;You can define your own shell functions. We will see a lot more of this later, but let&amp;rsquo;s show a quick example for now:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-sh" data-lang="sh"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;$ restart-shell &lt;span style="color:#f92672"&gt;()&lt;/span&gt; &lt;span style="color:#f92672"&gt;{&lt;/span&gt; exec -l &lt;span style="color:#e6db74"&gt;&amp;#34;&lt;/span&gt;$SHELL&lt;span style="color:#e6db74"&gt;&amp;#34;&lt;/span&gt; &lt;span style="color:#f92672"&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This snippet creates a function that restarts the shell (quite useful if you are messing with shell configuration files or think you might have irreversibly goofed up your current session).&lt;/p&gt;
&lt;p&gt;We can execute this function just like any command:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-sh" data-lang="sh"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;$ restart-shell
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;And running &lt;code&gt;type&lt;/code&gt; will show us that this is a function:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-sh" data-lang="sh"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;$ type restart-shell
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;restart-shell is a &lt;span style="color:#66d9ef"&gt;function&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;restart-shell &lt;span style="color:#f92672"&gt;()&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#f92672"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; exec -l $SHELL
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#f92672"&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Functions are one of the most powerful shell constructs we will see; they are extremely useful for building sophisticated logic. We&amp;rsquo;re going to see them in a lot more detail later, but for now it is enough to know that they exist, and can run logic, and are run as commands.&lt;/p&gt;
&lt;h2 id="aliases"&gt;Aliases&lt;/h2&gt;
&lt;p&gt;An alias is just a shortcut. Type in a certain set of characters, and the shell will replace them with the value defined in the alias.&lt;/p&gt;
&lt;p&gt;Some common commands are actually already aliases - for example, in my &lt;code&gt;zsh&lt;/code&gt; shell, the &lt;code&gt;ls&lt;/code&gt; command is an alias:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-sh" data-lang="sh"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;% type -a ls
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;ls is an alias &lt;span style="color:#66d9ef"&gt;for&lt;/span&gt; ls -G
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;ls is /bin/ls
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;I make sure that when I use the &lt;code&gt;ls&lt;/code&gt; command, the shell always expands it to &lt;code&gt;ls -G&lt;/code&gt;, which colours the output.&lt;/p&gt;
&lt;p&gt;We can quickly define aliases to save on keystrokes. For example:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-sh" data-lang="sh"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;$ alias k&lt;span style="color:#f92672"&gt;=&lt;/span&gt;&lt;span style="color:#e6db74"&gt;&amp;#39;kubectl&amp;#39;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;From this point on, I can use the &lt;code&gt;k&lt;/code&gt; alias as shorthand for the &lt;code&gt;kubectl&lt;/code&gt; command.&lt;/p&gt;
&lt;p&gt;Aliases are far less sophisticated than functions. Think of them as keystroke savers and nothing more, and you won&amp;rsquo;t go far wrong. Aliases are not portable across shells and have certain behaviours which can make them problematic to work with, there will be an entire chapter dedicated to alisases coming up in the series.&lt;/p&gt;
&lt;h2 id="so-what"&gt;So What?&lt;/h2&gt;
&lt;p&gt;So we now hopefully have a greater understanding of the variety of shell commands. Not all commands are executables, not all of the commands we &lt;em&gt;think&lt;/em&gt; are executables necessarily are, and some commands might be more sophisticated.&lt;/p&gt;
&lt;p&gt;As a shell user, the key things to remember are:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Executables are &amp;lsquo;safe&amp;rsquo; - they are programs your system can use; your shell just calls out to them.&lt;/li&gt;
&lt;li&gt;Builtins are &lt;em&gt;very&lt;/em&gt; shell-specific and usually control the shell itself&lt;/li&gt;
&lt;li&gt;Functions are powerful ways to write logic but will normally be shell-specific.&lt;/li&gt;
&lt;li&gt;Aliases are conveniences for human operators, but only in the context of an interactive shell.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;To find out how a command is implemented, just use the &lt;code&gt;type -a&lt;/code&gt; command:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-sh" data-lang="sh"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;$ type -a cat
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;cat is /bin/cat
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id="more-than-you-need-to-know"&gt;More than You Need to Know&lt;/h2&gt;
&lt;p&gt;OK, for the masochistic few, you might be wondering about all of the other commands and utilities you may have seen that can tell you about programs and commands:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;what&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;whatis&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;which&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;whence&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;where&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;whereis&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;command&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;type&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;A &lt;em&gt;lot&lt;/em&gt; of these are legacy and should be avoided, but for completeness sake, we&amp;rsquo;ll go through them.&lt;/p&gt;
&lt;h3 id="what"&gt;&lt;code&gt;what&lt;/code&gt;&lt;/h3&gt;
&lt;p&gt;&lt;code&gt;what&lt;/code&gt; reads out special metadata embedded in a program, generally used to identify the version of source code it was built from:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-sh" data-lang="sh"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;$ what /bin/ls
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;/bin/ls
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; Copyright &lt;span style="color:#f92672"&gt;(&lt;/span&gt;c&lt;span style="color:#f92672"&gt;)&lt;/span&gt; 1989, 1993, &lt;span style="color:#ae81ff"&gt;1994&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; PROGRAM:ls PROJECT:file_cmds-272.220.1
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;There should be almost no circumstance in which you need to use it in your day-to-day work, but you might come across it if you &lt;em&gt;meant&lt;/em&gt; to type &lt;code&gt;whatis&lt;/code&gt;.&lt;/p&gt;
&lt;h3 id="whatis"&gt;&lt;code&gt;whatis&lt;/code&gt;&lt;/h3&gt;
&lt;p&gt;&lt;code&gt;whatis&lt;/code&gt; searches a local help database for text. This can be useful in tracking down manual pages:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-sh" data-lang="sh"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;$ whatis bash
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;bash&lt;span style="color:#f92672"&gt;(&lt;/span&gt;1&lt;span style="color:#f92672"&gt;)&lt;/span&gt; - GNU Bourne-Again SHell
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;bashbug&lt;span style="color:#f92672"&gt;(&lt;/span&gt;1&lt;span style="color:#f92672"&gt;)&lt;/span&gt; - report a bug in bash
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;But I can&amp;rsquo;t imagine it will be a regularly used tool by most users.&lt;/p&gt;
&lt;h3 id="which"&gt;&lt;code&gt;which&lt;/code&gt;&lt;/h3&gt;
&lt;p&gt;&lt;code&gt;which&lt;/code&gt; will search your &lt;code&gt;$PATH&lt;/code&gt; to see whether an executable can be found. With the &lt;code&gt;-a&lt;/code&gt; flag, it will show all results.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-sh" data-lang="sh"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;$ which -a vi
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;/usr/local/bin/vi
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;/usr/bin/vi
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;code&gt;which&lt;/code&gt; originated in &lt;code&gt;csh&lt;/code&gt;. It remains on many systems for compatibility but in general should be avoided due to potentially odd behaviour&lt;sup id="fnref:4"&gt;&lt;a href="#fn:4" class="footnote-ref" role="doc-noteref"&gt;4&lt;/a&gt;&lt;/sup&gt;.&lt;/p&gt;
&lt;h3 id="whence"&gt;&lt;code&gt;whence&lt;/code&gt;&lt;/h3&gt;
&lt;p&gt;&lt;code&gt;whence&lt;/code&gt; was added to the Korn shell. You are unlikely to use it unless you are on systems using &lt;code&gt;ksh&lt;/code&gt;. &lt;code&gt;zsh&lt;/code&gt; also has this command, but it should be avoided and considered non-standard.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-sh" data-lang="sh"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;% whence brew
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;/usr/local/bin/brew
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h3 id="where"&gt;&lt;code&gt;where&lt;/code&gt;&lt;/h3&gt;
&lt;p&gt;This is a shell builtin that can provide information on commands, similar to &lt;code&gt;type&lt;/code&gt;:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-sh" data-lang="sh"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;% where ls
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;ls: aliased to ls -G
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;/bin/ls
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;However, &lt;code&gt;type&lt;/code&gt; should be preferred, as it is more standard.&lt;/p&gt;
&lt;h3 id="whereis"&gt;&lt;code&gt;whereis&lt;/code&gt;&lt;/h3&gt;
&lt;p&gt;&lt;code&gt;whereis&lt;/code&gt; is available on some systems and generally operates the same as &lt;code&gt;which&lt;/code&gt;, searching paths for an executable:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-sh" data-lang="sh"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;% whereis ls
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;/bin/ls
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Again, &lt;code&gt;type&lt;/code&gt; should be preferred for compatability.&lt;/p&gt;
&lt;h3 id="command"&gt;&lt;code&gt;command&lt;/code&gt;&lt;/h3&gt;
&lt;p&gt;&lt;code&gt;command&lt;/code&gt; is defined in the POSIX standard, so should be expected to be present on most modern systems. Without arguments, it simply executes a command. With the &lt;code&gt;-v&lt;/code&gt; argument, you get a fairly machine-readable or processable response; with the &lt;code&gt;-V&lt;/code&gt; argument, you get a more human readable response:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-sh" data-lang="sh"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;% command -v ls
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;alias ls&lt;span style="color:#f92672"&gt;=&lt;/span&gt;&lt;span style="color:#e6db74"&gt;&amp;#39;ls -G&amp;#39;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;% command -V ls
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;ls is an alias &lt;span style="color:#66d9ef"&gt;for&lt;/span&gt; ls -G
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;code&gt;command&lt;/code&gt; can be useful in scripts, as we will see in later chapters.&lt;/p&gt;
&lt;h3 id="type"&gt;&lt;code&gt;type&lt;/code&gt;&lt;/h3&gt;
&lt;p&gt;&lt;code&gt;type&lt;/code&gt; is part of the Unix standard and will be present in most modern systems. As we&amp;rsquo;ve already seen, it will identify the type of command as well as the location for an executable:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-sh" data-lang="sh"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;% type -a ls
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;ls is an alias &lt;span style="color:#66d9ef"&gt;for&lt;/span&gt; ls -G
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;ls is /bin/ls
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This command can also be used to only search for paths:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-sh" data-lang="sh"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;% type -p ls
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;ls is /bin/ls
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;strong&gt;Summary&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;In summary, avoid anything that starts with &amp;lsquo;&lt;code&gt;w&lt;/code&gt;&amp;rsquo;! These are legacy commands, generally needed only when working on older Unix machines. &lt;code&gt;type&lt;/code&gt; or &lt;code&gt;command&lt;/code&gt; should be used instead.&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;&lt;strong&gt;Footnotes&lt;/strong&gt;&lt;/p&gt;
&lt;div class="footnotes" role="doc-endnotes"&gt;
&lt;hr&gt;
&lt;ol&gt;
&lt;li id="fn:1"&gt;
&lt;p&gt;We will cover permissions and modes in later chapters.&amp;#160;&lt;a href="#fnref:1" class="footnote-backref" role="doc-backlink"&gt;&amp;#x21a9;&amp;#xfe0e;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id="fn:2"&gt;
&lt;p&gt;Why these names and locations? It&amp;rsquo;s a long story. The best place to start if you are intersted is the &lt;a href="https://en.wikipedia.org/wiki/Filesystem_Hierarchy_Standard"&gt;Filesystem Hierarchy Standard&lt;/a&gt;.&amp;#160;&lt;a href="#fnref:2" class="footnote-backref" role="doc-backlink"&gt;&amp;#x21a9;&amp;#xfe0e;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id="fn:3"&gt;
&lt;p&gt;&lt;code&gt;chmod&lt;/code&gt; changes the mode of a file; &lt;code&gt;+x&lt;/code&gt; means &amp;lsquo;add the executable bit&amp;rsquo;. This tells the operating system the file can be executed.&amp;#160;&lt;a href="#fnref:3" class="footnote-backref" role="doc-backlink"&gt;&amp;#x21a9;&amp;#xfe0e;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id="fn:4"&gt;
&lt;p&gt;&lt;a href="https://unix.stackexchange.com/questions/85249/why-not-use-which-what-to-use-then"&gt;Stack Exchange: Why not use “which”? What to use then?&lt;/a&gt;&amp;#160;&lt;a href="#fnref:4" class="footnote-backref" role="doc-backlink"&gt;&amp;#x21a9;&amp;#xfe0e;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;</description><category>CodeProject</category></item><item><title>Effective Shell Part 6: Everything You Don't Need To Know About Job Control</title><link>https://dwmkerr.com/effective-shell-6-job-control/</link><pubDate>Mon, 10 Jun 2019 08:26:33 +0000</pubDate><guid>https://dwmkerr.com/effective-shell-6-job-control/</guid><description>&lt;p&gt;&lt;em&gt;Job control&lt;/em&gt; is a feature of most shells, which is generally not particularly intuitive to work with. However, knowing the basics can help prevent you from getting yourself into a tangle, and can from time to time make certain tasks a little easier.&lt;/p&gt;
&lt;p&gt;In this chapter, we&amp;rsquo;ll look at the main features of job control, why it can be a problematic, and some alternatives.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.dwmkerr.com/effective-shell-part-1-navigating-the-command-line/"&gt;Part 1: Navigating the Command Line&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.dwmkerr.com/effective-shell-part-2-become-a-clipboard-gymnast/"&gt;Part 2: Become a Clipboard Gymnast&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.dwmkerr.com/effective-shell-part-3-getting-hepl/"&gt;Part 3: Getting Help&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dwmkerr.com/effective-shell-4-moving-around/"&gt;Part 4: Moving Around&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dwmkerr.com/effective-shell-part-5-understanding-the-shell/"&gt;Part 5: Interlude - Understanding the Shell&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;a href="https://dwmkerr.com/effective-shell-6-job-control/"&gt;Part 6: Everything You Don&amp;rsquo;t Need to Know About Job Control&lt;/a&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dwmkerr.com/effective-shell-7-shell-commands/"&gt;Part 7: The Subtleties of Shell Commands&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="what-is-job-control"&gt;What Is Job Control?&lt;/h2&gt;
&lt;p&gt;Let&amp;rsquo;s start with an example. I am building a simple web page. It has one &lt;code&gt;index.html&lt;/code&gt; file, one &lt;code&gt;styles.css&lt;/code&gt; file, and one &lt;code&gt;code.js&lt;/code&gt; file. The &lt;code&gt;index.html&lt;/code&gt; file looks like this:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-html" data-lang="html"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&amp;lt;&lt;span style="color:#f92672"&gt;html&lt;/span&gt;&amp;gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &amp;lt;&lt;span style="color:#f92672"&gt;head&lt;/span&gt;&amp;gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &amp;lt;&lt;span style="color:#f92672"&gt;title&lt;/span&gt;&amp;gt;My New Project&amp;lt;/&lt;span style="color:#f92672"&gt;title&lt;/span&gt;&amp;gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &amp;lt;&lt;span style="color:#f92672"&gt;link&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;rel&lt;/span&gt;&lt;span style="color:#f92672"&gt;=&lt;/span&gt;&lt;span style="color:#e6db74"&gt;&amp;#34;stylesheet&amp;#34;&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;type&lt;/span&gt;&lt;span style="color:#f92672"&gt;=&lt;/span&gt;&lt;span style="color:#e6db74"&gt;&amp;#34;text/css&amp;#34;&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;href&lt;/span&gt;&lt;span style="color:#f92672"&gt;=&lt;/span&gt;&lt;span style="color:#e6db74"&gt;&amp;#34;styles.css&amp;#34;&lt;/span&gt;&amp;gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &amp;lt;&lt;span style="color:#f92672"&gt;script&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;src&lt;/span&gt;&lt;span style="color:#f92672"&gt;=&lt;/span&gt;&lt;span style="color:#e6db74"&gt;&amp;#34;code.js&amp;#34;&lt;/span&gt;&amp;gt;&amp;lt;/&lt;span style="color:#f92672"&gt;script&lt;/span&gt;&amp;gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &amp;lt;/&lt;span style="color:#f92672"&gt;head&lt;/span&gt;&amp;gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &amp;lt;&lt;span style="color:#f92672"&gt;body&lt;/span&gt;&amp;gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;&amp;lt;!-- Snip... --&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &amp;lt;/&lt;span style="color:#f92672"&gt;body&lt;/span&gt;&amp;gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&amp;lt;/&lt;span style="color:#f92672"&gt;html&lt;/span&gt;&amp;gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Opening the file in a browser doesn&amp;rsquo;t quite work, as it won&amp;rsquo;t load the code or the styles. We need a web server to serve styles and code.&lt;/p&gt;
&lt;p&gt;A super-useful one-liner to run a web server on any machine with Python installed is:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-sh" data-lang="sh"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;python -m SimpleHTTPServer &lt;span style="color:#ae81ff"&gt;3000&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;In fact, this is so useful that I normally &lt;em&gt;alias&lt;/em&gt; this command, so that I can just type &lt;code&gt;serve&lt;/code&gt;. We&amp;rsquo;ll see aliases in a later chapter.&lt;/p&gt;
&lt;p&gt;For now, if we run this command (you can get &lt;a href="https://github.com/dwmkerr/effective-shell/tree/master/6-job-control/sample"&gt;the three sample files here&lt;/a&gt; if you want to try this yourself), then we can open the webpage in a browser, with the styles and code loaded:&lt;/p&gt;
&lt;img src="images/website-screenshot.png" alt="Screenshot: Website" width="600" /&gt;
&lt;p&gt;We can also see that the server has served the HTML, JavaScript, and CSS files:&lt;/p&gt;
&lt;img src="images/server-screenshot.png" alt="Screenshot: Server" width="600" /&gt;
&lt;p&gt;All well and good so far.&lt;/p&gt;
&lt;h2 id="the-problem"&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Let&amp;rsquo;s say we want to now continue using our shell, maybe to edit the website with a terminal editor like Vim or Emacs, or we want to zip up the site, or just run any shell command&lt;sup id="fnref:1"&gt;&lt;a href="#fn:1" class="footnote-ref" role="doc-noteref"&gt;1&lt;/a&gt;&lt;/sup&gt;.&lt;/p&gt;
&lt;p&gt;We have a problem. The &lt;code&gt;python&lt;/code&gt; process is still running - it&amp;rsquo;s serving the website. Our shell is essentially useless, until we stop the server. See what happens when I try to edit a file:&lt;/p&gt;
&lt;img src="images/blocked-shell.gif" alt="Demo: Blocked Shell" width="600" /&gt;
&lt;p&gt;In the example above, I try to run &lt;code&gt;vi&lt;/code&gt;, but nothing is happening. Standard input is not being read by the server and not being interpreted by the shell.&lt;/p&gt;
&lt;p&gt;I have to kill the server by hitting &lt;code&gt;Ctrl+C&lt;/code&gt; (which sends a &lt;code&gt;SIGINT&lt;/code&gt;&lt;sup id="fnref:2"&gt;&lt;a href="#fn:2" class="footnote-ref" role="doc-noteref"&gt;2&lt;/a&gt;&lt;/sup&gt; - we&amp;rsquo;ll see more about signals later), clear my screen to get rid of all of the error messages, then start again.&lt;/p&gt;
&lt;p&gt;This is obviously not optimal. Let&amp;rsquo;s look at some solutions.&lt;/p&gt;
&lt;h2 id="solution-1-start-the-server-in-the-background"&gt;Solution 1: Start the Server in the Background&lt;/h2&gt;
&lt;p&gt;In most shells, you can run a command and instruct the shell to run it in the &lt;em&gt;background&lt;/em&gt;. To do this, you end the line with an ampersand. Here&amp;rsquo;s how the example would look in this case:&lt;/p&gt;
&lt;img src="images/start-in-background.gif" alt="Demo: Starting a Background Job" width="600" /&gt;
&lt;p&gt;By ending the command with an &lt;code&gt;&amp;amp;&lt;/code&gt; ampersand symbol, we instruct the shell to run the command as a &lt;em&gt;background job&lt;/em&gt;. This means that our shell is still functional. The shell has also notified us that this command is running as a background job with a specific &lt;em&gt;job number&lt;/em&gt;:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-sh" data-lang="sh"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;% python -m SimpleHTTPServer &lt;span style="color:#ae81ff"&gt;3000&lt;/span&gt; &amp;amp;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#f92672"&gt;[&lt;/span&gt;1&lt;span style="color:#f92672"&gt;]&lt;/span&gt; &lt;span style="color:#ae81ff"&gt;19372&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;In slightly obtuse language, the shell has informed us that it has started a job in the background, with job number &lt;code&gt;1&lt;/code&gt; and that this job is currently handling the process with ID &lt;code&gt;19372&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The ampersand solution is a fairly common pattern used in day-to-day work.&lt;/p&gt;
&lt;h2 id="solution-2-move-the-server-to-the-background"&gt;Solution 2: Move the Server to the Background&lt;/h2&gt;
&lt;p&gt;Let&amp;rsquo;s say you forgot to start the command in the background. Most likely in this case you&amp;rsquo;d kill the server with &lt;code&gt;Ctrl+C&lt;/code&gt; and then start it again with the &lt;code&gt;&amp;amp;&lt;/code&gt; option. However, what if this was a large file download or a task you didn&amp;rsquo;t want to abort?&lt;/p&gt;
&lt;p&gt;In the example below, we&amp;rsquo;ll move the job to the background:&lt;/p&gt;
&lt;img src="images/move-to-background.gif" alt="Demo: Moving a Job to the Background" width="600" /&gt;
&lt;p&gt;The process is currently in the foreground, so my shell is inactive. Hitting &lt;code&gt;Ctrl+Z&lt;/code&gt; sends a &amp;lsquo;suspend&amp;rsquo; signal to the process&lt;sup id="fnref:3"&gt;&lt;a href="#fn:3" class="footnote-ref" role="doc-noteref"&gt;3&lt;/a&gt;&lt;/sup&gt;, pausing it and moving it to the background.&lt;/p&gt;
&lt;p&gt;Let&amp;rsquo;s dissect this:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-sh" data-lang="sh"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;% python -m SimpleHTTPServer &lt;span style="color:#ae81ff"&gt;3000&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;Serving HTTP on 0.0.0.0 port &lt;span style="color:#ae81ff"&gt;3000&lt;/span&gt; ...
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;127.0.0.1 - - &lt;span style="color:#f92672"&gt;[&lt;/span&gt;03/Jun/2019 13:38:45&lt;span style="color:#f92672"&gt;]&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#34;GET / HTTP/1.1&amp;#34;&lt;/span&gt; &lt;span style="color:#ae81ff"&gt;200&lt;/span&gt; -
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;^Z
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#f92672"&gt;[&lt;/span&gt;1&lt;span style="color:#f92672"&gt;]&lt;/span&gt; + &lt;span style="color:#ae81ff"&gt;21268&lt;/span&gt; suspended python -m SimpleHTTPServer &lt;span style="color:#ae81ff"&gt;3000&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The shell echos as I type, so we see &lt;code&gt;^Z&lt;/code&gt; (i.e., the &lt;code&gt;Ctrl+Z&lt;/code&gt; chord I entered). The shell responds by moving the process into a background job and suspending it.&lt;/p&gt;
&lt;p&gt;The key here is that it is &lt;em&gt;suspended&lt;/em&gt;. The process is paused. So the web server is no longer serving. If you are following with the sample, reload your browser. The webpage fails to load, as the server process is not able to respond to requests.&lt;/p&gt;
&lt;p&gt;To &lt;em&gt;continue&lt;/em&gt; the job, in the background, we use the &lt;code&gt;bg&lt;/code&gt; (&amp;lsquo;background&amp;rsquo;) command, with a &lt;em&gt;job identifier&lt;/em&gt; (which always starts with a &lt;code&gt;%&lt;/code&gt; symbol - we&amp;rsquo;ll see why soon) to tell the shell to continue the job:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-sh" data-lang="sh"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;% bg %1
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#f92672"&gt;[&lt;/span&gt;1&lt;span style="color:#f92672"&gt;]&lt;/span&gt; + &lt;span style="color:#ae81ff"&gt;21268&lt;/span&gt; continued python -m SimpleHTTPServer &lt;span style="color:#ae81ff"&gt;3000&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The shell lets us know the job is being continued, and if we load the webpage again, the content is shown as expected.&lt;/p&gt;
&lt;p&gt;As a final check, we run the &lt;code&gt;jobs&lt;/code&gt; command to see what jobs the shell is running:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-sh" data-lang="sh"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;% jobs
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#f92672"&gt;[&lt;/span&gt;1&lt;span style="color:#f92672"&gt;]&lt;/span&gt; + running python -m SimpleHTTPServer &lt;span style="color:#ae81ff"&gt;3000&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;And there you have it - our server is running as a background job. This is exactly what we would see if we run &lt;code&gt;jobs&lt;/code&gt; after starting the server with an &lt;code&gt;&amp;amp;&lt;/code&gt; at the end. In fact, using an &lt;code&gt;&amp;amp;&lt;/code&gt; is perhaps an easier way to remember how to continue a suspended job:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-sh" data-lang="sh"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;% %1 &amp;amp;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#f92672"&gt;[&lt;/span&gt;1&lt;span style="color:#f92672"&gt;]&lt;/span&gt; + &lt;span style="color:#ae81ff"&gt;21268&lt;/span&gt; continued python -m SimpleHTTPServer &lt;span style="color:#ae81ff"&gt;3000&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;In the same way ending a command with &lt;code&gt;&amp;amp;&lt;/code&gt; runs it in the background, ending a job identifier with &lt;code&gt;&amp;amp;&lt;/code&gt; &lt;em&gt;continues&lt;/em&gt; it in the background.&lt;/p&gt;
&lt;p&gt;There is at least one more way to move a job to the background&lt;sup id="fnref:4"&gt;&lt;a href="#fn:4" class="footnote-ref" role="doc-noteref"&gt;4&lt;/a&gt;&lt;/sup&gt;, but I have not yet found it useful in any scenarios, and it is overly complex to explain. See the footnote for details if you are interested.&lt;/p&gt;
&lt;h2 id="moving-background-jobs-to-the-foreground"&gt;Moving Background Jobs to the Foreground&lt;/h2&gt;
&lt;p&gt;If you have a job in the background, you can bring it back to the foreground with the &lt;code&gt;fg&lt;/code&gt; (&amp;lsquo;foreground&amp;rsquo;) command. Let&amp;rsquo;s show the jobs, with the &lt;code&gt;jobs&lt;/code&gt; command:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-sh" data-lang="sh"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;% jobs
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#f92672"&gt;[&lt;/span&gt;1&lt;span style="color:#f92672"&gt;]&lt;/span&gt; + running python -m SimpleHTTPServer &lt;span style="color:#ae81ff"&gt;3000&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Here I have a background job running a server. Any one of the following commands will bring it back to the foreground:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-sh" data-lang="sh"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;fg %1 &lt;span style="color:#75715e"&gt;# Explicitly bring Job 1 into the foreground&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;%1 &lt;span style="color:#75715e"&gt;# ...or in shorthand, just enter the job id...&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;fg &lt;span style="color:#75715e"&gt;# ...if not given an id, fg and bg assume the most recent job.&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Now the job is in the foreground, and you can interact with the process again however you like.&lt;/p&gt;
&lt;h2 id="cleaning-up-jobs"&gt;Cleaning Up Jobs&lt;/h2&gt;
&lt;p&gt;You might realise you cannot continue what you are doing because an old job is &lt;em&gt;still running&lt;/em&gt;. Here&amp;rsquo;s an example:&lt;/p&gt;
&lt;img src="images/kill-job.gif" alt="Demo: Cleaning Up Jobs" width="600" /&gt;
&lt;p&gt;I tried to run my web server, but there was still one running as a background job. The server failed to start because the port is in use.&lt;/p&gt;
&lt;p&gt;To clean it up, I run the &lt;code&gt;jobs&lt;/code&gt; command to list the jobs:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-sh" data-lang="sh"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;% jobs
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#f92672"&gt;[&lt;/span&gt;1&lt;span style="color:#f92672"&gt;]&lt;/span&gt; + suspended python -m SimpleHTTPServer &lt;span style="color:#ae81ff"&gt;3000&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;There&amp;rsquo;s my old web server. Note that even though it is suspended, it&amp;rsquo;ll still be blocking the port it is serving on&lt;sup id="fnref:5"&gt;&lt;a href="#fn:5" class="footnote-ref" role="doc-noteref"&gt;5&lt;/a&gt;&lt;/sup&gt;. The process is paused, but it is still holding onto all of the resources it is using.&lt;/p&gt;
&lt;p&gt;Now that I know the job identifier (&lt;code&gt;%1&lt;/code&gt; in this case), I can kill the job:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-sh" data-lang="sh"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;% kill %1
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#f92672"&gt;[&lt;/span&gt;1&lt;span style="color:#f92672"&gt;]&lt;/span&gt; + &lt;span style="color:#ae81ff"&gt;22843&lt;/span&gt; terminated python -m SimpleHTTPServer &lt;span style="color:#ae81ff"&gt;3000&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;em&gt;This is why job identifiers start with a percentage sign!&lt;/em&gt; The &lt;code&gt;kill&lt;/code&gt; command I have used is not a special job control command (like &lt;code&gt;bg&lt;/code&gt; or &lt;code&gt;fg&lt;/code&gt;). It is the normal &lt;code&gt;kill&lt;/code&gt; command, which terminates a process. But shells that support job control can normally use a job identifier in place of a &lt;em&gt;process identifier&lt;/em&gt;. So rather than working out what the process identifier is that I need to kill, I can just use the job identifier&lt;sup id="fnref:6"&gt;&lt;a href="#fn:6" class="footnote-ref" role="doc-noteref"&gt;6&lt;/a&gt;&lt;/sup&gt;.&lt;/p&gt;
&lt;h2 id="why-you-shouldnt-use-jobs"&gt;Why You Shouldn&amp;rsquo;t Use Jobs&lt;/h2&gt;
&lt;p&gt;Avoid jobs. They are not intuitive to interface with, and they suffer from some serious problems.&lt;/p&gt;
&lt;p&gt;The most obvious one is that all jobs write to the same output, meaning you can quickly get garbled output like this:&lt;/p&gt;
&lt;img src="images/output.png" alt="Screenshot: Garbled Output" width="600" /&gt;
&lt;p&gt;This is what happens when I run a job, which just outputs text every second. It&amp;rsquo;s in the background, but it&amp;rsquo;s printing all over my commands. Even running the &lt;code&gt;jobs&lt;/code&gt; command to try and find the job to stop it is difficult.&lt;/p&gt;
&lt;p&gt;Input is even more complex. If a job is &lt;em&gt;running&lt;/em&gt; in the background, but requires input, it will be &lt;em&gt;silently suspended&lt;/em&gt;. This can cause confusion.&lt;/p&gt;
&lt;p&gt;Jobs &lt;em&gt;can&lt;/em&gt; be used in scripts but must be done so with caution and could easily confuse a consumer of the script if they leave background jobs hanging around, which cannot be easily cleaned up&lt;sup id="fnref:7"&gt;&lt;a href="#fn:7" class="footnote-ref" role="doc-noteref"&gt;7&lt;/a&gt;&lt;/sup&gt;.&lt;/p&gt;
&lt;p&gt;Handling errors and exit codes for jobs can be problematic, causing confusion, poor error handling, or overly complex code.&lt;/p&gt;
&lt;h2 id="how-to-escape-jobs"&gt;How to Escape Jobs&lt;/h2&gt;
&lt;p&gt;If there are two things to take away, it would be this:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;If you have started running a command in the foreground, and you don&amp;rsquo;t want to stop it and would rather move it to the background, hit &lt;code&gt;Ctrl+Z&lt;/code&gt;. Then Google &amp;ldquo;job control&amp;rdquo;.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;And:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;If you think there is a job running in the background, and it is messing with your screen, type &lt;code&gt;fg&lt;/code&gt; to bring it to the front and kill it with &lt;code&gt;Ctrl+C&lt;/code&gt;. Repeat as needed!&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;In either case, if you need to do something more subtle, you can return to this reference. But the first command should allow you to get your shell back while you work out how to continue the job, and the second should kill a background job that is messing with your screen.&lt;/p&gt;
&lt;h2 id="alternatives-to-jobs"&gt;Alternatives to Jobs&lt;/h2&gt;
&lt;p&gt;If you are using any kind of modern terminal such as iTerm, Terminal or the GNOME Terminal, just open a new tab or split! Much easier.&lt;/p&gt;
&lt;p&gt;The benefit to this is that each tab gets its own standard input and output, so there&amp;rsquo;s no risk of overwriting. And of course you can hide/reveal/rearrange the tabs however you like.&lt;/p&gt;
&lt;p&gt;The traditional alternative to a job for an operator who simply wants more than one thing going on at once would be a &lt;em&gt;terminal multiplexer&lt;/em&gt;, such as &lt;code&gt;screen&lt;/code&gt; or &lt;code&gt;tmux&lt;/code&gt;:&lt;/p&gt;
&lt;p&gt;&lt;img src="images/terminal-multiplexer.gif" alt="terminal-multiplexer"&gt;&lt;/p&gt;
&lt;p&gt;Multiplexers work in a very similar way to a modern graphical terminal - they manage many shell instances. The benefits to a modern terminal, such as iTerm, is that you have a very intuitive GUI and lots of features.&lt;/p&gt;
&lt;p&gt;The benefits to a multiplexer are that you can run them over SSH sessions to manage complex operations on remote machines and that they run a client-server model, meaning many people can work with many multiplexed processes (and they can persist beyond sessions).&lt;/p&gt;
&lt;p&gt;My personal preference is both - I use a modern terminal &lt;em&gt;and&lt;/em&gt; run everything inside it in &lt;code&gt;tmux&lt;/code&gt;. We&amp;rsquo;ll look at both of these options in later chapters.&lt;/p&gt;
&lt;h2 id="quick-reference"&gt;Quick Reference&lt;/h2&gt;
&lt;p&gt;You might find that jobs are useful, or you might find that they are not. Either way, here&amp;rsquo;s a quick reference of some common commands:&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Command&lt;/th&gt;
&lt;th&gt;Usage&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;command &amp;amp;&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Run the command as a background job.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;&amp;lt;Ctrl+Z&amp;gt;&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Move the current process into a background job, suspended.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;jobs&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;List all jobs.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;fg %1&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Move background job number 1 into the foreground.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;bg %1&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Continue background job number 1.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;kill %1&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Terminate job number 1.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;wait %1&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Block until job number 1 exits.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;If you want to find out more about the gory details of jobs, the best place to start is the &lt;a href="https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#Job-Control"&gt;Bash Manual - Job Control Section&lt;/a&gt;, or the &amp;lsquo;Job Control&amp;rsquo; section of your preferred shell&amp;rsquo;s manual.&lt;/p&gt;
&lt;p&gt;I hope you found this useful, and, as always, please leave comments, questions or suggestions below!&lt;/p&gt;
&lt;hr&gt;
&lt;h2 id="footnotes"&gt;Footnotes&lt;/h2&gt;
&lt;div class="footnotes" role="doc-endnotes"&gt;
&lt;hr&gt;
&lt;ol&gt;
&lt;li id="fn:1"&gt;
&lt;p&gt;If you are not a heavy shell user, this might seem unlikely. But if you do a lot of work in shells, such as sysadmin, devops, or do your coding from a terminal, this happens all the time!&amp;#160;&lt;a href="#fnref:1" class="footnote-backref" role="doc-backlink"&gt;&amp;#x21a9;&amp;#xfe0e;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id="fn:2"&gt;
&lt;p&gt;Signals like &lt;code&gt;SIGINT&lt;/code&gt;, &lt;code&gt;SIGKILL&lt;/code&gt;, &lt;code&gt;SIGTERM&lt;/code&gt; and so on will be covered in a later chapter.&amp;#160;&lt;a href="#fnref:2" class="footnote-backref" role="doc-backlink"&gt;&amp;#x21a9;&amp;#xfe0e;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id="fn:3"&gt;
&lt;p&gt;Technically, &lt;code&gt;SIGTSTP&lt;/code&gt; - which is &amp;lsquo;TTY stop&amp;rsquo;. If you have always wondered about the &amp;lsquo;TTY&amp;rsquo; acroynm, check the previous chatper, &lt;a href="https://dwmkerr.com/effective-shell-part-5-understanding-the-shell/"&gt;Interlude: Understanding the Shell&lt;/a&gt;.&amp;#160;&lt;a href="#fnref:3" class="footnote-backref" role="doc-backlink"&gt;&amp;#x21a9;&amp;#xfe0e;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id="fn:4"&gt;
&lt;p&gt;The alternative method is to use &lt;code&gt;Ctrl+Y&lt;/code&gt;, which will send a &lt;em&gt;delayed interrupt&lt;/em&gt;, which will continue to run the process until it tries to read from &lt;code&gt;stdin&lt;/code&gt;. At this point, the job is suspended and the control given to the shell. The operator can then use &lt;code&gt;bg&lt;/code&gt; or &lt;code&gt;kill&lt;/code&gt; or &lt;code&gt;fg&lt;/code&gt; to either move to the background, stop the process, or keep in the foreground as preferred. See: &lt;a href="https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#Job-Control"&gt;https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#Job-Control&lt;/a&gt;&amp;#160;&lt;a href="#fnref:4" class="footnote-backref" role="doc-backlink"&gt;&amp;#x21a9;&amp;#xfe0e;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id="fn:5"&gt;
&lt;p&gt;Another super-useful snippet: &lt;code&gt;lsof -i -P -n | grep 8000&lt;/code&gt; to find any process that has a given port open. Another one for the aliases chapter!&amp;#160;&lt;a href="#fnref:5" class="footnote-backref" role="doc-backlink"&gt;&amp;#x21a9;&amp;#xfe0e;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id="fn:6"&gt;
&lt;p&gt;There are times this is needed. If a job runs &lt;em&gt;many processes&lt;/em&gt; - for example, by running a pipeline - the process identifier will change as the command moves from one stage of the pipeline to the next. The job identifier will remain constant. Remember, a job is a shell &lt;em&gt;command&lt;/em&gt;, so could run many processes.&amp;#160;&lt;a href="#fnref:6" class="footnote-backref" role="doc-backlink"&gt;&amp;#x21a9;&amp;#xfe0e;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id="fn:7"&gt;
&lt;p&gt;To see how bad this can be, create a script that starts jobs, then run it. Then run the &lt;code&gt;jobs&lt;/code&gt; command to see what is running. The output might surprise you!&amp;#160;&lt;a href="#fnref:7" class="footnote-backref" role="doc-backlink"&gt;&amp;#x21a9;&amp;#xfe0e;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;</description><category>CodeProject</category></item><item><title>A portable and magic-free way to open Pull Requests from the Command Line</title><link>https://dwmkerr.com/a-portable-and-magic-free-way-to-open-pull-requests-from-the-command-line/</link><pubDate>Wed, 10 Oct 2018 09:17:26 +0000</pubDate><guid>https://dwmkerr.com/a-portable-and-magic-free-way-to-open-pull-requests-from-the-command-line/</guid><description>&lt;p&gt;This little bash snippet will let you open a GitHub or GitLab pull request from the command line on most Unix-like systems (OSX, Ubuntu, etc), without using any magic libraries, ZSH tricks or other dependencies.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;tl;dr&lt;/strong&gt; download the &lt;a href="https://gist.github.com/dwmkerr/bae3fdca2d7208ec5d0008911d79b47d"&gt;&lt;code&gt;gpr.sh&lt;/code&gt;&lt;/a&gt; gist.&lt;/p&gt;
&lt;p&gt;&lt;img src="images/gpr.png" alt="gpr"&gt;&lt;/p&gt;
&lt;p&gt;Here&amp;rsquo;s how it looks in action OSX:&lt;/p&gt;
&lt;p&gt;&lt;img src="images/gpr.gif" alt="gpr"&gt;&lt;/p&gt;
&lt;p&gt;And Ubuntu:&lt;/p&gt;
&lt;p&gt;&lt;img src="images/gpr-ubuntu.gif" alt="gpr-ubuntu"&gt;&lt;/p&gt;
&lt;p&gt;The script is available as the &lt;a href="https://gist.github.com/dwmkerr/bae3fdca2d7208ec5d0008911d79b47d"&gt;&lt;code&gt;gpr.sh&lt;/code&gt;&lt;/a&gt; gist. You can also find it in my &lt;a href="https://github.com/dwmkerr/dotfiles"&gt;dotfiles&lt;/a&gt;, in the &lt;a href="https://github.com/dwmkerr/dotfiles/blob/master/profile/git.sh"&gt;git.sh&lt;/a&gt; file.&lt;/p&gt;
&lt;h2 id="the-script"&gt;The Script&lt;/h2&gt;
&lt;p&gt;Here&amp;rsquo;s the script in its entirety:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;# Colour constants for nicer output.&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;GREEN&lt;span style="color:#f92672"&gt;=&lt;/span&gt;&lt;span style="color:#e6db74"&gt;&amp;#39;\033[0;32m&amp;#39;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;RESET&lt;span style="color:#f92672"&gt;=&lt;/span&gt;&lt;span style="color:#e6db74"&gt;&amp;#39;\033[0m&amp;#39;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;# Push the current branch to origin, set upstream, open the PR page if possible.&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;gpr&lt;span style="color:#f92672"&gt;()&lt;/span&gt; &lt;span style="color:#f92672"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;# Get the current branch name, or use &amp;#39;HEAD&amp;#39; if we cannot get it.&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; branch&lt;span style="color:#f92672"&gt;=&lt;/span&gt;&lt;span style="color:#66d9ef"&gt;$(&lt;/span&gt;git symbolic-ref -q HEAD&lt;span style="color:#66d9ef"&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; branch&lt;span style="color:#f92672"&gt;=&lt;/span&gt;&lt;span style="color:#e6db74"&gt;${&lt;/span&gt;branch##refs/heads/&lt;span style="color:#e6db74"&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; branch&lt;span style="color:#f92672"&gt;=&lt;/span&gt;&lt;span style="color:#e6db74"&gt;${&lt;/span&gt;branch&lt;span style="color:#66d9ef"&gt;:-&lt;/span&gt;HEAD&lt;span style="color:#e6db74"&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;# Pushing take a little while, so let the user know we&amp;#39;re working.&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; echo &lt;span style="color:#e6db74"&gt;&amp;#34;Opening pull request for &lt;/span&gt;&lt;span style="color:#e6db74"&gt;${&lt;/span&gt;GREEN&lt;span style="color:#e6db74"&gt;}${&lt;/span&gt;branch&lt;span style="color:#e6db74"&gt;}${&lt;/span&gt;RESET&lt;span style="color:#e6db74"&gt;}&lt;/span&gt;&lt;span style="color:#e6db74"&gt;...&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;# Push to origin, grabbing the output but then echoing it back.&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; push_output&lt;span style="color:#f92672"&gt;=&lt;/span&gt;&lt;span style="color:#e6db74"&gt;`&lt;/span&gt;git push origin -u &lt;span style="color:#e6db74"&gt;${&lt;/span&gt;branch&lt;span style="color:#e6db74"&gt;}&lt;/span&gt; 2&amp;gt;&amp;amp;1&lt;span style="color:#e6db74"&gt;`&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; echo &lt;span style="color:#e6db74"&gt;&amp;#34;&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; echo &lt;span style="color:#e6db74"&gt;${&lt;/span&gt;push_output&lt;span style="color:#e6db74"&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;# If there&amp;#39;s anything which starts with http, it&amp;#39;s a good guess it&amp;#39;ll be a&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;# link to GitHub/GitLab/Whatever. So open it.&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; link&lt;span style="color:#f92672"&gt;=&lt;/span&gt;&lt;span style="color:#66d9ef"&gt;$(&lt;/span&gt;echo &lt;span style="color:#e6db74"&gt;${&lt;/span&gt;push_output&lt;span style="color:#e6db74"&gt;}&lt;/span&gt; | grep -o &lt;span style="color:#e6db74"&gt;&amp;#39;http.*&amp;#39;&lt;/span&gt; | sed -e &lt;span style="color:#e6db74"&gt;&amp;#39;s/[[:space:]]*$//&amp;#39;&lt;/span&gt;&lt;span style="color:#66d9ef"&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;if&lt;/span&gt; &lt;span style="color:#f92672"&gt;[&lt;/span&gt; &lt;span style="color:#e6db74"&gt;${&lt;/span&gt;link&lt;span style="color:#e6db74"&gt;}&lt;/span&gt; &lt;span style="color:#f92672"&gt;]&lt;/span&gt;; &lt;span style="color:#66d9ef"&gt;then&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; echo &lt;span style="color:#e6db74"&gt;&amp;#34;&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; echo &lt;span style="color:#e6db74"&gt;&amp;#34;Opening: &lt;/span&gt;&lt;span style="color:#e6db74"&gt;${&lt;/span&gt;GREEN&lt;span style="color:#e6db74"&gt;}${&lt;/span&gt;link&lt;span style="color:#e6db74"&gt;}${&lt;/span&gt;RESET&lt;span style="color:#e6db74"&gt;}&lt;/span&gt;&lt;span style="color:#e6db74"&gt;...&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; python -mwebbrowser &lt;span style="color:#e6db74"&gt;${&lt;/span&gt;link&lt;span style="color:#e6db74"&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;fi&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#f92672"&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id="how-it-works"&gt;How It Works&lt;/h2&gt;
&lt;p&gt;Blow-by-blow, let&amp;rsquo;s take a look.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;# Colour constants for nicer output.&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;GREEN&lt;span style="color:#f92672"&gt;=&lt;/span&gt;&lt;span style="color:#e6db74"&gt;&amp;#39;\033[0;32m&amp;#39;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;RESET&lt;span style="color:#f92672"&gt;=&lt;/span&gt;&lt;span style="color:#e6db74"&gt;&amp;#39;\033[0m&amp;#39;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;To make colouring console output easier, we create strings with the escape code required to set the &amp;lsquo;green&amp;rsquo; colour, and reset the text colour.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;gpr&lt;span style="color:#f92672"&gt;()&lt;/span&gt; &lt;span style="color:#f92672"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;# Get the current branch name, or use &amp;#39;HEAD&amp;#39; if we cannot get it.&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; branch&lt;span style="color:#f92672"&gt;=&lt;/span&gt;&lt;span style="color:#66d9ef"&gt;$(&lt;/span&gt;git symbolic-ref -q HEAD&lt;span style="color:#66d9ef"&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; branch&lt;span style="color:#f92672"&gt;=&lt;/span&gt;&lt;span style="color:#e6db74"&gt;${&lt;/span&gt;branch##refs/heads/&lt;span style="color:#e6db74"&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; branch&lt;span style="color:#f92672"&gt;=&lt;/span&gt;&lt;span style="color:#e6db74"&gt;${&lt;/span&gt;branch&lt;span style="color:#66d9ef"&gt;:-&lt;/span&gt;HEAD&lt;span style="color:#e6db74"&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Now we define the &lt;code&gt;gpr&lt;/code&gt; (Git Pull Request) function. We&amp;rsquo;ll need to push the current branch, so we need to get the current branch name. There&amp;rsquo;s plenty of discussion on how this works on &lt;a href="https://stackoverflow.com/questions/6245570/how-to-get-the-current-branch-name-in-git"&gt;Stack Overflow: How to get the current branch name in Git&lt;/a&gt;. Essentially we just get the symbolic name for the head of our current branch, which will be something like this:&lt;/p&gt;
&lt;pre tabindex="0"&gt;&lt;code&gt;refs/heads/my-new-branch
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;We then use &lt;a href="https://www.tldp.org/LDP/abs/html/string-manipulation.html"&gt;Bash substring removal&lt;/a&gt; to rip out the &lt;code&gt;ref/heads/&lt;/code&gt; part. If we have no branch (for example, we are detached) we just use &lt;code&gt;HEAD&lt;/code&gt; a the branch name.&lt;/p&gt;
&lt;p&gt;Next we have this:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;# Pushing take a little while, so let the user know we&amp;#39;re working.&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; echo &lt;span style="color:#e6db74"&gt;&amp;#34;Opening pull request for &lt;/span&gt;&lt;span style="color:#e6db74"&gt;${&lt;/span&gt;GREEN&lt;span style="color:#e6db74"&gt;}${&lt;/span&gt;branch&lt;span style="color:#e6db74"&gt;}${&lt;/span&gt;RESET&lt;span style="color:#e6db74"&gt;}&lt;/span&gt;&lt;span style="color:#e6db74"&gt;...&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;# Push to origin, grabbing the output but then echoing it back.&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; push_output&lt;span style="color:#f92672"&gt;=&lt;/span&gt;&lt;span style="color:#e6db74"&gt;`&lt;/span&gt;git push origin -u &lt;span style="color:#e6db74"&gt;${&lt;/span&gt;branch&lt;span style="color:#e6db74"&gt;}&lt;/span&gt; 2&amp;gt;&amp;amp;1&lt;span style="color:#e6db74"&gt;`&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; echo &lt;span style="color:#e6db74"&gt;&amp;#34;&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; echo &lt;span style="color:#e6db74"&gt;${&lt;/span&gt;push_output&lt;span style="color:#e6db74"&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;We&amp;rsquo;ve previously defined some strings which include the escape codes to colour terminal output. Now we just show the user the branch we&amp;rsquo;re going to push, push it and then store all of the output in the &lt;code&gt;push_output&lt;/code&gt; variable.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;2&amp;gt;&amp;amp;1&lt;/code&gt; idiom is a common one. This simply makes sure we put all &lt;code&gt;stderr&lt;/code&gt; output (which is always file descriptor 2) into &lt;code&gt;stdout&lt;/code&gt; (which is always file descriptor 1). This means whether the program writes output to &lt;code&gt;stdout&lt;/code&gt; or &lt;code&gt;stderr&lt;/code&gt;, we capture it. There&amp;rsquo;s a nice write-up on this in the blog post &amp;lsquo;&lt;a href="https://www.brianstorti.com/understanding-shell-script-idiom-redirect/"&gt;Understanding Shell Script&amp;rsquo;s idiom: 2&amp;gt;&amp;amp;1
&lt;/a&gt;&amp;rsquo;.&lt;/p&gt;
&lt;p&gt;The output from Git push will be dependent on the Git server being used. For GitHub it&amp;rsquo;ll look like this:&lt;/p&gt;
&lt;pre tabindex="0"&gt;&lt;code&gt;remote:
remote: Create a pull request for &amp;#39;feat/doc-cleanup&amp;#39; on GitHub by visiting:
remote: https://github.com/dwmkerr/dotfiles/pull/new/feat/doc-cleanup
remote:
To github.com:dwmkerr/dotfiles
* [new branch] feat/doc-cleanup -&amp;gt; feat/doc-cleanup
Branch feat/doc-cleanup set up to track remote branch feat/doc-cleanup from origin.
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Now all we want to do is see if there is any text which starts with &lt;code&gt;http&lt;/code&gt; and if there is, then open it. Here&amp;rsquo;s how we do that:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;# If there&amp;#39;s anything which starts with http, it&amp;#39;s a good guess it&amp;#39;ll be a&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;# link to GitHub/GitLab/Whatever. So open it.&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; link&lt;span style="color:#f92672"&gt;=&lt;/span&gt;&lt;span style="color:#66d9ef"&gt;$(&lt;/span&gt;echo &lt;span style="color:#e6db74"&gt;${&lt;/span&gt;push_output&lt;span style="color:#e6db74"&gt;}&lt;/span&gt; | grep -o &lt;span style="color:#e6db74"&gt;&amp;#39;http.*&amp;#39;&lt;/span&gt; | sed -e &lt;span style="color:#e6db74"&gt;&amp;#39;s/[[:space:]]*$//&amp;#39;&lt;/span&gt;&lt;span style="color:#66d9ef"&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;if&lt;/span&gt; &lt;span style="color:#f92672"&gt;[&lt;/span&gt; &lt;span style="color:#e6db74"&gt;${&lt;/span&gt;link&lt;span style="color:#e6db74"&gt;}&lt;/span&gt; &lt;span style="color:#f92672"&gt;]&lt;/span&gt;; &lt;span style="color:#66d9ef"&gt;then&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; echo &lt;span style="color:#e6db74"&gt;&amp;#34;&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; echo &lt;span style="color:#e6db74"&gt;&amp;#34;Opening: &lt;/span&gt;&lt;span style="color:#e6db74"&gt;${&lt;/span&gt;GREEN&lt;span style="color:#e6db74"&gt;}${&lt;/span&gt;link&lt;span style="color:#e6db74"&gt;}${&lt;/span&gt;RESET&lt;span style="color:#e6db74"&gt;}&lt;/span&gt;&lt;span style="color:#e6db74"&gt;...&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; python -mwebbrowser &lt;span style="color:#e6db74"&gt;${&lt;/span&gt;link&lt;span style="color:#e6db74"&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;fi&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This uses &lt;code&gt;grep&lt;/code&gt; to rip out everything from &lt;code&gt;http&lt;/code&gt; onwards, and the &lt;code&gt;sed&lt;/code&gt; to remove any trailing whitespace. If we have found a link, we use &lt;code&gt;python&lt;/code&gt; to open it (which is a fairly safe cross-platform solution).&lt;/p&gt;
&lt;p&gt;That&amp;rsquo;s it! When you have a branch ready which you want to push and create a pull request from, just run:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;gpr
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;And the branch will be pushed to &lt;code&gt;origin&lt;/code&gt;, and if there is a Pull Request webpage, it&amp;rsquo;ll be opened.&lt;/p&gt;
&lt;h2 id="prior-art"&gt;Prior Art&lt;/h2&gt;
&lt;p&gt;My colleague Tobias recently shared a nice trick we worked out to open a GitLab merge request - which also now works for GitHub:&lt;/p&gt;
&lt;blockquote class="twitter-tweet" data-lang="en"&gt;&lt;p lang="en" dir="ltr"&gt;git push and directly open PR in Chrome - works for &lt;a href="https://twitter.com/github?ref_src=twsrc%5Etfw"&gt;@github&lt;/a&gt; &amp;amp; &lt;a href="https://twitter.com/gitlab?ref_src=twsrc%5Etfw"&gt;@gitlab&lt;/a&gt; 🚀&lt;br&gt;&lt;br&gt;Here is how to set it up 👉 &lt;a href="https://t.co/YfNTmdwTFt"&gt;https://t.co/YfNTmdwTFt&lt;/a&gt; &lt;a href="https://twitter.com/hashtag/github?src=hash&amp;amp;ref_src=twsrc%5Etfw"&gt;#github&lt;/a&gt; &lt;a href="https://twitter.com/hashtag/gitlab?src=hash&amp;amp;ref_src=twsrc%5Etfw"&gt;#gitlab&lt;/a&gt; &lt;a href="https://t.co/ISE9kVZmw1"&gt;pic.twitter.com/ISE9kVZmw1&lt;/a&gt;&lt;/p&gt;&amp;mdash; Tobias Büschel (@TobiasBueschel) &lt;a href="https://twitter.com/TobiasBueschel/status/1042452158430502915?ref_src=twsrc%5Etfw"&gt;September 19, 2018&lt;/a&gt;&lt;/blockquote&gt;
&lt;script async src="https://platform.twitter.com/widgets.js" charset="utf-8"&gt;&lt;/script&gt;
&lt;p&gt;I wanted to be able to use the same trick in Ubuntu and other Linux distros, but realised it relied on &lt;a href="https://github.com/robbyrussell/oh-my-zsh"&gt;oh-my-zsh&lt;/a&gt; and assumed OSX with Chrome as the browser, so tweaked it to the above. Thanks Tobi!&lt;/p&gt;</description><category>CodeProject</category></item><item><title>Effective Shell Part 3: Getting Help</title><link>https://dwmkerr.com/effective-shell-part-3-getting-hepl/</link><pubDate>Tue, 19 Dec 2017 09:05:18 +0000</pubDate><guid>https://dwmkerr.com/effective-shell-part-3-getting-hepl/</guid><description>&lt;p&gt;This is the third part of my &lt;a href="https://github.com/dwmkerr/effective-shell"&gt;Effective Shell&lt;/a&gt; series - practical examples of ways to be more efficient with everyday tasks in a shell.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.dwmkerr.com/effective-shell-part-1-navigating-the-command-line/"&gt;Part 1: Navigating the Command Line&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.dwmkerr.com/effective-shell-part-2-become-a-clipboard-gymnast/"&gt;Part 2: Become a Clipboard Gymnast&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;a href="https://www.dwmkerr.com/effective-shell-part-3-getting-hepl/"&gt;Part 3: Getting Help&lt;/a&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dwmkerr.com/effective-shell-4-moving-around/"&gt;Part 4: Moving Around&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dwmkerr.com/effective-shell-part-5-understanding-the-shell/"&gt;Part 5: Interlude - Understanding the Shell&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dwmkerr.com/effective-shell-6-job-control/"&gt;Part 6: Everything You Don&amp;rsquo;t Need to Know About Job Control&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dwmkerr.com/effective-shell-7-shell-commands/"&gt;Part 7: The Subtleties of Shell Commands&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;In this article I&amp;rsquo;ll show you how to quickly get help when working with tools in the shell, without disrupting your flow!&lt;/p&gt;
&lt;h2 id="getting-help-is-important"&gt;Getting Help is Important!&lt;/h2&gt;
&lt;p&gt;If you are trying to be more effective when using the shell, it is crucial to know how to quickly look things up.&lt;/p&gt;
&lt;p&gt;There&amp;rsquo;ll be many circumstances where you&amp;rsquo;ll need to open a browser to search for help, but there&amp;rsquo;s also a wealth of information only a few keystrokes away. Looking up parameters, checking how to run commads, C library docs or useful information like ASCII charts are available directly in the system.&lt;/p&gt;
&lt;p&gt;Before we look at the standard way of accessing documentation on unix-like systems, which is the &lt;code&gt;man&lt;/code&gt; command, I&amp;rsquo;m going to introduce &lt;a href="https://github.com/tldr-pages/tldr"&gt;&lt;code&gt;tldr&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Nine times out of ten I get the help I need in a few seconds with &lt;code&gt;tldr&lt;/code&gt;, so if you take only one thing away from the article, take the first section. Then if you want to learn more about the system manuals, read on!&lt;/p&gt;
&lt;h2 id="tldr"&gt;tl;dr&lt;/h2&gt;
&lt;p&gt;Let&amp;rsquo;s say I need to find and replace some text in a file. I know I can do this with the &lt;code&gt;sed&lt;/code&gt; command, but have forgotten the syntax.&lt;/p&gt;
&lt;p&gt;All I need to do is run &lt;code&gt;tldr sed&lt;/code&gt;:&lt;/p&gt;
&lt;p&gt;&lt;img src="images/tldr-sed.png" alt="tldr sed screenshot"&gt;&lt;/p&gt;
&lt;p&gt;The first example is exactly what I&amp;rsquo;m looking for. Now for any more detail than a few basic examples, I&amp;rsquo;m going to have to go to the manual, but it&amp;rsquo;s overkill for the basics. Here&amp;rsquo;s what &lt;code&gt;man sed&lt;/code&gt; shows me:&lt;/p&gt;
&lt;p&gt;&lt;img src="images/man-sed.png" alt="sed manpage"&gt;&lt;/p&gt;
&lt;p&gt;And this is just page one of six! There&amp;rsquo;s a &lt;em&gt;lot&lt;/em&gt; of detail, which is great sometimes, but for a quick lookup, &lt;code&gt;tldr&lt;/code&gt; is perfect.&lt;/p&gt;
&lt;p&gt;You can install the &lt;a href="https://github.com/tldr-pages/tldr"&gt;&lt;code&gt;tldr&lt;/code&gt;&lt;/a&gt; tool with &lt;code&gt;npm install -g tldr&lt;/code&gt;. It&amp;rsquo;s open source and community maintained.&lt;/p&gt;
&lt;p&gt;Now a lot of the time, you are still going to need more help or more detail. For the rest of the article, we&amp;rsquo;ll dive a bit deeper into &lt;code&gt;man&lt;/code&gt;, the system manual pages.&lt;/p&gt;
&lt;h2 id="understanding-man"&gt;Understanding &amp;lsquo;man&amp;rsquo;&lt;/h2&gt;
&lt;p&gt;Most tools you encounter in the shell have manual pages available. Many people will be familiar with the &lt;code&gt;man&lt;/code&gt; command to get help on a tool, but let&amp;rsquo;s take a look in a bit more detail, there&amp;rsquo;s actually a lot more available than just the documentation for common commands.&lt;/p&gt;
&lt;h3 id="getting-help-on-a-command"&gt;Getting help on a command&lt;/h3&gt;
&lt;p&gt;The most basic way to get help on a command is with &lt;code&gt;man&lt;/code&gt;. Here&amp;rsquo;s an example:&lt;/p&gt;
&lt;pre tabindex="0"&gt;&lt;code&gt;$ man cp
CP(1) BSD General Commands Manual CP(1)
NAME
cp -- copy files
SYNOPSIS
cp [-R [-H | -L | -P]] [-fi | -n] [-apvX] source_file target_file
cp [-R [-H | -L | -P]] [-fi | -n] [-apvX] source_file ...
target_directory
DESCRIPTION
In the first synopsis form, the cp utility copies the contents of the
source_file to the target_file. In the second synopsis form, the con-
tents of each named source_file is copied to the destination
target_directory. The names of the files themselves are not changed. If
cp detects an attempt to copy a file to itself, the copy will fail.
...
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The &lt;code&gt;man&lt;/code&gt; command opens the manual for the given tool. These manuals should contain all command line options and details of how to use the tool.&lt;/p&gt;
&lt;p&gt;You can scroll up and down through the content with the arrow keys, this is because the information is presented in the shell &lt;em&gt;pager&lt;/em&gt;, which is a tool for looking through content which might not easily fit on a screen.&lt;/p&gt;
&lt;h3 id="using-the-pager"&gt;Using the pager&lt;/h3&gt;
&lt;p&gt;The first thing you might notice is that you can move through the manual pages with the arrow keys.&lt;/p&gt;
&lt;p&gt;Manpages are just text files, and &lt;code&gt;man&lt;/code&gt; opens them in a pager tool, which is what is providing the keyboard interface to look through the file.&lt;/p&gt;
&lt;p&gt;On most systems, the pager will be the &lt;code&gt;less&lt;/code&gt; program. There are lots of commands you can use to navigate through files with &lt;code&gt;less&lt;/code&gt;, but the bare essentials are:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;d&lt;/code&gt; - Scroll down half a page&lt;/li&gt;
&lt;li&gt;&lt;code&gt;u&lt;/code&gt; - Scroll up half a page&lt;/li&gt;
&lt;li&gt;&lt;code&gt;j&lt;/code&gt; / &lt;code&gt;k&lt;/code&gt; - Scroll down or up a line. You can also use the arrow keys for this&lt;/li&gt;
&lt;li&gt;&lt;code&gt;q&lt;/code&gt; - Quit&lt;/li&gt;
&lt;li&gt;&lt;code&gt;/&amp;lt;search&amp;gt;&lt;/code&gt; - Search for text&lt;/li&gt;
&lt;li&gt;&lt;code&gt;n&lt;/code&gt; - When searching, find the next occurrence&lt;/li&gt;
&lt;li&gt;&lt;code&gt;N&lt;/code&gt; - When searching, find the previous occurrence&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;There are &lt;em&gt;many&lt;/em&gt; other commands, but the set above is normally what I find myself using the most.&lt;/p&gt;
&lt;p&gt;If you are interested, you can actually see what your pager is with the command below:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-sh" data-lang="sh"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;$ echo $PAGER
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;less
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The &lt;code&gt;$PAGER&lt;/code&gt; environment variable is used to tell the shell what program to use for paging. More details are found with &lt;code&gt;man man&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;You can put any text content into your pager - try this:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-sh" data-lang="sh"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;ls -al /usr/bin | less
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This lists the contents of the &lt;code&gt;/usr/bin&lt;/code&gt; folder, piping the output to &lt;code&gt;less&lt;/code&gt; so we can easily scroll through it.&lt;/p&gt;
&lt;p&gt;There are alternative pagers available (on many Unix-y systems you&amp;rsquo;ll have &lt;code&gt;less&lt;/code&gt;, &lt;code&gt;more&lt;/code&gt; and &lt;code&gt;most&lt;/code&gt;) but in general you&amp;rsquo;ll normally get what you need with &lt;code&gt;less&lt;/code&gt;.&lt;/p&gt;
&lt;h3 id="whats-with-the-numbers"&gt;What&amp;rsquo;s with the numbers?&lt;/h3&gt;
&lt;p&gt;You&amp;rsquo;ll often see tools referred to in manpages with numbers after them. Take a look at &lt;code&gt;man less&lt;/code&gt;:&lt;/p&gt;
&lt;p&gt;&lt;img src="images/numbers.png" alt="Screenshot of numbers"&gt;&lt;/p&gt;
&lt;p&gt;The number is the manual &lt;strong&gt;Section Number&lt;/strong&gt;. The different sections of the manual are documented be found on most unix-like systems in &lt;code&gt;man&lt;/code&gt;&amp;rsquo;s documentation, which you can check by running &lt;code&gt;man man&lt;/code&gt;&lt;sup id="fnref:1"&gt;&lt;a href="#fn:1" class="footnote-ref" role="doc-noteref"&gt;1&lt;/a&gt;&lt;/sup&gt;. Here&amp;rsquo;s what you&amp;rsquo;d get on Ubuntu 16:&lt;/p&gt;
&lt;p&gt;| 1 | Executable programs or shell commands |
| 2 | System calls (functions provided by the kernel) |
| 3 | Library calls (functions within program libraries) |
| 4 | Special files (usually found in /dev) |
| 5 | File formats and conventions eg /etc/passwd |
| 6 | Games |
| 7 | Miscellaneous (including macro packages and conventions), e.g. man(7), groff(7) |
| 8 | System administration commands (usually only for root) |
| 9 | Kernel routines [Non standard] |&lt;/p&gt;
&lt;p&gt;We&amp;rsquo;ll go through the setions in detail shorltly.&lt;/p&gt;
&lt;p&gt;You can specifically choose &lt;em&gt;which&lt;/em&gt; section of the manual you are looking in by using:&lt;/p&gt;
&lt;pre tabindex="0"&gt;&lt;code&gt;man &amp;lt;section&amp;gt; &amp;lt;search&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;You can also get more information about the sections themselves by opening up the &lt;code&gt;intro&lt;/code&gt; page. For example:&lt;/p&gt;
&lt;pre tabindex="0"&gt;&lt;code&gt;$ man 1 intro
INTRO(1) BSD General Commands Manual INTRO(1)
NAME
intro -- introduction to general commands (tools and utilities)
DESCRIPTION
Section one of the manual contains most of the commands which comprise...
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Why would you do this, and why would you care? A few examples from each section show how this can be quite useful to know about.&lt;/p&gt;
&lt;h4 id="section-1-programs-and-shell-commands"&gt;Section 1: Programs and Shell Commands&lt;/h4&gt;
&lt;p&gt;These are programs, probably what you are going to be looking up most regularly! For example, &lt;code&gt;man 1 time&lt;/code&gt; shows:&lt;/p&gt;
&lt;pre tabindex="0"&gt;&lt;code&gt;TIME(1) BSD General Commands Manual TIME(1)
NAME
time -- time command execution
SYNOPSIS
time [-lp] utility
DESCRIPTION
The time utility executes and times utility. After the utility finishes, time writes the total time
elapsed, the time consumed by system overhead, and the time used to execute utility to the standard
error stream. Times are reported in seconds.
...
&lt;/code&gt;&lt;/pre&gt;&lt;h4 id="section-2-system-calls"&gt;Section 2: System Calls&lt;/h4&gt;
&lt;p&gt;You&amp;rsquo;ll probably not use this section unless you are doing systems programming&lt;sup id="fnref:2"&gt;&lt;a href="#fn:2" class="footnote-ref" role="doc-noteref"&gt;2&lt;/a&gt;&lt;/sup&gt;. This section contains info on the avaiable Linux Kernel system calls. For example, running &lt;code&gt;man 2 chown&lt;/code&gt; gives:&lt;/p&gt;
&lt;pre tabindex="0"&gt;&lt;code&gt;CHOWN(2) BSD System Calls Manual CHOWN(2)
NAME
chown, fchown, lchown, fchownat -- change owner and group of a file
SYNOPSIS
#include &amp;lt;unistd.h&amp;gt;
int
chown(const char *path, uid_t owner, gid_t group);
...
&lt;/code&gt;&lt;/pre&gt;&lt;h4 id="section-3-library-calls"&gt;Section 3: Library Calls&lt;/h4&gt;
&lt;p&gt;These are the manpages for the C standard library functions. For example, &lt;code&gt;man 3 time&lt;/code&gt;:&lt;/p&gt;
&lt;pre tabindex="0"&gt;&lt;code&gt;TIME(3) BSD Library Functions Manual TIME(3)
NAME
time -- get time of day
LIBRARY
Standard C Library (libc, -lc)
SYNOPSIS
#include &amp;lt;time.h&amp;gt;
time_t
time(time_t *tloc);
...
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Here we can see why the sections are important to know about.&lt;/p&gt;
&lt;p&gt;Running &lt;code&gt;man time&lt;/code&gt; would &lt;em&gt;not&lt;/em&gt; open the page above, because &lt;code&gt;man&lt;/code&gt; searches the library in ascending section order, meaning that it actually finds &lt;code&gt;time(1)&lt;/code&gt; and shows the pages for the &lt;code&gt;time&lt;/code&gt; program, not the &lt;code&gt;time&lt;/code&gt; C library call.&lt;/p&gt;
&lt;p&gt;Because of the potential ambiguity of names if no section number is included, in lots of Linux documentation you&amp;rsquo;ll see the man section number written next to library calls, system calls, programs and so on (things will refer to &lt;code&gt;sed(1)&lt;/code&gt; or &lt;code&gt;time(3)&lt;/code&gt; for example.&lt;/p&gt;
&lt;h4 id="section-4-devices"&gt;Section 4: Devices&lt;/h4&gt;
&lt;p&gt;This section deals with the special devices which live in the &lt;code&gt;/dev/*&lt;/code&gt; folder. For example, running &lt;code&gt;man 4 random&lt;/code&gt; shows:&lt;/p&gt;
&lt;pre tabindex="0"&gt;&lt;code&gt;RANDOM(4) BSD Kernel Interfaces Manual RANDOM(4)
NAME
random , urandom -- random data source devices.
SYNOPSIS
pseudo-device random
DESCRIPTION
The random device produces uniformly distributed random byte values of
potentially high quality.
...
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Again, we see that section numbers can be important. If you just run &lt;code&gt;man random&lt;/code&gt;, you&amp;rsquo;ll see:&lt;/p&gt;
&lt;pre tabindex="0"&gt;&lt;code&gt;RANDOM(3) BSD Library Functions Manual RANDOM(3)
NAME
initstate, random, setstate, srandom, srandomdev -- better random num-
ber generator; routines for changing generators
LIBRARY
Standard C Library (libc, -lc)
SYNOPSIS
#include &amp;lt;stdlib.h&amp;gt;
char *
initstate(unsigned seed, char *state, size_t size);
long
random(void);
...
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Which is the manpage for &lt;code&gt;random(3)&lt;/code&gt;, which is C library function, not the &lt;code&gt;/dev/random&lt;/code&gt; file!&lt;/p&gt;
&lt;h4 id="section-5-file-formats"&gt;Section 5: File Formats&lt;/h4&gt;
&lt;p&gt;This section details special files in the system. For example, &lt;code&gt;man 5 crontab&lt;/code&gt; shows:&lt;/p&gt;
&lt;pre tabindex="0"&gt;&lt;code&gt;CRONTAB(5) BSD File Formats Manual CRONTAB(5)
NAME
crontab -- tables for driving cron
DESCRIPTION
A crontab file contains instructions to the cron(8) daemon of the gen-
eral form: ``run this command at this time on this date&amp;#39;&amp;#39;. Each user
has their own crontab, and commands in any given crontab will be exe-
cuted as the user who owns the crontab. Uucp and News will usually
have their own crontabs, eliminating the need for explicitly running
su(1) as part of a cron command.
...
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Which describes the crontab file used to define scheduled tasks. Again, this is different to &lt;code&gt;man crontab&lt;/code&gt; which would document &lt;code&gt;crontab(1)&lt;/code&gt;. Similarly, &lt;code&gt;man 5 passwd&lt;/code&gt; is going to show something quite different to &lt;code&gt;man passwd&lt;/code&gt;.&lt;/p&gt;
&lt;h4 id="section-6-games"&gt;Section 6: Games&lt;/h4&gt;
&lt;p&gt;Nothing says it better than &lt;code&gt;man 6 intro&lt;/code&gt; itself (this&amp;rsquo;ll not work on a Mac sadly, but try it on another Linux system):&lt;/p&gt;
&lt;pre tabindex="0"&gt;&lt;code&gt;...
DESCRIPTION
Section 6 of the manual describes all the games and funny little programs available on the system.
...
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;There are probably a few silly programs available on your system, here you&amp;rsquo;ll find their manuals. For example, &lt;code&gt;man 6 banner&lt;/code&gt; on a Mac shows:&lt;/p&gt;
&lt;pre tabindex="0"&gt;&lt;code&gt;BANNER(6) BSD Games Manual BANNER(6)
NAME
banner -- print large banner on printer
SYNOPSIS
banner [-d] [-t] [-w width] message ...
DESCRIPTION
Banner prints a large, high quality banner on the standard output. If
the message is omitted, it prompts for and reads one line of its stan-
dard input.
...
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This section is going to be highly dependent on your OS!&lt;/p&gt;
&lt;h4 id="section-7-miscellaneous"&gt;Section 7: Miscellaneous&lt;/h4&gt;
&lt;p&gt;This is where you&amp;rsquo;ll find additional assorted documentation. For example, &lt;code&gt;man 7 ascii&lt;/code&gt; shows:&lt;/p&gt;
&lt;pre tabindex="0"&gt;&lt;code&gt;ASCII(7) BSD Miscellaneous Information Manual ASCII(7)
NAME
ascii -- octal, hexadecimal and decimal ASCII character sets
DESCRIPTION
The octal set:
000 nul 001 soh 002 stx 003 etx 004 eot 005 enq 006 ack 007 bel
...
&lt;/code&gt;&lt;/pre&gt;&lt;h4 id="section-8-system-commands"&gt;Section 8: System Commands&lt;/h4&gt;
&lt;p&gt;We&amp;rsquo;ve actually already seen one of these commands mentioned, in the manpage for &lt;code&gt;crontab(5)&lt;/code&gt; it mentions &lt;code&gt;cron(8)&lt;/code&gt;. Let&amp;rsquo;s see, with &lt;code&gt;man 8 cron&lt;/code&gt;:&lt;/p&gt;
&lt;pre tabindex="0"&gt;&lt;code&gt;CRON(8) BSD System Manager&amp;#39;s Manual CRON(8)
NAME
cron -- daemon to execute scheduled commands (Vixie Cron)
SYNOPSIS
cron [-s] [-o] [-x debugflag[,...]]
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;These are commands which sysadmins would normally run. You might open section eight unexpectedly, for example &lt;code&gt;man chmod&lt;/code&gt; will open &lt;code&gt;chmod(1)&lt;/code&gt;, but &lt;code&gt;man chown&lt;/code&gt; will open &lt;code&gt;chown(8)&lt;/code&gt;, as it is a system command.&lt;/p&gt;
&lt;p&gt;Some distributions might vary for Section Nine. On my Mac it contains information about the kernel interfaces, a C style guide and some more.&lt;/p&gt;
&lt;h4 id="getting-the-index-of-manual-section"&gt;Getting the Index of Manual Section&lt;/h4&gt;
&lt;p&gt;Manpages are just files on the filesystem, so you can get the index of a section just by looking in the appropriate folder.&lt;/p&gt;
&lt;p&gt;For example, to index the available system calls, try &lt;code&gt;ls /usr/share/man/man2&lt;/code&gt;:&lt;/p&gt;
&lt;pre tabindex="0"&gt;&lt;code&gt;EV_SET.2
FD_CLR.2
FD_COPY.2
FD_ISSET.2
FD_SET.2
FD_ZERO.2
_exit.2
accept.2
access.2
acct.2
...
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This is quick and easy way to see what sort of entries you have on your system. If you want to work out where an entry lives, use the &lt;code&gt;-w&lt;/code&gt; flag:&lt;/p&gt;
&lt;pre tabindex="0"&gt;&lt;code&gt;$ man -w printf
/usr/share/man/man1/printf.1
&lt;/code&gt;&lt;/pre&gt;&lt;h3 id="searching-the-manual"&gt;Searching the Manual&lt;/h3&gt;
&lt;p&gt;You can search the manpage titles and summaries with &lt;code&gt;man -k&lt;/code&gt;. For example, &lt;code&gt;man -k cpu&lt;/code&gt; shows:&lt;/p&gt;
&lt;pre tabindex="0"&gt;&lt;code&gt;cpuwalk.d(1m) - Measure which CPUs a process runs on. Uses DTrace
dispqlen.d(1m) - dispatcher queue length by CPU. Uses DTrace
gasm(n), grammar::me::cpu::gasm(n) - ME assembler
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;You can find more advanced options for searching by using your newfound &lt;code&gt;man&lt;/code&gt; skills on &lt;code&gt;man&lt;/code&gt; itself.&lt;/p&gt;
&lt;h2 id="thats-enough"&gt;That&amp;rsquo;s Enough!&lt;/h2&gt;
&lt;p&gt;I&amp;rsquo;d recommend &lt;code&gt;tldr&lt;/code&gt; as a first-call for checking to see how to use a command.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;man&lt;/code&gt; is a powerful tool to dive deeper into how programs and components of the system work. Like many tools which have been around for a long time, there&amp;rsquo;s a lot you can do with &lt;code&gt;man&lt;/code&gt;. Much of it you&amp;rsquo;ll likely never need, so I&amp;rsquo;ve tried to keep this article to the basics.&lt;/p&gt;
&lt;p&gt;Understanding manpage sections is useful - you&amp;rsquo;ll see them referenced again and again in documentation on the system and online.&lt;/p&gt;
&lt;p&gt;I hope this helps you save some time when you are working! Please let me know in the comments if you have any questions or thoughts.&lt;/p&gt;
&lt;p&gt;You can also check out the &lt;a href="https://github.com/dwmkerr/effective-shell"&gt;rest of the effective shell series&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id="appendix-dash"&gt;Appendix: Dash&lt;/h2&gt;
&lt;p&gt;As a final note, if you find yourself using &lt;code&gt;man&lt;/code&gt; a lot because you work offline (I fly a lot so find it very helpful when on a plane with no WiFi), you should also look at &lt;em&gt;Dash&lt;/em&gt;&lt;sup id="fnref:3"&gt;&lt;a href="#fn:3" class="footnote-ref" role="doc-noteref"&gt;3&lt;/a&gt;&lt;/sup&gt;.&lt;/p&gt;
&lt;p&gt;Dash is simply an offline documentation aggregator. It can download online manuals for many, many different programming languages, frameworks, technologies and so on. I actually have a &lt;code&gt;vim&lt;/code&gt; keyboard command to open the word under the cursor in dash, with the documentation automatically set based on the type of the file.&lt;/p&gt;
&lt;p&gt;This is super-useful if you are offline at lot and need more sophisticated offline documentation. You can find out more about it at &lt;a href="https://kapeli.com/dash"&gt;https://kapeli.com/dash&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id="footnotes"&gt;Footnotes&lt;/h2&gt;
&lt;div class="footnotes" role="doc-endnotes"&gt;
&lt;hr&gt;
&lt;ol&gt;
&lt;li id="fn:1"&gt;
&lt;p&gt;Weirdly satisfying to run.&amp;#160;&lt;a href="#fnref:1" class="footnote-backref" role="doc-backlink"&gt;&amp;#x21a9;&amp;#xfe0e;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id="fn:2"&gt;
&lt;p&gt;Which it is always fun to try if you get the chance, and a great way to learn more about the fundamentals of the operating system.&amp;#160;&lt;a href="#fnref:2" class="footnote-backref" role="doc-backlink"&gt;&amp;#x21a9;&amp;#xfe0e;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id="fn:3"&gt;
&lt;p&gt;Dash is a paid product. Full disclosure - I don&amp;rsquo;t get any money from them or anyone else to write about anything, all content is 100% based on my experiences. I don&amp;rsquo;t run ads on my site either.&amp;#160;&lt;a href="#fnref:3" class="footnote-backref" role="doc-backlink"&gt;&amp;#x21a9;&amp;#xfe0e;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;</description><category>CodeProject</category></item><item><title>Effective Shell Part 2: Become a Clipboard Gymnast</title><link>https://dwmkerr.com/effective-shell-part-2-become-a-clipboard-gymnast/</link><pubDate>Tue, 10 Oct 2017 09:57:54 +0000</pubDate><guid>https://dwmkerr.com/effective-shell-part-2-become-a-clipboard-gymnast/</guid><description>&lt;p&gt;This is the second part of my &lt;a href="https://github.com/dwmkerr/effective-shell"&gt;Effective Shell&lt;/a&gt; series, which contains practical tips for using the shell to help with every day tasks and be more efficient:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.dwmkerr.com/effective-shell-part-1-navigating-the-command-line/"&gt;Part 1: Navigating the Command Line&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;a href="https://www.dwmkerr.com/effective-shell-part-2-become-a-clipboard-gymnast/"&gt;Part 2: Become a Clipboard Gymnast&lt;/a&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.dwmkerr.com/effective-shell-part-3-getting-hepl/"&gt;Part 3: Getting Help&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dwmkerr.com/effective-shell-4-moving-around/"&gt;Part 4: Moving Around&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dwmkerr.com/effective-shell-part-5-understanding-the-shell/"&gt;Part 5: Interlude - Understanding the Shell&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dwmkerr.com/effective-shell-6-job-control/"&gt;Part 6: Everything You Don&amp;rsquo;t Need to Know About Job Control&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dwmkerr.com/effective-shell-7-shell-commands/"&gt;Part 7: The Subtleties of Shell Commands&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;In this article I&amp;rsquo;ll show you how you can use the shell as an efficient tool to compliment how you use the clipboard.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Note for Linux Users: In this article I&amp;rsquo;ll use the &lt;code&gt;pbcopy&lt;/code&gt; and &lt;code&gt;pbpaste&lt;/code&gt; commands to access the clipboard, which are available on a Mac only. To get access to the same commands on other platforms, check &lt;a href="#appendixclipboardaccessonlinux"&gt;Appendix: Clipboard Access on Linux&lt;/a&gt;&lt;/em&gt;.&lt;/p&gt;
&lt;h2 id="use-the-shell-on-the-clipboard"&gt;Use the Shell on the Clipboard&lt;/h2&gt;
&lt;p&gt;You can easily use shell commands on the contents of your clipboard. Just use &lt;code&gt;pbpaste&lt;/code&gt; to output the clipboard, run the output through some commands, then use &lt;code&gt;pbcopy&lt;/code&gt; to copy the result.&lt;/p&gt;
&lt;p&gt;Try copying the following text:&lt;/p&gt;
&lt;pre tabindex="0"&gt;&lt;code&gt;Kirk Van Houten
Timothy Lovejoy
Artie Ziff
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Then in the shell, run:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;pbpaste
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;You should see the contents of the clipboard. Now we&amp;rsquo;ll look at some ways that shell access to the clipboard can help with common tasks.&lt;/p&gt;
&lt;h2 id="removing-formatting"&gt;Removing Formatting&lt;/h2&gt;
&lt;p&gt;Don&amp;rsquo;t you hate it when you have to copy formatted text and don&amp;rsquo;t have an easy way to paste it as &lt;em&gt;unformatted&lt;/em&gt; text? Here&amp;rsquo;s an example, I want to copy this Wikipedia page on &amp;lsquo;bash&amp;rsquo;, and paste it into a Word document:&lt;/p&gt;
&lt;p&gt;&lt;img src="images/strip-formatting-before.png" alt="Copying and pasting with formatting"&gt;&lt;/p&gt;
&lt;p&gt;Many programs have a shortcut to paste the contents of the clipboard (such as &amp;lsquo;command + shift + v&amp;rsquo;) but if you are like me you might find yourself pasting &lt;em&gt;into&lt;/em&gt; a plain text editor just to copy &lt;em&gt;out&lt;/em&gt; the plain text.&lt;/p&gt;
&lt;p&gt;If you just run the command &lt;code&gt;pbpaste | pbcopy&lt;/code&gt;, you can easily strip the formatting:&lt;/p&gt;
&lt;p&gt;&lt;img src="images/strip-formatting-after-2.png" alt="Stripping formatting from the clipboard"&gt;&lt;/p&gt;
&lt;p&gt;We&amp;rsquo;re just piping out the clipboard (which ends up as plain text, cause we&amp;rsquo;re in a terminal!) and then piping that plain text &lt;em&gt;back into the clipboard&lt;/em&gt;, replacing the formatted text which was there before.&lt;/p&gt;
&lt;p&gt;This little trick can be very useful. But we can use the same pattern to quickly manipulate the contents of the clipboard in more sophisticated ways.&lt;/p&gt;
&lt;h2 id="manipulating-text"&gt;Manipulating Text&lt;/h2&gt;
&lt;p&gt;Let&amp;rsquo;s say someone has emailed me a list of people I need to invite to an event:&lt;/p&gt;
&lt;p&gt;&lt;img src="images/email_list_excel.png" alt="Email List"&gt;&lt;/p&gt;
&lt;p&gt;The problem is:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;The list is in Excel and is formatted&lt;/li&gt;
&lt;li&gt;The list has duplicates&lt;/li&gt;
&lt;li&gt;I need to turn each name into an email address like &amp;lsquo;&lt;a href="mailto:Artie_Ziff@simpsons.com"&gt;Artie_Ziff@simpsons.com&lt;/a&gt;&amp;rsquo;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;And I want to email everyone quickly.&lt;/p&gt;
&lt;p&gt;We can quickly handle this task without leaving the shell.&lt;/p&gt;
&lt;p&gt;Copy the raw text below if you want to try out the same commands and follow along:&lt;/p&gt;
&lt;pre tabindex="0"&gt;&lt;code&gt;Artie Ziff
Kirk Van Houten
Timothy Lovejoy
Artie Ziff
Nick Riviera
Seymore Skinner
Hank Scorpio
Timothy Lovejoy
John Frink
Cletus Spuckler
Ruth Powers
Artie Ziff
Agnes Skinner
Helen Lovejoy
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;First, we copy the text to the clipboard.&lt;/p&gt;
&lt;p&gt;Now we can paste and sort:&lt;/p&gt;
&lt;pre tabindex="0"&gt;&lt;code&gt;$ pbpaste | sort
Agnes Skinner
Artie Ziff
Artie Ziff
Artie Ziff
Cletus Spuckler
Hank Scorpio
Helen Lovejoy
John Frink
Kirk Van Houten
Nick Riviera
Ruth Powers
Seymore Skinner
Timothy Lovejoy
Timothy Lovejoy
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Then remove the duplicates:&lt;/p&gt;
&lt;pre tabindex="0"&gt;&lt;code&gt;$ pbpaste | sort | uniq
Agnes Skinner
Artie Ziff
Cletus Spuckler
Hank Scorpio
Helen Lovejoy
John Frink
Kirk Van Houten
Nick Riviera
Ruth Powers
Seymore Skinner
Timothy Lovejoy
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Replace the underscore with an ampersand:&lt;/p&gt;
&lt;pre tabindex="0"&gt;&lt;code&gt;$ pbpaste | sort | uniq | tr &amp;#34; &amp;#34; &amp;#34;_&amp;#34;
Agnes_Skinner
Artie_Ziff
Cletus_Spuckler
Hank_Scorpio
Helen_Lovejoy
John_Frink
Kirk_Van_Houten
Nick_Riviera
Ruth_Powers
Seymore_Skinner
Timothy_Lovejoy
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Then add the final part of the email address:&lt;/p&gt;
&lt;pre tabindex="0"&gt;&lt;code&gt;$ pbpaste | sort | uniq | tr &amp;#34; &amp;#34; &amp;#34;_&amp;#34; | sed &amp;#39;s/$/@simpsons.com/&amp;#39;
Agnes_Skinner@simpsons.com
Artie_Ziff@simpsons.com
Cletus_Spuckler@simpsons.com
Hank_Scorpio@simpsons.com
Helen_Lovejoy@simpsons.com
John_Frink@simpsons.com
Kirk_Van_Houten@simpsons.com
Nick_Riviera@simpsons.com
Ruth_Powers@simpsons.com
Seymore_Skinner@simpsons.com
Timothy_Lovejoy@simpsons.com
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This looks perfect! We can now put the transformed text back onto the clipboard:&lt;/p&gt;
&lt;pre tabindex="0"&gt;&lt;code&gt;$ pbpaste | sort | uniq | tr &amp;#39; &amp;#39; &amp;#39;_&amp;#39; | sed &amp;#39;s/$/@simpsons.com&amp;#39; | pbcopy
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;All in all we have the following pipeline:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;code&gt;pbpaste&lt;/code&gt; - output the clipboard&lt;/li&gt;
&lt;li&gt;&lt;code&gt;sort&lt;/code&gt; - order the output&lt;/li&gt;
&lt;li&gt;&lt;code&gt;uniq&lt;/code&gt; - deduplicate the rows&lt;/li&gt;
&lt;li&gt;&lt;code&gt;tr ' ' '_'&lt;/code&gt; - replace spaces with underscores&lt;/li&gt;
&lt;li&gt;&lt;code&gt;sed /$/@simpsons.com&lt;/code&gt; - add the email domain to the end of the row&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Building this in one go is hard, let&amp;rsquo;s look at little more at the pipeline.&lt;/p&gt;
&lt;h2 id="thinking-in-pipelines"&gt;Thinking in Pipelines&lt;/h2&gt;
&lt;p&gt;Some of these commands might be unfamiliar, some might not make sense, and you might be thinking &amp;lsquo;how would I remember that&amp;rsquo;. Actually, there are many ways to solve the problem above, this is the one I came up with by &lt;em&gt;iteratively&lt;/em&gt; changing my input text.&lt;/p&gt;
&lt;p&gt;Here&amp;rsquo;s what I mean - you&amp;rsquo;ll see that I actually build a pipeline like this step-by-step:&lt;/p&gt;
&lt;p&gt;&lt;img src="images/pipeline.gif" alt="Animation of the process of building a pipeline"&gt;&lt;/p&gt;
&lt;p&gt;You can see in the screenshots that I start simple, and step by step add the stages we need.&lt;/p&gt;
&lt;p&gt;(P.S - if you are wondering how I am jumping backwards and forwards a word at a time, check the last chapter &amp;lsquo;&lt;a href="www.dwmkerr.com/effective-shell-part-1-navigating-the-command-line/"&gt;Navigating the Command Line&lt;/a&gt;&amp;rsquo;).&lt;/p&gt;
&lt;p&gt;What we&amp;rsquo;re doing here is only possible because these simple commands all follow &amp;rsquo;the Unix Philosophy&amp;rsquo;. They do one thing well, and each command expects it&amp;rsquo;s input to become the input of &lt;em&gt;another&lt;/em&gt; command later on. Specifically:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;The commands are primitive and simple - &lt;code&gt;sort&lt;/code&gt; is sorting a list, &lt;code&gt;uniq&lt;/code&gt; is making elements unique.&lt;/li&gt;
&lt;li&gt;The commands don&amp;rsquo;t produce unnecessary output - &lt;code&gt;sort&lt;/code&gt; doesn&amp;rsquo;t add a header such as &lt;code&gt;Sorted Items&lt;/code&gt;, which is great because otherwise it would clutter our pipeline.&lt;/li&gt;
&lt;li&gt;We are chaining commands together, the output of one becomes the input of another.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;We don&amp;rsquo;t need a command such as &amp;lsquo;Take a muddy list, sort and clean it, then turn pairs of words into an email address&amp;rsquo; - with a few simple &amp;lsquo;workhorse&amp;rsquo; commands we can easily build this functionality ourselves.&lt;/p&gt;
&lt;p&gt;These workhorse commands will be introduced and detailed as we go through the series. We&amp;rsquo;ll also spend a lot more time looking at pipelines.&lt;/p&gt;
&lt;p&gt;I hope this was useful! Please comment if you have any questions or tips. To see further articles as they come out, follow the repo at:&lt;/p&gt;
&lt;p&gt;&lt;a href="https://github.com/dwmkerr/effective-shell"&gt;github.com/dwmkerr/effective-shell&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Or just follow &lt;a href="https://twitter.com/dwmkerr"&gt;@dwmkerr&lt;/a&gt; on Twitter.&lt;/p&gt;
&lt;h1 id="appendix---clipboard-access-on-linux"&gt;Appendix - Clipboard Access on Linux&lt;/h1&gt;
&lt;p&gt;If you are using Linux, there is no &lt;code&gt;pbcopy&lt;/code&gt; and &lt;code&gt;pbpaste&lt;/code&gt; commands. You can use the &lt;a href="https://linux.die.net/man/1/xclip"&gt;&lt;code&gt;xclip&lt;/code&gt;&lt;/a&gt; tool to create equivalent commands.&lt;/p&gt;
&lt;p&gt;First, install &lt;code&gt;xclip&lt;/code&gt;:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;sudo apt-get install -y xclip
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Then add the following to your &lt;code&gt;.bashrc&lt;/code&gt; file:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;# Create mac style aliases for clipboard access.&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;alias pbcopy&lt;span style="color:#f92672"&gt;=&lt;/span&gt;&lt;span style="color:#e6db74"&gt;&amp;#34;xclip -selection c&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;alias pbpaste&lt;span style="color:#f92672"&gt;=&lt;/span&gt;&lt;span style="color:#e6db74"&gt;&amp;#34;xclip -selection c -o&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Obviously you can use any alias you like! The article assumes that &lt;code&gt;pbcopy&lt;/code&gt; and &lt;code&gt;pbpaste&lt;/code&gt; have been used.&lt;/p&gt;</description><category>CodeProject</category></item></channel></rss>