{"id":65838,"date":"2022-10-17T12:55:52","date_gmt":"2022-10-17T12:55:52","guid":{"rendered":"\/tutorials\/?p=65838"},"modified":"2026-03-10T10:06:12","modified_gmt":"2026-03-10T10:06:12","slug":"bash-script-example","status":"publish","type":"post","link":"\/my\/tutorials\/bash-script-example","title":{"rendered":"25 common Linux bash script examples to get you started"},"content":{"rendered":"<?xml encoding=\"utf-8\" ?><p>Bash or Bourne-again shell is one of the most popular shells and command languages for Linux VPS enthusiasts. It was first released in 1989 and was used as the default shell for most Linux distributions ever since.<\/p><p>Bash scripting allows users and system administrators to automate processes and save hundreds of hours of manual work. It&rsquo;s worth mentioning that Bash is also available for Windows and macOS.<\/p><p>This tutorial will introduce you to what bash scripting is. It features over twenty useful bash script examples to start your bash scripting journey.<\/p><p>\n\n\n<\/p><div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><a href=\"https:\/\/assets.hostinger.com\/content\/tutorials\/pdf\/Linux-Commands-Cheat-Sheet.pdf\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" width=\"1024\" height=\"283\" src=\"https:\/\/www.hostinger.com\/tutorials\/wp-content\/uploads\/sites\/2\/2022\/11\/Linux-cheat-sheet-1024x283.png\" alt=\"\" class=\"wp-image-69262\" srcset=\"https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2022\/11\/Linux-cheat-sheet-1536x425.png 1024w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2022\/11\/Linux-cheat-sheet-300x83.png 300w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2022\/11\/Linux-cheat-sheet-150x41.png 150w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2022\/11\/Linux-cheat-sheet-768x212.png 768w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2022\/11\/Linux-cheat-sheet.png 2048w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure><\/div><h2 class=\"wp-block-heading\" id=\"h-what-is-bash-scripting-used-for\">What Is Bash Scripting Used For<\/h2><p>Before we move on to the topic of bash scripting use cases, we need to elaborate on what bash and bash scripting are.<\/p><p>Bash is a <strong>command-line interface interpreter<\/strong> that runs in a text window where users can manage and execute shell commands. Bash &ndash; or shell scripting &ndash; on the other hand is the process of writing a set of commands to be executed on a Linux system. A file that includes such instructions is called a bash script.<\/p><p>To put it simply, the bash interpreter reads the bash script and executes the commands at the same time. For example, a Linux user can execute hundreds of commands with a single click instead of inputting them one by one. For this reason, bash scripting is the go-to choice for increasing productivity, setting up automation, and eliminating repetitive tasks.<\/p><h2 class=\"wp-block-heading\" id=\"h-25-bash-scripts-examples\">25 Bash Scripts Examples<\/h2><p>The following section will cover 25 of the most popular bash scripting examples, including variable manipulation and echoing out various values. We will also cover functions, arrays, loops, and much more.<\/p><h3 class=\"wp-block-heading\" id=\"h-1-hello-world\">1. Hello World<\/h3><p><strong>Hello World<\/strong> is the most simple bash script to start with. We will create a new variable called <strong>learningbash<\/strong> and print out the words <strong>Hello World<\/strong>. First, open a new shell script file with a text editor of your choice:<\/p><pre class=\"wp-block-preformatted\">nano hello.sh<\/pre><p>Paste the following lines into it:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">#!\/bin\/bash\n#Creates a new variable with a value of \"Hello World\"\nlearningbash=\"Hello World\"\necho $learningbash<\/pre><div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><a href=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2022\/10\/The-command-line-window-showcasing-the-output-of-the-first-bash-script-%E2%80%93-Hello-World.png\"><img decoding=\"async\" src=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2022\/10\/The-command-line-window-showcasing-the-output-of-the-first-bash-script-%E2%80%93-Hello-World.png\" alt=\"The command-line window showcasing the output of the first bash script &ndash; Hello World\" class=\"wp-image-65840\"><\/a><\/figure><\/div><p>The first line (<strong>\/bin\/bash<\/strong>) is used in every bash script. It instructs the operating system to use a bash interpreter as a command interpreter.<\/p><h3 class=\"wp-block-heading\" id=\"h-2-echo-command\">2. Echo Command<\/h3><p>The <strong>echo<\/strong> bash command can be used to print out text as well as values of variables. In the following example, we will showcase how quotation marks affect the echo command. We will start by opening a new bash script file:<\/p><pre class=\"wp-block-preformatted\">nano echo.sh<\/pre><p>This simple bash script example will create a new variable and print it out while using different quotation marks.<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">#!\/bin\/bash\nprovider=\"Hostinger\"\necho 'The best hosting provider is $provider'\necho \"The best hosting provider is $provider\"<\/pre><div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><a href=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2022\/10\/The-command-line-window-shows-the-echo-command.png\"><img decoding=\"async\" width=\"350\" height=\"85\" src=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2022\/10\/The-command-line-window-shows-the-echo-command.png\" alt=\"The command-line window shows the echo command\" class=\"wp-image-65841\" srcset=\"https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2022\/10\/The-command-line-window-shows-the-echo-command.png 350w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2022\/10\/The-command-line-window-shows-the-echo-command-300x73.png 300w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2022\/10\/The-command-line-window-shows-the-echo-command-150x36.png 150w\" sizes=\"(max-width: 350px) 100vw, 350px\" \/><\/a><\/figure><\/div><p>As you can see, if the echo bash command is used with double quotation marks <strong>&ldquo;&ldquo;<\/strong>, then the script will print out the actual value of a variable. Otherwise, if the single quotation marks <strong>&lsquo;&lsquo; <\/strong>are used, it will print out only the name of a variable.<\/p><h3 class=\"wp-block-heading\" id=\"h-3-sleep-command\">3. Sleep Command<\/h3><p><a href=\"\/my\/tutorials\/linux-sleep-command\">Sleep command<\/a> halts all currently running bash scripts and puts the system to sleep. Start by creating a new bash script file:<\/p><pre class=\"wp-block-preformatted\">nano sleep.sh<\/pre><p>Then, paste in the following simple script:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">#!\/bin\/bash\nsleep 10 &amp;&amp; echo &ldquo;I&rsquo;ve been sleeping for 10 seconds, I want more&rdquo; &amp;&amp; sleep 10 &amp;&amp; echo &ldquo;I&rsquo;m done sleeping, thanks!&rdquo;<\/pre><div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><a href=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2022\/10\/A-bash-script-with-the-sleep-command.-The-basic-idea-is-that-it-puts-the-system-to-a-halt-for-a-set-amount-of-time.png\"><img decoding=\"async\" width=\"413\" height=\"86\" src=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2022\/10\/A-bash-script-with-the-sleep-command.-The-basic-idea-is-that-it-puts-the-system-to-a-halt-for-a-set-amount-of-time.png\" alt=\"A bash script with the sleep command. The basic idea is that it puts the system to a halt for a set amount of time\" class=\"wp-image-65842\" srcset=\"https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2022\/10\/A-bash-script-with-the-sleep-command.-The-basic-idea-is-that-it-puts-the-system-to-a-halt-for-a-set-amount-of-time.png 413w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2022\/10\/A-bash-script-with-the-sleep-command.-The-basic-idea-is-that-it-puts-the-system-to-a-halt-for-a-set-amount-of-time-300x62.png 300w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2022\/10\/A-bash-script-with-the-sleep-command.-The-basic-idea-is-that-it-puts-the-system-to-a-halt-for-a-set-amount-of-time-150x31.png 150w\" sizes=\"(max-width: 413px) 100vw, 413px\" \/><\/a><\/figure><\/div><p>The above example starts with a simple sleep bash command that will put your system to sleep for 10 seconds. After that, we combine the previously learned echo command with sleep &ndash; this way system will sleep for 10 seconds, then print out some words, sleep again, print out some words again and end its operation.<\/p><p><div class=\"protip\">\n                    <h4 class=\"title\">Pro Tip<\/h4>\n                    <p> A bash script can always be terminated by clicking <strong>CTRL + C<\/strong> without waiting for it to finish its operation. <\/p>\n                <\/div>\n\n\n\n<\/p><h3 class=\"wp-block-heading\" id=\"h-4-wait-command\">4. Wait Command<\/h3><p><strong>wait<\/strong> is a built-in <a href=\"\/my\/tutorials\/linux-commands\">Linux command<\/a> that waits for completion of running process. The wait command is used with a particular process id or job id.<\/p><p>Here&rsquo;s how to create a <strong>wait<\/strong> bash script. Begin by creating a new bash file:<\/p><pre class=\"wp-block-preformatted\">nano wait.sh<\/pre><p>Paste in the following:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">#!\/bin\/bash\nwait 1234\necho &ldquo;Done&rdquo;<\/pre><p><div><p class=\"important\"><strong>Important!<\/strong> If no job ID is provided, the wait command waits until all child background jobs are completed. <\/p><\/div>\n\n\n\n<\/p><h3 class=\"wp-block-heading\" id=\"h-5-comments\">5. Comments<\/h3><p>Users can easily add comments to their bash scripts with the <strong># <\/strong>symbol. It is extra useful if you&rsquo;ve got a lengthy script that needs explaining on some lines.<\/p><p>Begin by creating a new bash script:<\/p><pre class=\"wp-block-preformatted\">nano comments.sh<\/pre><p>Then paste in the following:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">#!\/bin\/bash\n# Define a variable named Hostinger\nprovider=\"Hostinger\"\n# Print out the following text\necho 'The best hosting provider is $provider'\n# Print out the following text with $provider variable value\necho \"The best hosting provider is $provider\"<\/pre><div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><a href=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2022\/10\/The-command-line-window-showing-single-line-comment-functionality.-It_s-worth-noting-that-bash-comments-are-not-displayed-with-the-script-output.png\"><img decoding=\"async\" width=\"348\" height=\"87\" src=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2022\/10\/The-command-line-window-showing-single-line-comment-functionality.-It_s-worth-noting-that-bash-comments-are-not-displayed-with-the-script-output.png\" alt=\"The command-line window showing single line comment functionality. It's worth noting that bash comments are not displayed with the script output.\" class=\"wp-image-65843\" srcset=\"https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2022\/10\/The-command-line-window-showing-single-line-comment-functionality.-It_s-worth-noting-that-bash-comments-are-not-displayed-with-the-script-output.png 348w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2022\/10\/The-command-line-window-showing-single-line-comment-functionality.-It_s-worth-noting-that-bash-comments-are-not-displayed-with-the-script-output-300x75.png 300w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2022\/10\/The-command-line-window-showing-single-line-comment-functionality.-It_s-worth-noting-that-bash-comments-are-not-displayed-with-the-script-output-150x38.png 150w\" sizes=\"(max-width: 348px) 100vw, 348px\" \/><\/a><\/figure><\/div><p>Keep in mind that bash comments are only visible on a text editor.<\/p><h3 class=\"wp-block-heading\" id=\"h-6-get-user-input\">6. Get User Input<\/h3><p>To take input from users, we&rsquo;ll use the <strong>read<\/strong> bash command. First, create a new bash shell file:<\/p><pre class=\"wp-block-preformatted\">nano read.sh<\/pre><p>Then, fill it with the script below:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">#!\/bin\/bash\necho \"What is your age?\"\nread age\necho \"Wow, you look younger than $age years old\"<\/pre><p>In the above example, an age value was entered by the user. The output was then printed via the echo command.<\/p><h3 class=\"wp-block-heading\" id=\"h-7-loops\">7. Loops<\/h3><p>A loop is an essential tool in various programming languages. To put it simply, a <a href=\"\/my\/tutorials\/bash-for-loop-guide-and-examples\/\">bash loop<\/a> is a set of instructions that are repeated until a user-specified condition is reached. Start by creating a loop bash program:<\/p><pre class=\"wp-block-preformatted\">nano whileloop.sh<\/pre><p>Then paste in the following:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">#!\/bin\/bash\nn=0\nwhile :\ndo\necho Countdown: $n\n((n++))\ndone<\/pre><p>This will work as a countdown to infinity until you press <strong>CTRL + C<\/strong> to stop the script.<\/p><p>Now that we&rsquo;ve tested the while loop, we can move on to the for loop. Create a bash file for it:<\/p><pre class=\"wp-block-preformatted\">nano forloop.sh<\/pre><p>It should contain the script below:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">#!\/bin\/bash\nfor (( n=2; n&lt;=10; n++ ))\ndo\necho \"$n seconds\"\ndone<\/pre><div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><a href=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2022\/10\/A-bash-script-showcasing-the-for-loop.-It-can-be-seen-that-it-is-very-easy-to-write-scripts-in-bash-as-they-require-only-a-few-elements-to-work.png\"><img decoding=\"async\" width=\"264\" height=\"199\" src=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2022\/10\/A-bash-script-showcasing-the-for-loop.-It-can-be-seen-that-it-is-very-easy-to-write-scripts-in-bash-as-they-require-only-a-few-elements-to-work.png\" alt='A bash script showcasing the \"for\" loop' class=\"wp-image-65845\" srcset=\"https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2022\/10\/A-bash-script-showcasing-the-for-loop.-It-can-be-seen-that-it-is-very-easy-to-write-scripts-in-bash-as-they-require-only-a-few-elements-to-work.png 264w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2022\/10\/A-bash-script-showcasing-the-for-loop.-It-can-be-seen-that-it-is-very-easy-to-write-scripts-in-bash-as-they-require-only-a-few-elements-to-work-150x113.png 150w\" sizes=\"(max-width: 264px) 100vw, 264px\" \/><\/a><\/figure><\/div><p>The script prints out numbers from 2 to 10 while adding the<strong> seconds<\/strong> keyword to it.<\/p><h3 class=\"wp-block-heading\" id=\"h-8-create-an-array\">8. Create an Array<\/h3><p>A <a href=\"\/my\/tutorials\/bash-array\">bash array<\/a> is a data structure designed to store information in an indexed way. It is extra useful if users need to store and retrieve thousands of pieces of data fast. What makes bash arrays special is that unlike any other programming language, they can store different types of elements. For example, you can use a bash array to store both strings and numbers.<\/p><p>Create a new file in the current directory:<\/p><pre class=\"wp-block-preformatted\">nano array.sh<\/pre><p>Combine the freshly learned <strong>for<\/strong> loop with a new indexed array:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">#!\/bin\/bash\n# Create an indexed array\nIndexedArray=(egg burger milk)\n#Iterate over the array to get all the values\nfor i in \"${IndexedArray[@]}\";do echo \"$i\";done<\/pre><div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><a href=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2022\/10\/A-bash-script-to-create-and-print-out-an-array.-Last-line-of-the-script-indicates-a-for-loop-that-prints-out-the-array.png\"><img decoding=\"async\" width=\"249\" height=\"102\" src=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2022\/10\/A-bash-script-to-create-and-print-out-an-array.-Last-line-of-the-script-indicates-a-for-loop-that-prints-out-the-array.png\" alt=\"A bash script to create and print out an array\" class=\"wp-image-65846\" srcset=\"https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2022\/10\/A-bash-script-to-create-and-print-out-an-array.-Last-line-of-the-script-indicates-a-for-loop-that-prints-out-the-array.png 249w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2022\/10\/A-bash-script-to-create-and-print-out-an-array.-Last-line-of-the-script-indicates-a-for-loop-that-prints-out-the-array-150x61.png 150w\" sizes=\"(max-width: 249px) 100vw, 249px\" \/><\/a><\/figure><\/div><p>The script iterates over the <strong>IndexedArray<\/strong> and prints out all the values.<\/p><h3 class=\"wp-block-heading\" id=\"h-9-conditional-statements\">9. Conditional Statements<\/h3><p>The most popular and widely used conditional statement is <strong>if<\/strong>. Even though the if statement is easy to write and understand, it can be used in advanced shell scripts as well.<\/p><p>Begin with a new bash file:<\/p><pre class=\"wp-block-preformatted\">nano if.sh<\/pre><p>Paste the code below in it:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">#!\/bin\/bash\nsalary=1000\nexpenses=800\n#Check if salary and expenses are equal\nif [ $salary == $expenses ];\nthen\n    echo \"Salary and expenses are equal\"\n#Check if salary and expenses are not equal\nelif [ $salary != $expenses ];\nthen\n    echo \"Salary and expenses are not equal\"\nfi<\/pre><p>This script creates two new variables and compares whether they are equal or not.<\/p><h3 class=\"wp-block-heading\" id=\"h-10-functions\">10. Functions<\/h3><p>A bash function is a set of commands that can be reused numerous times throughout a bash script. Create a new file:<\/p><pre class=\"wp-block-preformatted\">nano function.sh<\/pre><p>Then, paste in the following code &ndash; it creates a simple Hello World function.<\/p><pre class=\"wp-block-preformatted\">#!\/bin\/bash\nhello () {\n   echo 'Hello World!'\n}\nhello<\/pre><h3 class=\"wp-block-heading\" id=\"h-11-display-string-length\">11. Display String Length<\/h3><p>There are a couple of ways of counting string length in bash. We&rsquo;ll talk about the simplest. Create a file named <strong>stringlength.sh<\/strong>:<\/p><pre class=\"wp-block-preformatted\">nano stringlength.sh<\/pre><p>Fill it with the following:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">#!\/bin\/bash\n# Create a new string\nmystring=\"lets count the length of this string\"\ni=${#mystring}\necho \"Length: $i\"<\/pre><p>Here, the <strong># <\/strong>operator is used to get the length of the string variable.<\/p><h3 class=\"wp-block-heading\" id=\"h-12-extract-string\">12. Extract String<\/h3><p>If users need to remove unnecessary parts from strings, they can use the Bash string extraction tools. Start by creating a new bash script:<\/p><pre class=\"wp-block-preformatted\">nano extractstring.sh<\/pre><p>The following script has 4 values, 3 of them being strings. In our example, we will extract only the number value. This can be done via the <a href=\"\/my\/tutorials\/linux-cut-command\"><strong>cut<\/strong> command<\/a>. First, we instruct the command that each variable is separated by a comma by using the <strong>-d <\/strong>flag. Then we ask the cut command to extract the 5th value.<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">#!\/bin\/bash\ncut -d , -f 5 &lt;&lt;&lt; \"Website,Domain,DNS,SMTP,5005\"<\/pre><p>In another example, we have a string that is mixed with some numbers. We will use expr substr commands to extract only the <strong>Hostinger <\/strong>text value.<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">#!\/bin\/bash\nexpr substr \"458449Hostinger4132\" 7 9<\/pre><h3 class=\"wp-block-heading\" id=\"h-13-find-and-replace-string\">13. Find and Replace String<\/h3><p>Another useful bash script for strings is <strong>find and replace<\/strong>. Create a file named <strong>findreplace.sh<\/strong>:<\/p><pre class=\"wp-block-preformatted\">nano findreplace.sh<\/pre><p>Then paste in the following bash script:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">#!\/bin\/bash\nfirst=\"I drive a BMW and Volvo\"\nsecond=\"Audi\"\necho \"${first\/BMW\/\"$second\"}\" <\/pre><div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><a href=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2022\/10\/Find-and-replace-script-in-bash.png\"><img decoding=\"async\" width=\"304\" height=\"64\" src=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2022\/10\/Find-and-replace-script-in-bash.png\" alt=\"Find and replace script in bash\" class=\"wp-image-65847\" srcset=\"https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2022\/10\/Find-and-replace-script-in-bash.png 304w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2022\/10\/Find-and-replace-script-in-bash-300x63.png 300w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2022\/10\/Find-and-replace-script-in-bash-150x32.png 150w\" sizes=\"(max-width: 304px) 100vw, 304px\" \/><\/a><\/figure><\/div><p>The find and replace functionality doesn&rsquo;t require any special commands, it can all be done with string manipulation.<\/p><h3 class=\"wp-block-heading\" id=\"h-14-concatenate-strings\">14. Concatenate Strings<\/h3><p>Concatenation is the term used for appending one string to the end of another string. Start by creating <strong>concatenation.sh<\/strong> file.<\/p><pre class=\"wp-block-preformatted\">nano concatenation.sh<\/pre><p>The most simple example would be the following:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">#!\/bin\/bash\nfirststring=\"The secret is...\"\nsecondstring=\"Bash\"\nthirdstring=\"$firststring$secondstring\"\necho \"$thirdstring\"<\/pre><p>The above script will connect the values of <strong>firststring <\/strong>and <strong>secondstring<\/strong> variables creating a whole new <strong>thirdstring<\/strong>.<\/p><p>A more advanced example would look like this:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">#!\/bin\/bash\nfirststring=\"The secret is...\"\nfirststring+=\"Bash\"\necho \"$firststring\"<\/pre><p>The script uses the <strong>+= <\/strong>operator to join the strings. With this method, you can concatenate strings with only one variable.<\/p><h3 class=\"wp-block-heading\" id=\"h-15-check-if-a-number-is-even-or-odd\">15. Check if a Number is Even or Odd<\/h3><p>Odd and even numbers can be easily divided using the <strong>if<\/strong> statement and some simple math. Create a file named <strong>evenoddnumbers.sh<\/strong>:<\/p><pre class=\"wp-block-preformatted\">nano evenoddnumbers.sh<\/pre><p>The script uses the read command to read user input and divides it by 2. If the answer is 0, the number is even.<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">#!\/bin\/bash\nread -p \"Enter a number and I will check if its odd or even \" mynumber\nif [ $((mynumber%2)) -eq 0 ]\nthen\necho \"Your number is even\"\nelse\necho \"Your number is odd.\"\nfi<\/pre><h3 class=\"wp-block-heading\" id=\"h-16-generate-factorial-of-number\">16. Generate Factorial of Number<\/h3><p>The factorial of a number is the result of all positive descending integers. For example, the factorial of 5 would be 120:<\/p><pre class=\"wp-block-preformatted\">5! = 5*4*3*2*1 = 120<\/pre><p>Factorial scrips are very useful for users learning about recursion. Start by <a href=\"\/my\/tutorials\/how-to-run-sh-file-in-linux\">creating a <strong>.sh<\/strong> file<\/a> executable:<\/p><pre class=\"wp-block-preformatted\">factorial.sh<\/pre><p>The following script will ask the user to enter a number they want to get the factorial of and use a <strong>for loop<\/strong> to calculate it.<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">#!\/bin\/bash\necho Enter the number you want to get factorial for\nread mynumber\nfactorial=1\nfor ((i=1;i&lt;=mynumber;i++))\ndo\nfactorial=$(($factorial*$i))\ndone\necho $factorial<\/pre><div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><a href=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2022\/10\/The-command-line-window-displaying-shell-script-for-getting-factorial-of-a-number.png\"><img decoding=\"async\" width=\"382\" height=\"103\" src=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2022\/10\/The-command-line-window-displaying-shell-script-for-getting-factorial-of-a-number.png\" alt=\"The command-line window displaying shell script for getting factorial of a number\" class=\"wp-image-65848\" srcset=\"https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2022\/10\/The-command-line-window-displaying-shell-script-for-getting-factorial-of-a-number.png 382w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2022\/10\/The-command-line-window-displaying-shell-script-for-getting-factorial-of-a-number-300x81.png 300w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2022\/10\/The-command-line-window-displaying-shell-script-for-getting-factorial-of-a-number-150x40.png 150w\" sizes=\"(max-width: 382px) 100vw, 382px\" \/><\/a><\/figure><\/div><h3 class=\"wp-block-heading\" id=\"h-17-create-directories\">17. Create Directories<\/h3><p>It is effortless to create directories in bash unless you need to create a lot of directories quickly. In the following example, we will use the bash script to create a set of directories with the same subdirectories in each.<\/p><p>First, create a file named <strong>directories.sh<\/strong>:<\/p><pre class=\"wp-block-preformatted\">nano directories.sh<\/pre><p>Then paste in the following code:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">#!\/bin\/bash\nmkdir -p {Math,English,Geography,Arts}\/{notes,examresults,portfolio}<\/pre><p>The script creates 4 main directories: <strong>Math<\/strong>, <strong>English<\/strong>, <strong>Geography<\/strong>, and <strong>Arts<\/strong>. The <strong>Notes<\/strong>, <strong>examresults<\/strong>, and <strong>portfolio<\/strong> subdirectories are also created inside each.<\/p><p>If you were to replace the <strong>\/<\/strong> symbol in the middle with <strong>_<\/strong>, the script would look like this:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">#!\/bin\/bash\nmkdir -p {Math,English,Geography,Arts}_{notes,examresults,portfolio}<\/pre><p>Here&rsquo;s the output for it displaying a merge of the two directories:<\/p><div class=\"wp-block-image\"><figure class=\"aligncenter size-full is-resized\"><a href=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2022\/10\/A-bash-script-to-create-a-lot-of-directories-quickly.-The-first-bash-case-statement-indicates-which-interpreter-to-use.pgn_.png\"><img decoding=\"async\" width=\"398\" height=\"87\" src=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2022\/10\/A-bash-script-to-create-a-lot-of-directories-quickly.-The-first-bash-case-statement-indicates-which-interpreter-to-use.pgn_.png\" alt=\"A bash script to create a lot of directories quickly. The first bash case statement indicates which interpreter to use\" class=\"wp-image-65849\" style=\"width:398px;height:87px\" srcset=\"https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2022\/10\/A-bash-script-to-create-a-lot-of-directories-quickly.-The-first-bash-case-statement-indicates-which-interpreter-to-use.pgn_.png 398w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2022\/10\/A-bash-script-to-create-a-lot-of-directories-quickly.-The-first-bash-case-statement-indicates-which-interpreter-to-use.pgn_-300x66.png 300w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2022\/10\/A-bash-script-to-create-a-lot-of-directories-quickly.-The-first-bash-case-statement-indicates-which-interpreter-to-use.pgn_-150x33.png 150w\" sizes=\"(max-width: 398px) 100vw, 398px\" \/><\/a><\/figure><\/div><h3 class=\"wp-block-heading\" id=\"h-18-read-files\">18. Read Files<\/h3><p>In order to read a file in bash, you will need to create a sample file first. Do so with the following command:<\/p><pre class=\"wp-block-preformatted\">nano mysamplefile.txt<\/pre><p>Fill it with some sample data:<\/p><pre class=\"wp-block-preformatted\">Out of all scripting languages, bash is the most popular one. It allows programmers to run scripts effortlessly in a variety of Linux distros.<\/pre><p>Then create the actual script file:<\/p><pre class=\"wp-block-preformatted\">nano readfiles.sh<\/pre><p>Fill it with the following lines:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">#!\/bin\/bash  \nmyvalue=`cat mysamplefile.txt`  \necho \"$myvalue\"<\/pre><p>Running the script results in this output:<\/p><div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><a href=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2022\/10\/A-command-line-window-displays-bash-script-meant-to-read-file-supplied-and-print-its-contents.png\"><img decoding=\"async\" width=\"721\" height=\"78\" src=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2022\/10\/A-command-line-window-displays-bash-script-meant-to-read-file-supplied-and-print-its-contents.png\" alt=\"A command-line window displays bash script meant to read file supplied and print its contents\" class=\"wp-image-65850\" srcset=\"https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2022\/10\/A-command-line-window-displays-bash-script-meant-to-read-file-supplied-and-print-its-contents.png 721w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2022\/10\/A-command-line-window-displays-bash-script-meant-to-read-file-supplied-and-print-its-contents-300x32.png 300w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2022\/10\/A-command-line-window-displays-bash-script-meant-to-read-file-supplied-and-print-its-contents-150x16.png 150w\" sizes=\"(max-width: 721px) 100vw, 721px\" \/><\/a><\/figure><\/div><h3 class=\"wp-block-heading\" id=\"h-19-print-files-with-line-count\">19. Print Files With Line Count<\/h3><p>We&rsquo;ll print a file with its line count. Let&rsquo;s create it first:<\/p><pre class=\"wp-block-preformatted\">nano cars.txt<\/pre><p>In our example, we will fill it with our favorite car brands:<\/p><pre class=\"wp-block-preformatted\">Audi\nBMW\nBentley\nMaserati\nSeat\nVolvo<\/pre><p>Save the file and create a new bash script:<\/p><pre class=\"wp-block-preformatted\">nano printlines.sh<\/pre><p>Then paste in the following code:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">#!\/bin\/bash\nmyfile='cars.txt'\ni=1\nwhile read lines; do\necho \"$i : $lines\"\ni=$((i+1))\ndone &lt; $myfile<\/pre><div class=\"wp-block-image\"><figure class=\"aligncenter size-full is-resized\"><a href=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2022\/10\/The-command-line-window-shows-a-bash-script-that-prints-out-file-contents-with-a-line-number.-Everything-is-done-with-the-help-of-the-while-loop.png\"><img decoding=\"async\" width=\"292\" height=\"305\" src=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2022\/10\/The-command-line-window-shows-a-bash-script-that-prints-out-file-contents-with-a-line-number.-Everything-is-done-with-the-help-of-the-while-loop.png\" alt=\"The command-line window shows a bash script that prints out file contents with a line number\" class=\"wp-image-65851\" style=\"width:292px;height:305px\" srcset=\"https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2022\/10\/The-command-line-window-shows-a-bash-script-that-prints-out-file-contents-with-a-line-number.-Everything-is-done-with-the-help-of-the-while-loop.png 292w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2022\/10\/The-command-line-window-shows-a-bash-script-that-prints-out-file-contents-with-a-line-number.-Everything-is-done-with-the-help-of-the-while-loop-287x300.png 287w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2022\/10\/The-command-line-window-shows-a-bash-script-that-prints-out-file-contents-with-a-line-number.-Everything-is-done-with-the-help-of-the-while-loop-144x150.png 144w\" sizes=\"(max-width: 292px) 100vw, 292px\" \/><\/a><\/figure><\/div><p>The file contents of <strong>cars.txt<\/strong> match the printout of the while loop script.<\/p><h3 class=\"wp-block-heading\" id=\"h-20-delete-files\">20. Delete Files<\/h3><p>To delete an existing file, you can use an <strong>if<\/strong> statement to check if the file exists and instruct the bash script to remove it. Start by creating the bash script file:<\/p><pre class=\"wp-block-preformatted\">nano deletefiles.sh<\/pre><p>The following script will create a new file named <strong>cars.txt<\/strong>, and then &ndash; with the help of the if statement &ndash; check if it exists and delete it.<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">#!\/bin\/bash\nmyfile='cars.txt'\ntouch $myfile\nif [ -f $myfile ]; then\n   rm cars.txt\n   echo \"$myfile deleted\"\nfi<\/pre><h3 class=\"wp-block-heading\" id=\"h-21-test-if-file-exists\">21. Test if File Exists<\/h3><p>In order to check if a given file exists, users can perform conditional tests. In this case, we&rsquo;ll use<strong> <\/strong>an<strong> if <\/strong>statement with a <strong>-f<\/strong> flag. The flag checks if a given file exists and is a regular file. Start by creating the script file:<\/p><pre class=\"wp-block-preformatted\">nano exists.sh<\/pre><p>Copy and paste the following script:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">#!\/bin\/bash\nMyFile=cars.txt\nif [ -f \"$MyFile\" ]; then\necho \"$MyFile exists.\"\nelse \necho \"$MyFile does not exist.\"\nfi<\/pre><p>Running the script results in the following output:<\/p><div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><a href=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2022\/10\/Bash-script-to-check-if-a-given-file-exists.-Mind-the-last-fi-bash-line-it-indicates-that-all-if-conditional-statements-ends.png\"><img decoding=\"async\" width=\"258\" height=\"68\" src=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2022\/10\/Bash-script-to-check-if-a-given-file-exists.-Mind-the-last-fi-bash-line-it-indicates-that-all-if-conditional-statements-ends.png\" alt=\"Bash script to check if a given file exists. Mind the last fi bash line, it indicates that all if conditional statements ends\" class=\"wp-image-65852\" srcset=\"https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2022\/10\/Bash-script-to-check-if-a-given-file-exists.-Mind-the-last-fi-bash-line-it-indicates-that-all-if-conditional-statements-ends.png 258w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2022\/10\/Bash-script-to-check-if-a-given-file-exists.-Mind-the-last-fi-bash-line-it-indicates-that-all-if-conditional-statements-ends-150x40.png 150w\" sizes=\"(max-width: 258px) 100vw, 258px\" \/><\/a><\/figure><\/div><h3 class=\"wp-block-heading\" id=\"h-22-check-inodes-and-disk-usage\">22. Check Inodes and Disk Usage<\/h3><p>Inodes represent data units on a physical or virtual server. Each text file, video, folder, HTML file, or script is 1 inode. We&rsquo;ll check how many inodes there are in a directory, as too many can cause the system to slow down significantly. Start by creating the bash script:<\/p><pre class=\"wp-block-preformatted\">nano inodesdisk.sh<\/pre><p>Paste in the following code &ndash; it will check inodes in descending order as well as show disk usage in a given directory:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">#!\/bin\/bash\nfind . -printf \"%hn\" | cut -d\/ -f-2 | sort | uniq -c | sort -rn\ndu -shc * | sort -rh<\/pre><p>It will look something like this on the command line:<\/p><div class=\"wp-block-image\"><figure class=\"aligncenter size-full is-resized\"><a href=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2022\/10\/A-command-line-window-showing-bash-script-to-count-inodes-and-display-disk-usage.png\"><img decoding=\"async\" width=\"298\" height=\"294\" src=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2022\/10\/A-command-line-window-showing-bash-script-to-count-inodes-and-display-disk-usage.png\" alt=\"A command-line window showing bash script to count inodes and display disk usage\" class=\"wp-image-65853\" style=\"width:298px;height:294px\" srcset=\"https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2022\/10\/A-command-line-window-showing-bash-script-to-count-inodes-and-display-disk-usage.png 298w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2022\/10\/A-command-line-window-showing-bash-script-to-count-inodes-and-display-disk-usage-150x148.png 150w\" sizes=\"(max-width: 298px) 100vw, 298px\" \/><\/a><\/figure><\/div><p>The given directory has 15 inodes and all files take up 20KB.<\/p><h3 class=\"wp-block-heading\" id=\"h-23-send-email-example\">23. Send Email Example<\/h3><p>It is possible to send mail via bash scripts as well. In order to do so, users first need a functional mail transport agent. On Ubuntu 22.04, the installation command will look like this:<\/p><pre class=\"wp-block-preformatted\">sudo apt-get install mailutils<\/pre><p>Once you&rsquo;ve taken care of the mail transport agent installation, create a new bash script:<\/p><pre class=\"wp-block-preformatted\">nano mail.sh<\/pre><p>Here are its contents:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">#!\/bin\/bash\nRecipient=\"myawesomeinbox@domain.tld\"\nMysubject=\"Regarding our talk\"\nMymessage=\"Call me\"\n`mail -s $Mysubject $Recipients &lt;&lt;&lt; $Mymessage`<\/pre><p><div><p class=\"important\"><strong>Important!<\/strong> The above script is meant for testing purposes only as it won&rsquo;t work normally with services like Gmail. We recommend using <a href=\"\/my\/tutorials\/send-emails-using-php-mail\" target=\"&rdquo;_blank&rdquo;\" rel=\"noopener\">PHPMailer<\/a> instead. <\/p><\/div>\n\n\n\n<\/p><h3 class=\"wp-block-heading\" id=\"h-24-update-packages\">24. Update Packages<\/h3><p>Keeping the system and all of its applications up to date is crucial. You can create a bash script to do it. Mind that this script requires root privileges. First, create the bash script file:<\/p><pre class=\"wp-block-preformatted\">nano maintenance.sh<\/pre><p>Fill it with these lines:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">#!\/bin\/bash\napt-get update\napt-get upgrade<\/pre><p>Make sure to preface the script with the sudo command when you run it:<\/p><pre class=\"wp-block-preformatted\">sudo bash maintenance.sh<\/pre><p><div><p class=\"important\"><strong>Important!<\/strong>Apt package manager is used on Debian based distributions only. If you&rsquo;re using a different distribution, make sure to update the command accordingly. <\/p><\/div>\n\n\n\n<\/p><h3 class=\"wp-block-heading\" id=\"h-25-show-server-information\">25. Show Server Information<\/h3><p>The following script will list a few important server metrics: system&rsquo;s date, uptime as well as memory, and network usage statistics. We&rsquo;ll start by creating a new file for it:<\/p><pre class=\"wp-block-preformatted\">nano system.sh<\/pre><p>Here&rsquo;s the script for it:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">#!\/bin\/bash\necho \"Date\"\ndate\necho \"Uptime\"\nuptime\necho \"Memory Usage\"\nfree -m\necho \"Network Usage\"\nip a<\/pre><div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><a href=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2022\/10\/A-bash-script-that-displays-general-information-about-the-system-date-uptime-memory-and-network-usage.png\"><img decoding=\"async\" width=\"826\" height=\"323\" src=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2022\/10\/A-bash-script-that-displays-general-information-about-the-system-date-uptime-memory-and-network-usage.png\" alt=\"A bash script that displays general information about the system: date, uptime, memory, and network usage\" class=\"wp-image-65854\" srcset=\"https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2022\/10\/A-bash-script-that-displays-general-information-about-the-system-date-uptime-memory-and-network-usage.png 826w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2022\/10\/A-bash-script-that-displays-general-information-about-the-system-date-uptime-memory-and-network-usage-300x117.png 300w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2022\/10\/A-bash-script-that-displays-general-information-about-the-system-date-uptime-memory-and-network-usage-150x59.png 150w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2022\/10\/A-bash-script-that-displays-general-information-about-the-system-date-uptime-memory-and-network-usage-768x300.png 768w\" sizes=\"(max-width: 826px) 100vw, 826px\" \/><\/a><\/figure><\/div><h2 class=\"wp-block-heading\" id=\"h-conclusion\">Conclusion<\/h2><p>Linux bash scripting is extremely useful for users looking to convert complex sequences of commands into one script effortlessly. Even if you are a casual user, you can make your life easier by automating simple tasks like updating packages or putting your system to sleep.<\/p><p>In this tutorial, we&rsquo;ve covered the basics of bash scripting and how can it be used to automate tasks and increase productivity. We&rsquo;ve also listed 25 of the most common simple bash scripts that you can try out.<\/p><p>We hope this article has helped you understand bash scripting better. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Bash or Bourne-again shell is one of the most popular shells and command languages for Linux VPS enthusiasts. It was first released in 1989 and was used as the default shell for most Linux distributions ever since. Bash scripting allows users and system administrators to automate processes and save hundreds of hours of manual work. [&#8230;]<\/p>\n<p><a class=\"btn btn-secondary understrap-read-more-link\" href=\"\/my\/tutorials\/bash-script-example\">Read More&#8230;<\/a><\/p>\n","protected":false},"author":279,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"rank_math_title":"25 Easy Bash Script Examples To Get You Started","rank_math_description":"If you want to learn the fundamentals of bash scripting, read this article to find 25 bash scripting examples.","rank_math_focus_keyword":"bash script example","footnotes":""},"categories":[22639],"tags":[],"class_list":["post-65838","post","type-post","status-publish","format-standard","hentry","category-vps"],"hreflangs":[{"locale":"en-US","link":"https:\/\/www.hostinger.com\/tutorials\/bash-script-example","default":0},{"locale":"id-ID","link":"https:\/\/www.hostinger.com\/id\/tutorial\/contoh-bash-script","default":0},{"locale":"en-UK","link":"https:\/\/www.hostinger.com\/uk\/tutorials\/bash-script-example","default":0},{"locale":"en-MY","link":"https:\/\/www.hostinger.com\/my\/tutorials\/bash-script-example","default":0},{"locale":"en-PH","link":"https:\/\/www.hostinger.com\/ph\/tutorials\/bash-script-example","default":0},{"locale":"en-IN","link":"https:\/\/www.hostinger.com\/in\/tutorials\/bash-script-example","default":0},{"locale":"en-CA","link":"https:\/\/www.hostinger.com\/ca\/tutorials\/bash-script-example","default":0},{"locale":"en-AU","link":"https:\/\/www.hostinger.com\/au\/tutorials\/bash-script-example","default":0},{"locale":"en-NG","link":"https:\/\/www.hostinger.com\/ng\/tutorials\/bash-script-example","default":0}],"_links":{"self":[{"href":"https:\/\/www.hostinger.com\/my\/tutorials\/wp-json\/wp\/v2\/posts\/65838","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hostinger.com\/my\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hostinger.com\/my\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/my\/tutorials\/wp-json\/wp\/v2\/users\/279"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/my\/tutorials\/wp-json\/wp\/v2\/comments?post=65838"}],"version-history":[{"count":12,"href":"https:\/\/www.hostinger.com\/my\/tutorials\/wp-json\/wp\/v2\/posts\/65838\/revisions"}],"predecessor-version":[{"id":126364,"href":"https:\/\/www.hostinger.com\/my\/tutorials\/wp-json\/wp\/v2\/posts\/65838\/revisions\/126364"}],"wp:attachment":[{"href":"https:\/\/www.hostinger.com\/my\/tutorials\/wp-json\/wp\/v2\/media?parent=65838"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hostinger.com\/my\/tutorials\/wp-json\/wp\/v2\/categories?post=65838"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hostinger.com\/my\/tutorials\/wp-json\/wp\/v2\/tags?post=65838"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}