<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Shell Life - Medium]]></title>
        <description><![CDATA[Doing everything we can in the command line. - Medium]]></description>
        <link>https://medium.com/shell-life?source=rss----613b0939c201---4</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>Shell Life - Medium</title>
            <link>https://medium.com/shell-life?source=rss----613b0939c201---4</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 12:01:04 GMT</lastBuildDate>
        <atom:link href="https://medium.com/feed/shell-life" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Convert images for web using the Sips command line on MacOSX]]></title>
            <link>https://medium.com/shell-life/convert-images-for-web-using-the-sips-command-line-on-macosx-656c502a67a6?source=rss----613b0939c201---4</link>
            <guid isPermaLink="false">https://medium.com/p/656c502a67a6</guid>
            <category><![CDATA[command-line]]></category>
            <category><![CDATA[apple]]></category>
            <category><![CDATA[applescript]]></category>
            <category><![CDATA[mac]]></category>
            <dc:creator><![CDATA[Mickaël Riga]]></dc:creator>
            <pubDate>Tue, 22 Sep 2020 09:26:45 GMT</pubDate>
            <atom:updated>2020-09-21T12:47:39.444Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*3NWiR82e1utYViDV" /><figcaption>Photo by <a href="https://unsplash.com/@brisch27?utm_source=medium&amp;utm_medium=referral">Brigitta Schneiter</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>We will see how to convert a folder of images to make them safe for web using the Mac OSX command line tool Sips. First in a shell script and then an AppleScript version if you want to share this functionality with less technical users.</p><p>There are obviously many ways to convert images for web. Some of them are tedious if you have a whole folder to convert. In this case I would generally use <a href="https://imagemagick.org/">ImageMagick</a>. I am quite familiar with it because I do pretty much any scripted image processing with it. I use it for resizing, composition, treatment, etc. <a href="https://imagemagick.org/">ImageMagick</a> is pretty much a photoshop that you can control with text. It is fairly simple considering what you can do with it, but the number of options can be intimidating if you are trying to do something simple.</p><p>There are other simpler tools when you just want to bulk convert images for web. If you happen to be on Mac OSX, then you already have sips installed. The good thing with this is that if you share a double-clickable script with somebody who is not technical, you can be sure that if they are on Mac OSX they have sips available.</p><p>The command sips is basically the low level command for images modifications. It allows us to resize images, rotate, flip, pad, change their color space, etc. Pretty much anything you can do with &quot;Preview.app&quot;. Here is the <a href="https://ss64.com/osx/sips.html">man page</a>. But you can access it from the command line with the traditional:</p><pre>$ <strong>man</strong> sips</pre><p>Now if we want to convert images for web, we want to make sure of the following:</p><ul><li>The image is in a format like jpeg or png.</li><li>The image 72dpi.</li><li>The image is compressed to minimize loading time.</li><li>The image is reasonably wide.</li><li>The image is RGB.</li></ul><p>Let’s start with the simplest thing and convert our images to jpeg. That way, we will get the details of the command.</p><pre>$ <strong>cd</strong> /path/to/my/images/folder/<br>$ <strong>mkdir</strong> ../web<br>$ <strong>sips</strong> -s format jpeg * --out &quot;../web/&quot;</pre><p>So the basics are that we move to the folder containing images. We create a destination folder called “web”. Then we use the sips command running it on all files because I&#39;ve put *. In reality you want to make sure these are image files, you can use a glob like this instead which uses extended glob: ?(*.jpg|*.jpeg|*.tif|*.tiff). Again simplified, you might to include more formats and their uppercase version. At the end we specify the destination folder with `--out &quot;../web/&quot;.</p><p>But the most interresting part is obviously the -s format jpeg. This turns the output into a jpeg and follows a pattern common with most arguments of sips. You set an attribute with key and value. The -s is indeed followed with 2 words. I haven&#39;t seen this on many commands.</p><p>Now let’s add compression to our sips command:</p><pre>$ <strong>sips</strong> -s format jpeg -s formatOptions high * --out &quot;../web/&quot;</pre><p>As you can see the argument follows the same pattern with -s. The value can either be a number which will be interpreted as a percentage of compression like &quot;80&quot;. Or you have keywords describing the quality like &quot;best&quot;, &quot;high&quot;, &quot;normal&quot; and &quot;low&quot;.</p><p>Now let’s add the image resolution:</p><pre>$ <strong>sips</strong> -s format jpeg -s formatOptions high -s dpiWidth 72 -s dpiHeight 72 * --out &quot;../web/&quot;</pre><p>Again quite explicit and following the same pattern. You want to set it for the width <strong>and</strong> the height. And there you have it: 72 dpi.</p><p>Now for the resizing there are many options or strategies. You can read the man page for more details, but I went for the -Z option which sets the longest side of the image. That is to say with &quot;1600&quot;, the output will be 1600 pixels wide if it is a landscape image, or 1600 pixels high if it is a portrait image.</p><pre>$ <strong>sips</strong> -s format jpeg -s formatOptions high -s dpiWidth 72 -s dpiHeight 72 -Z 1600 * --out &quot;../web/&quot;</pre><p>Now let’s add the trickiest part and make the output RGB.</p><pre>$ <strong>sips</strong> -s format jpeg -s formatOptions high -s dpiWidth 72 -s dpiHeight 72 -Z 1600 -m &#39;/System/Library/ColorSync/Profiles/sRGB Profile.icc&#39; * --out &quot;../web/&quot;</pre><p>I said “tricky”, it is not that it is necessarily complicated but you need to reference a color profile file and it is not obvious where to find one of these on your machine. I did not find the location on the man page. You need a color profile because basic equivalence between RGB and CMYK is not ideal. You can read more about this on the article about colors of our <a href="https://www.tlbx.app/color-converter">Color Converter</a> application. I am not sure this is the simplest way to make an image RGB, and I am not sure the file is always at the same location on all versions of OSX. But it is quite likely if you have a fairly recent version.</p><p>So there you have it. A script to convert images for web. As an extra, here is a little <a href="https://en.wikipedia.org/wiki/AppleScript">AppleScript</a> which does pretty much the same thing but ask for details like width and compression with a simple GUI so that it can be used without fiddling with code. This is an ideal way to package a functionality to share with somebody who is not technical. Just open it with the AppleScript editor and export it as an App.</p><p>The AppleScript has a slightly different behavior though. It does filter files in the folder so that you convert only image files. You can obviously modify this list of formats. And the destination folder is created <strong>inside</strong> the folder containing the images. And this folder is created for you if it does not exist yet.</p><p>Here you go:</p><pre><strong>tell</strong> application &quot;Finder&quot;<br>  <strong>set</strong> myFolder <strong>to</strong> choose folder<br>  <strong>set</strong> myFiles <strong>to</strong> files of myFolder whose name extension is in {&quot;jpg&quot;, &quot;jpeg&quot;, &quot;tif&quot;, &quot;tiff&quot;, &quot;png&quot;, &quot;JPG&quot;, &quot;JPEG&quot;, &quot;TIF&quot;, &quot;TIFF&quot;, &quot;PNG&quot;}</pre><pre>  <strong>if</strong> (exists folder &quot;web&quot; of folder myFolder) is false <strong>then</strong><br>    <strong>make</strong> folder at myFolder with properties {name:&quot;web&quot;}<br>  <strong>end</strong> <strong>if</strong></pre><pre>  <strong>set</strong> widthChoices <strong>to</strong> {&quot;1600&quot;, &quot;800&quot;, &quot;400&quot;}<br>  <strong>set</strong> widthChoice <strong>to</strong> {choose from list widthChoices with prompt &quot;Pick a width&quot;}<br>  <strong>set</strong> qualityChoices <strong>to</strong> {&quot;high&quot;, &quot;normal&quot;, &quot;low&quot;}<br>  <strong>set</strong> qualityChoice <strong>to</strong> {choose from list qualityChoices with prompt &quot;Pick a quality&quot;}<br>  <strong>set</strong> formatChoices <strong>to</strong> {&quot;jpeg&quot;, &quot;png&quot;}<br>  <strong>set</strong> formatChoice <strong>to</strong> {choose from list formatChoices with prompt &quot;Pick a format&quot;}</pre><pre>  <strong>set</strong> imgPaths <strong>to</strong> &quot;&quot;<br>    <strong>repeat</strong> <strong>with</strong> myFile <strong>in</strong> myFiles<br>        <strong>set</strong> imgPaths <strong>to</strong> imgPaths &amp; quoted form of POSIX path of (myFile as text) &amp; &quot; &quot;<br>    <strong>end repeat</strong></pre><pre>  <strong>set</strong> destinationFolder <strong>to</strong> myFolder &amp; &quot;web:&quot; as string<br>  <strong>set</strong> destinationFolder <strong>to</strong> quoted form of POSIX path of destinationFolder</pre><pre>  <strong>do shell script</strong> &quot;sips -s format &quot; &amp; formatChoice &amp; &quot; -s formatOptions &quot; &amp; qualityChoice &amp; &quot; -Z &quot; &amp; widthChoice &amp; &quot; -s dpiWidth 72 -s dpiHeight 72 -m &#39;/System/Library/ColorSync/Profiles/sRGB Profile.icc&#39; &quot; &amp; imgPaths &amp; &quot; --out &quot; &amp; destinationFolder</pre><pre>  <strong>display dialog</strong> &quot;Done !&quot;</pre><pre><strong>end tell</strong></pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=656c502a67a6" width="1" height="1" alt=""><hr><p><a href="https://medium.com/shell-life/convert-images-for-web-using-the-sips-command-line-on-macosx-656c502a67a6">Convert images for web using the Sips command line on MacOSX</a> was originally published in <a href="https://medium.com/shell-life">Shell Life</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Zshell version of the flasher function]]></title>
            <link>https://medium.com/shell-life/zshell-version-of-the-flasher-function-3aee2be0e7db?source=rss----613b0939c201---4</link>
            <guid isPermaLink="false">https://medium.com/p/3aee2be0e7db</guid>
            <category><![CDATA[flasher]]></category>
            <category><![CDATA[linux]]></category>
            <category><![CDATA[zsh]]></category>
            <category><![CDATA[command-line]]></category>
            <dc:creator><![CDATA[Mickaël Riga]]></dc:creator>
            <pubDate>Tue, 22 Sep 2020 09:26:27 GMT</pubDate>
            <atom:updated>2020-09-21T12:33:43.421Z</atom:updated>
            <content:encoded><![CDATA[<p>The “flasher” function is just a small command which flashes your screen until you press any key. It is usually put at the end of a long running process in order to indicate that the job is finished.</p><pre>$ <strong>crystal</strong> build c3po-protocols.cr --release; <strong>flasher</strong></pre><p>Without further ado, here is the function:</p><pre><strong>flasher</strong> () {<br>  <strong>while</strong> true; <strong>do</strong><br>    <strong>printf</strong> &quot;\e[?5h&quot;<br>    <strong>sleep</strong> 0.2<br>    <strong>printf</strong> &quot;\e[?5l&quot;<br>    <strong>read</strong> -sk -t1 &amp;&amp; break<br>  <strong>done</strong><br>}</pre><p>As you can see if you know the Bash version, it is not very different. The tricky part is when it reads because read is a builtin and has different options. In fact if you run the Bash version, you&#39;ll get a bad option: -1. That is because in zshell, the option for the number of keys you are expecting is -k instead of -n. If you don&#39;t pass a number, it will default to 1.</p><p>The way this function works is that it uses the terminal <a href="https://en.wikipedia.org/wiki/ANSI_escape_code">escape sequences</a> \e[?5h and \e[?5l. They switch on and off reverse video in a loop. At the end of each cycle we try to read a character from the user with a timeout of 1 second. If no key is pressed after 1 second, it does another cycle. Pretty simple.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3aee2be0e7db" width="1" height="1" alt=""><hr><p><a href="https://medium.com/shell-life/zshell-version-of-the-flasher-function-3aee2be0e7db">Zshell version of the flasher function</a> was originally published in <a href="https://medium.com/shell-life">Shell Life</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[AppleScript to show unread emails in the terminal]]></title>
            <link>https://medium.com/shell-life/applescript-to-show-unread-emails-in-the-terminal-a05ddc515a24?source=rss----613b0939c201---4</link>
            <guid isPermaLink="false">https://medium.com/p/a05ddc515a24</guid>
            <category><![CDATA[unix]]></category>
            <category><![CDATA[apple]]></category>
            <category><![CDATA[shell]]></category>
            <category><![CDATA[command-line]]></category>
            <category><![CDATA[applescript]]></category>
            <dc:creator><![CDATA[Mickaël Riga]]></dc:creator>
            <pubDate>Tue, 22 Sep 2020 09:25:58 GMT</pubDate>
            <atom:updated>2020-09-14T13:24:44.009Z</atom:updated>
            <content:encoded><![CDATA[<p>If you are on OSX and use Mail.app, but work a lot in the command line and would like a simple command to print a summary of unread emails, then this post is for you.</p><p>For this we are going to use <a href="https://en.wikipedia.org/wiki/AppleScript">AppleScript</a>. We’ll see at the end why AppleScript is not necessarily a great scripting language, but it let’s us control graphical applications. This means we can do things that are hardly possible otherwise.</p><h3>Just print the amount of unread emails</h3><p>Let’s start with a first version:</p><pre>#!/usr/bin/osascript<br><strong>on run</strong><br>  <strong>tell</strong> application &quot;Mail&quot;<br>    <strong>return</strong> the unread count of inbox<br>  <strong>end tell</strong><br><strong>end run</strong></pre><p>This script just prints the total number of unread emails in all accounts. Save it in a file named “unread-emails” and make it executable with chmod +x unread-emails. Then you can call it from anywhere if the script is in your $PATH.</p><p>We’ve already learned a few things. We now know the <a href="https://en.wikipedia.org/wiki/Shebang_(Unix)">shebang line</a> for applescripts. This osascript command is what runs an applescript from the terminal, or a shell script. The &quot;OSA&quot; means <a href="https://en.wikipedia.org/wiki/AppleScript#Open_Scripting_Architecture">Open Scripting Architecture</a> and the command is called like this because potentially, applescript is not the only language which can script OSX applications. I never used it but there is a javascript version called &quot;JXA&quot;.</p><p>Back to our script, there is a main on run block which is the main function that is called when starting a script. I am not sure it is needed but I always put it in case I add arguments to the command. Then you have what is called a tell block. It puts you in the context of the Mail application so that you can control it. Once in this block, you can send instructions like check new mail or access specific objects like mailboxes and messages.</p><p>Then comes the line that actually gets the total number of unread emails. It should be self explanatory since applescripts are so verbose. It makes then easy to read, but not easy to write. And you can see the first most annoying thing: there is no easy way to print to stdout from an applescript as far as I know. One way is to use do shell script to use a shell command like echo or printf. The other way is to return something from on run because it will be printed.</p><p>I used the return method because otherwise each do shell script starts a new subprocess. However you&#39;ll see it is not great either because it means if you print a lot of things, you need to set an output string and add to it until you&#39;re done and can return it.</p><p>Anyway, this little script should work, it prints the amount of unread emails in all accounts.</p><h3>Separate each mail account</h3><p>If you are like me, you have more than one account, and some of them are disabled. Here is a new version which prints the amount of unread emails for each account.</p><pre>#!/usr/bin/osascript</pre><pre><strong>on run</strong><br>  <strong>tell</strong> application &quot;Mail&quot;<br>    <strong>set</strong> myOutput <strong>to</strong> &quot;&quot;<br>    <strong>repeat</strong> <strong>with</strong> a <strong>in</strong> every account <strong>whose</strong> enabled <strong>is</strong> true<br>      <strong>set</strong> accountName <strong>to</strong> name of a<br>      <strong>set</strong> myMailbox <strong>to</strong> mailbox &quot;INBOX&quot; of account accountName<br>      <strong>set</strong> myOutput <strong>to</strong> myOutput &amp; accountName &amp; &quot;: &quot;<br>      <strong>set</strong> myOutput <strong>to</strong> myOutput &amp; unread count of myMailbox<br>      <strong>set</strong> myOutput <strong>to</strong> myOutput &amp; &quot;\n&quot;<br>    <strong>end repeat</strong><br>    <strong>set</strong> myOutput <strong>to</strong> myOutput &amp; &quot;\nTotal: &quot; &amp; unread count of inbox<br>    <strong>return</strong> myOutput<br>  <strong>end tell</strong><br><strong>end run</strong></pre><p>Definitely more meaty. The first thing to note is that now we are using set ... to ... a lot. This is how you set a variable in applescript. The first variable is the string we will build up to return it as output.</p><p>We then iterate through accounts with a repeat loop. We use a whose statement to filter accounts and only get the ones that are enabled. For each account we print the account name and the amount of unread emails. The &amp; is how you concatanate strings in applescript. And the way you get a property of a record is with name of a. You can also use a&#39;s name which is supposed to read better, but as a developer I would rather have a dot notation to be honest.</p><p>One thing to note is that the way you get the count for one account is slightly different. The inbox shortcut seems to only work when it is for all accounts. When counting on a single account, you need to name the inbox explicitly with mailbox &quot;INBOX&quot; of account accountName.</p><p>At the very end, we print the total amount of unread emails like before and then return the string to be printed. When running this command, you should see an output like this:</p><pre>Perso: 2<br>Work: 1<br>Enq: 1</pre><pre>Total: 4</pre><h3>Add a summary for each unread email</h3><p>Alright, so far so good. Now let’s add a little summary for each unread email with the sender and the subject line.</p><pre>#!/usr/bin/osascript</pre><pre><strong>on run<br>  tell</strong> application &quot;Mail&quot;<br>    <strong>set</strong> myOutput <strong>to</strong> &quot;&quot;<br>    <strong>repeat</strong> <strong>with</strong> a <strong>in</strong> every account <strong>whose</strong> enabled <strong>is</strong> true<br>      <strong>set</strong> accountName <strong>to</strong> name of a<br>      <strong>set</strong> myMailbox <strong>to</strong> mailbox &quot;INBOX&quot; of account accountName<br>      <strong>set</strong> myOutput <strong>to</strong> myOutput &amp; accountName &amp; &quot;: &quot;<br>      <strong>set</strong> myOutput <strong>to</strong> myOutput &amp; unread count of myMailbox<br>      <strong>set</strong> myOutput <strong>to</strong> myOutput &amp; &quot;\n&quot;<br>      <strong>repeat with</strong> msg <strong>in</strong> every message of myMailbox <strong>whose</strong> read status <strong>is</strong> false<br>        <strong>set</strong> myOutput <strong>to</strong> myOutput &amp; &quot;- &quot; &amp; sender of msg &amp; &quot;\n&quot;<br>        <strong>set</strong> myOutput <strong>to</strong> myOutput &amp; &quot;  &quot; &amp; subject of msg &amp; &quot;\n&quot;<br>      <strong>end repeat<br>    end repeat</strong><br>    <strong>set</strong> myOutput <strong>to</strong> myOutput &amp; &quot;\nTotal: &quot; &amp; unread count of inbox<br>    <strong>return</strong> myOutput<br>  <strong>end tell<br>end run</strong></pre><p>The output should now look like this:</p><pre>Perso: 2<br>- Mom &lt;mom@family.com&gt;<br>  Did you receive my funny cat images ?<br>- Dad &lt;dad@family.com&gt;<br>  You need to help me set the wifi printer<br>Work: 1<br>- Boss &lt;boss@company.com&gt;<br>  Urgent !!!<br>Enq: 1<br>- &lt;hjdfffug@aj47fhvf83nd9.org&gt;<br>  This is not a spam. I&#39;ve lost 20 kg in 1h.</pre><pre>Total: 4</pre><p>The only difference in the script are these lines which iterate through unread messages:</p><pre><strong>repeat with</strong> msg <strong>in</strong> every message of myMailbox <strong>whose</strong> read status <strong>is</strong> false<br>  <strong>set</strong> myOutput <strong>to</strong> myOutput &amp; &quot;- &quot; &amp; sender of msg &amp; &quot;\n&quot;<br>  <strong>set</strong> myOutput <strong>to</strong> myOutput &amp; &quot;  &quot; &amp; subject of msg &amp; &quot;\n&quot;<br><strong>end repeat</strong></pre><p>So it iterates through the messages of the current mailbox that are unread (i.e. read status is false). And then for each one we display the sender and the subject line with a bit of indentation for clarity.</p><p>There are obviously a lot of things that can be improved. We could check for new mails and wait before printing all this info. Also we could use a bit of color. That would be trivial in a shell script, but applescript is not primarily made to interract with command line. We probably would have to process the output through another command which adds colors.</p><h3>Conclusion on Applescript</h3><p>I don’t personally know anybody who is really comfortable with AppleScript. In my opinion, the main reason is because it is too verbose for somebody who’s a developer, yet too technical for an average user. This is probably why Apple created the Automator layer on top of it.</p><p>One other reason is that error messages are not really helpful. The intention is that it reads like English, but then you miss a trivial word and get a cryptic message that is hard to interpret, even for a trained developer.</p><p>Speed can also be an issue because we essentially control graphical applications. This obviously defeats the point of a command line interface. For example printing unread messages like we just did could be more efficient if we used a command line mail client like <a href="http://www.mutt.org/">Mutt</a>. But writting an applescript is a good option when you want to come up with a quick and dirty solution. Or simply when there is no other way.</p><p>My advice would be to use it only for the needed part and do the rest with shell scripts. I mean a clear short task. Although if you’re writing a script that is meant to be used by somebody who is not technical, then that is where it shines. You can write a script that you start with a double click, then use system dialog boxes instead of the typical stdin/stdout flow. You can even use the system panel for choosing files. Or you can create droplets with the on open block.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a05ddc515a24" width="1" height="1" alt=""><hr><p><a href="https://medium.com/shell-life/applescript-to-show-unread-emails-in-the-terminal-a05ddc515a24">AppleScript to show unread emails in the terminal</a> was originally published in <a href="https://medium.com/shell-life">Shell Life</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Delete any Git branch the easy way]]></title>
            <link>https://medium.com/shell-life/delete-any-git-branch-the-easy-way-8857f6b35d06?source=rss----613b0939c201---4</link>
            <guid isPermaLink="false">https://medium.com/p/8857f6b35d06</guid>
            <category><![CDATA[unix]]></category>
            <category><![CDATA[github]]></category>
            <category><![CDATA[git]]></category>
            <category><![CDATA[command-line]]></category>
            <category><![CDATA[linux]]></category>
            <dc:creator><![CDATA[Mickaël Riga]]></dc:creator>
            <pubDate>Tue, 22 Sep 2020 09:25:41 GMT</pubDate>
            <atom:updated>2020-09-14T10:08:03.048Z</atom:updated>
            <content:encoded><![CDATA[<p>I don’t know about you but each time I want to delete a remote branch in <a href="https://git-scm.com/">Git</a>, I always end up looking online. For reference, here is the command:</p><pre>$ git push &lt;my-remote&gt; :&lt;my-branch&gt;</pre><p>Not the end of the world, but quite obscure to me because there is no reference to “branch” or “delete”. And the worst is that it does look very different from the command to delete a local branch.</p><pre>$ git branch -d &lt;my-branch&gt;</pre><p>That one is acceptable, although I would still write an alias for it. But the thing is that I would like them to be similar. There are probably other ways to do this, maybe simpler ones, but here is the <a href="https://www.zsh.org/">zshell</a> function I ended up writing:</p><pre>gbd() {<br>  if [[ $# == 0 ]] || [[ $1 == &#39;-h&#39; ]]; then<br>    echo &#39;Git branch delete&#39;<br>    echo &#39;Usage local:  gbd &lt;LOCAL_BRANCH&gt;&#39;<br>    echo &#39;Usage remote: gbd &lt;REMOTE&gt;/&lt;BRANCH&gt;&#39;<br>    return 0<br>  fi<br>  local remote=${1:h}<br>  local branch=${1:t}<br>  if [[ $remote == &#39;.&#39; ]]; then<br>    echo &quot;git branch -d $branch&quot;<br>    git branch -d &quot;$branch&quot;<br>  else<br>    echo &quot;git push $remote :$branch&quot;<br>    git push &quot;$remote&quot; &quot;:$branch&quot;<br>  fi<br>}</pre><p>The name “gbd” stands for “git branch delete” which respects the order of words in a subcommand chain. That is to say from the least specific to the most specific. You can obviously pick another name. Now if you call the command without arguments or with -h, it will print the usage. And as the usage shows, you can either pass the name of a local branch, or remote and branch separated by a slash.</p><pre>$ gbd newfeature<br>$ gbd staging/newfeature</pre><p>The good thing with using a slash as a separator between remote and branch is that I can use <a href="https://www.tlbx.app/blog/zshell-path-modifiers">Zshell path modifiers</a> to isolate them.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8857f6b35d06" width="1" height="1" alt=""><hr><p><a href="https://medium.com/shell-life/delete-any-git-branch-the-easy-way-8857f6b35d06">Delete any Git branch the easy way</a> was originally published in <a href="https://medium.com/shell-life">Shell Life</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Zshell path modifiers]]></title>
            <link>https://medium.com/shell-life/zshell-path-modifiers-5fe3f02e3398?source=rss----613b0939c201---4</link>
            <guid isPermaLink="false">https://medium.com/p/5fe3f02e3398</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[linux]]></category>
            <category><![CDATA[shell]]></category>
            <category><![CDATA[bash]]></category>
            <category><![CDATA[zsh]]></category>
            <dc:creator><![CDATA[Mickaël Riga]]></dc:creator>
            <pubDate>Tue, 22 Sep 2020 09:25:20 GMT</pubDate>
            <atom:updated>2020-09-09T14:01:13.828Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*ePQYMQersCz4aIyy" /><figcaption>Photo by <a href="https://unsplash.com/@dianadeaver?utm_source=medium&amp;utm_medium=referral">Diana Deaver</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><blockquote><strong><em>Disclaimer</em></strong><em>: </em><a href="https://www.zsh.org/"><em>Zshell</em></a><em> is the shell used for this article. It does not mean that these modifiers are not available in other shells, or that there is no other way to do the same thing in other shells. Look it up if you’re not sure.</em></blockquote><p>You probably already know a few ways to transform variables inherited from <a href="https://www.gnu.org/software/bash/">Bash</a>. If not, you should check them out because they are really useful. They allow you to get a substring, substitute a word for another, etc. Here are a few variable modifiers for handling paths.</p><p>One common use is to have a path variable and you want to extract the directory name or the extension. You could use commands for this like dirname or basename, but there is a better way than starting a process. There are built-in modifiers. You&#39;ll find many examples on the internet which remove something before or after a certain character, but there are better modifiers for this.</p><p>Let’s put a path in a variable:</p><pre>$ MYPATH=&quot;/path/to/image.png&quot;<br>$ <strong>echo</strong> $MYPATH<br>/path/to/image.png</pre><p>There are 2 ways of splitting a path. The first is head/tail, which splits at the last slash.</p><pre>$ <strong>echo</strong> $MYPATH:h<br>/path/to<br>$ <strong>echo</strong> $MYPATH:t<br>image.png</pre><p>The second way is root/extension, which splits at the last dot.</p><pre>$ <strong>echo</strong> $MYPATH:r<br>/path/to/image<br>$ <strong>echo</strong> $MYPATH:e<br>png</pre><p>If you want to get only the name of the file without the extension, you can combine tail and root.</p><pre>$ <strong>echo</strong> $MYPATH:t:r<br>image</pre><p>There is a lot more to this but if you use them in a string, it is safer to use curly brackets around the variable.</p><pre>$ <strong>echo</strong> &quot;This is my file name: ${MYPATH:t:r}&quot;<br>This is my file name: image</pre><p>Now another usefull one which gives you the absolute path of the variable.</p><pre>$ IMGPATH=img.png<br>$ <strong>echo</strong> ${IMGPATH:a}<br>/Users/me/img.png</pre><pre># It works with nesting<br>$ IMGPATH=images/img.png<br>$ <strong>echo</strong> ${IMGPATH:a}<br>/Users/me/images/img.png</pre><pre># It works with .. and . as well<br>$ IMGPATH=&quot;../victor/img.png&quot;<br>$ <strong>echo</strong> ${IMGPATH:a}<br>/Users/victor/img.png</pre><p>Note that the paths we make absolute do not have to exist. The modifier just outputs what it would be based on the current directory. And you can use :A instead if you want to resolve symbolic links.</p><p>Now the :c modifier outputs the absolute path of command name, using the PATH variable, like what the which command would do.</p><pre>$ MYCOMMAND=nvim<br>$ <strong>echo</strong> ${MYCOMMAND:c}<br>/usr/local/bin/nvim</pre><h3>It works with regular arguments as well</h3><p>If you put it in backets, you can use these on regular arguments, even when globbing. This command for example prints all the C filenames (without extension) nested in the src directory.</p><pre>$ <strong>print</strong> -l src/**/*.c(:t:r)</pre><h3>Other modifiers</h3><p>I know the title of this article refers to “path” modifiers but they don’t have to be used on paths as long as they do what you want. And equally, some modifiers were not created especially for paths, but can be used on them anyway. Here are a few.</p><p>Another useful modifier that could be used on path or anything else is the :s modifier. It is the typical substitute that you find in grep and in many other softwares. Here we copy all text files into another one with the markdown extension.</p><pre><strong>for</strong> file in *.txt; <strong>do</strong><br>  <strong>cp</strong> ${file} ${file:s/txt/md/}<br><strong>done</strong></pre><p>Like many substitution tool, it works only one time. If you want to substitute multiple times, use :gs instead.</p><pre>$ MYFILE=css/main.css<br>$ <strong>echo</strong> ${MYFILE:gs/css/js/}<br>js/main.js</pre><p>Also like many substitution tools, you can use any character for the delimiter. This is handy when you want to replace a slash because otherwise you would have to escape it.</p><pre>$ MYFILE=img/large/01.png<br>$ <strong>echo</strong> ${MYFILE:gs_/_._}<br>img.large.01.png</pre><p>You can quote text with :q and unquote it with :Q.</p><pre>$ MYVAR=&quot;Hello zshell &#39;world&#39;&quot;<br>$ <strong>echo</strong> ${MYVAR:q}<br>Hello\ zshell\ \&#39;world\&#39;</pre><pre>$ MYVAR=&quot;Hello\ zshell\ \&#39;world\&#39;&quot;<br>$ <strong>echo</strong> ${MYVAR:q}<br>Hello zshell &#39;world&#39;</pre><p>You can obviously make a variable all lowercase or all uppercase.</p><pre>$ MYVAR=&quot;MAKE ME WHISPER&quot;<br>$ <strong>echo</strong> ${MYVAR:l}<br>make me whisper</pre><pre>$ MYVAR=&quot;make me shout&quot;<br>$ <strong>echo</strong> ${MYVAR:u}<br>MAKE ME SHOUT</pre><p>There is also a way to capitalize words. I won’t go into details explaining why it looks different, but here is how you do this:</p><pre>$ MYVAR=&quot;hello world&quot;<br>$ <strong>echo</strong> ${(C)MYVAR}<br>Hello World</pre><p>Finally a very useful functionality is substring extraction. For this you use 2 numbers representing the index of the first character and the last one (excluded). For some reason, a negative index (from the end) works on the second number, but not the first one. Don’t hesitate to <a href="https://www.tlbx.app/contact">contact us</a> is you know why.</p><pre>$ MYDIGITS=&quot;0123456789&quot;<br>$ <strong>echo</strong> ${MYDIGITS:0:4}<br>012</pre><pre>$ <strong>echo</strong> ${MYDIGITS:0:-2}<br>01234567</pre><pre># You can omit the second number if you want the end of the string<br>$ <strong>echo</strong> ${MYDIGITS:2}<br>23456789</pre><h3>Conclusion</h3><p>This list of modifiers is not complete and there are a lot of other things to learn about <a href="https://www.zsh.org/">zshell</a>. I encourage you to read more about it. This <a href="https://grml.org/zsh/zsh-lovers.html">zsh lovers page</a> is a good start.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5fe3f02e3398" width="1" height="1" alt=""><hr><p><a href="https://medium.com/shell-life/zshell-path-modifiers-5fe3f02e3398">Zshell path modifiers</a> was originally published in <a href="https://medium.com/shell-life">Shell Life</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Introduction To Jobs In The Terminal]]></title>
            <link>https://medium.com/shell-life/introduction-to-jobs-in-the-terminal-9882ef5136e1?source=rss----613b0939c201---4</link>
            <guid isPermaLink="false">https://medium.com/p/9882ef5136e1</guid>
            <category><![CDATA[process]]></category>
            <category><![CDATA[linux]]></category>
            <category><![CDATA[command-line]]></category>
            <category><![CDATA[unix]]></category>
            <category><![CDATA[shell]]></category>
            <dc:creator><![CDATA[Mickaël Riga]]></dc:creator>
            <pubDate>Tue, 22 Sep 2020 09:24:51 GMT</pubDate>
            <atom:updated>2020-08-20T13:37:21.752Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*5kLHLxZjkN4ZGsdb" /><figcaption>Photo by <a href="https://unsplash.com/@dianadeaver?utm_source=medium&amp;utm_medium=referral">Diana Deaver</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Essentially, jobs are <a href="https://en.wikipedia.org/wiki/Process_(computing)">processes</a> which you started from your terminal session. You see all processes with the ps command, but processes which you started from one terminal session are also in your jobs and the shell has built-in functions to operate on them and improve your workflow.</p><p>Note that the examples below are using built-in functions, therefore I know they work in <a href="https://www.zsh.org/">zshell</a> and <a href="https://www.gnu.org/software/bash/">bash</a>, but there might be differences in other shells.</p><h3>The Basics</h3><p>Most of the commands you type are executed quickly and you cannot see them in your jobs. So in order to see something, let’s run something intentionally long.</p><pre>$ sleep 500</pre><p>Type this in your terminal and run it. Now you normally would be waiting 500 seconds for your prompt to be back. But here is something important to know: The shell is always a Ctrl+Z away.</p><p>Go ahead and type Ctrl+Z. You probably know about Ctrl+C if you deal with servers sometimes, or other long running processes. While Ctrl+C stops your job completely, Ctrl+Z only pauses it. You get back your prompt, can type other commands, and then resume your paused job whenever you want.</p><p>Go ahead and use the jobs command. You should see something like this:</p><pre>$ jobs<br>[1]  + suspended  sleep 500</pre><p>So the purpose of the jobs command is to show you the jobs with their state (suspended in this case) and an index. This index or job number is important because it can be passed to other commands as a reference.</p><p>Let say we wanted to kill this job, we can use the kill command. But instead of looking for its process ID, we only need to use the job number prefixed with %. This is important to always prefix them with % so that the command does not see it as a process ID.</p><p>So here we go:</p><pre>$ kill %1</pre><p>Now normally if you run jobs again, it should be empty.</p><p>We obviously could get the process ID of the last run command with echo $! but with jobs index, it does not have to be the last run command.</p><h3>A Practical Example</h3><p>Okay now let’s imagine a more practical case. You are working on a web project and you need to do some modifications. You start vim (or any other terminal-based editor). Then you start to edit your files and comes the time to try it in the browser. Which means that you need to start your web server.</p><p>Once again, the terminal is always a Ctrl+Z away. So let&#39;s pause vim with Ctrl+Z, and let&#39;s start our server:</p><pre>$ bundle exec sinatra &amp;&gt;/dev/null &amp;</pre><p>Here is a weird command if you are not used to it. The basic command itself does not matter, here we are starting a ruby/sinatra application but it could be any other server for any language. The end of the command is more relevant to our topic. Basically the &amp; at the very end makes this a background job, meaning that it does the same thing as without, but you get your prompt back. It runs in the background.</p><p>This is the other concept important to get about jobs. There are foreground jobs and background jobs. While a command is executing in your prompt, it is in the foreground. Any quick command is in the foreground for the duration of its execution. It is the same for long commands and this is why you don&#39;t get your prompt back until it is finished, or until you do something to stop it or pause it.</p><p>When you put a job in the background, you get back your prompt but it is not stopped or paused. It is still running in the background doing its job. You can start any command in the background by adding &amp; at the end. Now the problem is that a command in the background is still printing messages in your prompt since this terminal is still its stdout and stderr. This could be quite disruptive.</p><p>The &amp;&gt;/dev/null has an explanation which exceeds the scope of this tutorial, but just know that it just prevents the log messages from being printed on your screen. It discards them. A better option would obviously be to redirect this to a log file instead of /dev/null.</p><p>Now if we run the jobs command again you&#39;ll see something like:</p><pre>$ jobs<br>[1]  + suspended  vim .<br>[2]  + running    bundle exec sinatra &amp;&gt;/dev/null &amp;</pre><p>You now see 2 jobs. Please note the difference, the server is not paused/suspended. It is currently running and you can check your browser. Whereas when a command is paused, it stops its execution for a while. For example when we paused the sleep command, then it stopped counting. You have to resume a paused command if you want it to carry on with its work. Which leads me to our next step: Going back to vim.</p><p>One way to resume a job is to make it a foreground job. You can put a job to background with the bg command, or to the foreground with the fg command. If your text editor is job number 1 like in our example, then you can resume with:</p><pre>$ fg %1</pre><p>You’re back in the text editor. Also fg defaults to the last job you left, which means that you can go back and forth of any job with Ctrl+Z and fg. You don&#39;t have to type the job number in this case.</p><p>Now let’s say you’ve started your server but forgot to make it a background job. What you can do is pause it with Ctrl+Z and run bg immediatly. And then your server will be running again, but in the background. You don&#39;t need to type a job number because it is the last job you&#39;ve paused. And note that fg and bg are also resuming the execution of a paused job.</p><h3>Conclusion</h3><p>This probably will not change your world, but it is still satisfying to have more control on different jobs in a terminal session instead of having to use ps and process IDs which are quite long numbers compared to job numbers.</p><p>Here are 2 last tips:</p><p>The first is that if you want to refer to a job and you know for sure that it is the only job containing a certain word, you can refer to this word instead.</p><p>For example we could have stopped our server with:</p><pre>$ kill %?sinatra</pre><p>Note the % which means we are refering to a job, and the ? which tells the shell to look for that word<br> in the command instead of a job number.</p><p>The last tip is that if you use jobs a lot and also have multiple terminal tabs, it becomes very useful to always have the number of jobs written somewhere in your prompt. That way you can end them properly before closing your tab.</p><p>If you use <a href="https://www.zsh.org/">zshell</a>, the placeholder for having the number of jobs in your prompt is %j. I personnaly have this %(1j. %{$fg[red]%}%j.) which puts the number of jobs in red, but only if there is at least 1 job.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9882ef5136e1" width="1" height="1" alt=""><hr><p><a href="https://medium.com/shell-life/introduction-to-jobs-in-the-terminal-9882ef5136e1">Introduction To Jobs In The Terminal</a> was originally published in <a href="https://medium.com/shell-life">Shell Life</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>