<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Anton Sakhanovych</title>
    <description>The latest articles on DEV Community by Anton Sakhanovych (@antonsakhanovych).</description>
    <link>https://dev.to/antonsakhanovych</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F980282%2F592cf096-a539-4371-ba2f-13719fded259.jpeg</url>
      <title>DEV Community: Anton Sakhanovych</title>
      <link>https://dev.to/antonsakhanovych</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/antonsakhanovych"/>
    <language>en</language>
    <item>
      <title>Bash scripting guide</title>
      <dc:creator>Anton Sakhanovych</dc:creator>
      <pubDate>Thu, 01 Dec 2022 16:50:07 +0000</pubDate>
      <link>https://dev.to/antonsakhanovych/bash-scripting-guide-1op0</link>
      <guid>https://dev.to/antonsakhanovych/bash-scripting-guide-1op0</guid>
      <description>&lt;h1&gt;
  
  
  What is shebang?
&lt;/h1&gt;

&lt;p&gt;Shebang is a line that starts with &lt;code&gt;#!&lt;/code&gt; to specify the interpreter of the script!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note: Shebang is not specific to the bash scripting!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Usual shebang for the bash scripts looks like this:&lt;code&gt;#!/bin/bash&lt;/code&gt;. However, be careful! Because different linux distributions can have different paths to bash executable. You can check yours by typing &lt;code&gt;which bash&lt;/code&gt; into console. Mine path, for example, is &lt;code&gt;/usr/bin/bash&lt;/code&gt;. The portable shebang that will work on any linux distribution looks like this: &lt;code&gt;#!/usr/bin/env bash&lt;/code&gt; This way the script will look for the path of bash executable using &lt;code&gt;env&lt;/code&gt; command.&lt;/p&gt;

&lt;h2&gt;
  
  
  Hello World!
&lt;/h2&gt;

&lt;p&gt;Now let's write the hello world script!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#!/usr/bin/env bash

echo "Hello World!"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Variables
&lt;/h1&gt;

&lt;p&gt;Variables are pretty straight-forward in bash. You create variable like this: &lt;code&gt;name="Anton"&lt;/code&gt;. &lt;strong&gt;There should be no spaces on both sides of the equal sign!&lt;/strong&gt; To refer to the variable you use &lt;code&gt;$&lt;/code&gt;. For example: &lt;code&gt;echo $name&lt;/code&gt; would print out &lt;code&gt;Anton&lt;/code&gt;. You can even embed variables in strings: &lt;code&gt;echo "My name is $name"&lt;/code&gt; will print out &lt;code&gt;My name is Anton&lt;/code&gt;. &lt;strong&gt;Also, you cannot use single quotations marks to wrap the string!&lt;/strong&gt; This will work differently &lt;code&gt;echo 'My name is $name'&lt;/code&gt; This will print out: &lt;code&gt;My name is $name&lt;/code&gt; because single quotation marks interpret everything as string.&lt;/p&gt;

&lt;h1&gt;
  
  
  Basic math
&lt;/h1&gt;

&lt;p&gt;Basic math in bash is nothing like in other languages.&lt;br&gt;
if you type &lt;code&gt;5 + 5&lt;/code&gt; in the console it will give you this error: &lt;code&gt;bash: 5: command not found&lt;/code&gt; because bash is looking for command to execute but it's not there. Following this logic that the bash needs a command to execute, you might say that there should be a command that does math. And you would completely correct. The command for basic math is &lt;code&gt;expr&lt;/code&gt;. The usage: &lt;code&gt;expr 5 + 5&lt;/code&gt;. The spaces between arguments are important! If you try to run &lt;code&gt;expr 5 +5&lt;/code&gt;, it will give you an error: &lt;code&gt;expr: syntax error: unexpected argument ‘+5’&lt;/code&gt; &lt;/p&gt;
&lt;h1&gt;
  
  
  If statements
&lt;/h1&gt;

&lt;p&gt;If statements are also quite different compared to other languages. It doesn't have =, &amp;lt;, &amp;gt;, &amp;gt;=, &amp;lt;= operators. Instead you write your condition in square brackets like this &lt;code&gt;[$num -eq 1]&lt;/code&gt;. Under the hood bash uses command called &lt;code&gt;test&lt;/code&gt; You can use man pages to learn more. The basic comparison operators are -eq, -gt, lt, -ge, -le, -ne. The example script:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#!/usr/bin/env bash

age=18
if [ age -eq 18 ]
then
    echo "He is $age years old"
fi
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will print out: &lt;code&gt;He is 18 years old&lt;/code&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Exit codes
&lt;/h1&gt;

&lt;p&gt;Exit codes are useful when scripting! You can check whether previous command was successful. You use variable &lt;code&gt;$?&lt;/code&gt; to check the exit code of the previous command. &lt;strong&gt;The general rule for exit codes is that if exit code is not &lt;code&gt;0&lt;/code&gt; it means that you got an error.&lt;/strong&gt; If you run &lt;code&gt;ls&lt;/code&gt; and then &lt;code&gt;echo $?&lt;/code&gt; you will get &lt;code&gt;0&lt;/code&gt; because the execution of ls was successful. However, if you run &lt;code&gt;ls /nonexistent&lt;/code&gt; and then &lt;code&gt;echo $?&lt;/code&gt; it will give you &lt;code&gt;2&lt;/code&gt;. &lt;/p&gt;

&lt;h1&gt;
  
  
  While Loops
&lt;/h1&gt;

&lt;p&gt;While loops are pretty easy too! Combine if statement with while and you get this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;num=0
while [ $num -le 5 ] 
do
    #printing the number
    echo "$num"
    # adding one each iteration
    num=$(expr $num + 1)
done
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You will get this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0
1
2
3
4
5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  For Loops
&lt;/h1&gt;

&lt;p&gt;For loops are just as easy:&lt;br&gt;
Example script:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for n in {1..5}
do
    echo $n
done
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You will get:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1
2
3
4
5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Data Streams
&lt;/h1&gt;

&lt;p&gt;Data streams is the way to redirect standard output(stdout) and standard err(stderr). Use&lt;code&gt;1&amp;gt;&lt;/code&gt; to redirect stdout. &lt;code&gt;ls 1&amp;gt; output.txt&lt;/code&gt; After running this you will be able to find the result of the execution of this command in the file named &lt;code&gt;output.txt&lt;/code&gt;. However all the errors will still be printed out on the screen. You can use &lt;code&gt;2&amp;gt;&lt;/code&gt; to catch any errors and send them to file. And similarly &lt;code&gt;&amp;amp;&amp;gt;&lt;/code&gt; to redirect both stdout and stderr.&lt;/p&gt;

&lt;h1&gt;
  
  
  Functions
&lt;/h1&gt;

&lt;p&gt;Syntax for function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function_name(){
    echo "Parameter #1 is $1"
}

function_name 3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output: &lt;code&gt;Parameter #1 is 3&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Note that when you call a function you don't have parenthesis around the arguments you separate them with spaces.&lt;/p&gt;

&lt;p&gt;Also you can see that you can refer to parameters using &lt;code&gt;$&lt;/code&gt; and the number of the argument. In this case that would be &lt;code&gt;$1&lt;/code&gt; to refer to the first positional argument!&lt;/p&gt;

&lt;h1&gt;
  
  
  Case Statements
&lt;/h1&gt;

&lt;p&gt;Case statements! Just make sure that you put double semi-colon in the end of each case! The * case is just the default behavior in case your variable doesn't match any of the previous cases!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;case $variable in
   case1) instructions;;
   case2) instructions;;
   case3) instructions;;
   case4) instructions;;
   case5) instructions;;
   *) default behavior;;
esac
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Arguments
&lt;/h1&gt;

&lt;p&gt;Just like in functions you can access arguments that you passed while calling a function. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;name=$1
echo $name
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;if you call this script: &lt;code&gt;script.sh Anton&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;It will print out &lt;code&gt;Anton&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Also very handy variable: &lt;code&gt;$#&lt;/code&gt; which contains the total number of arguments. For the script call above the &lt;code&gt;$#&lt;/code&gt; would equal to &lt;code&gt;1&lt;/code&gt; because we passed only one argument &lt;code&gt;Anton&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This article is inspired by series on youtube: &lt;a href="https://www.youtube.com/playlist?list=PLT98CRl2KxKGj-VKtApD8-zCqSaN2mD4w"&gt;Bash Scripting Series&lt;/a&gt; by &lt;a href="https://www.youtube.com/@LearnLinuxTV"&gt;Learn Linux TV&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Thanks for reading this article! And the further exploration of bash scripting is highly encouraged!&lt;/p&gt;

</description>
      <category>bash</category>
      <category>tutorial</category>
      <category>beginners</category>
      <category>linux</category>
    </item>
  </channel>
</rss>
