{"id":15650,"date":"2021-09-06T06:36:00","date_gmt":"2021-09-06T06:36:00","guid":{"rendered":"https:\/\/www.hostinger.com\/tutorials\/?p=15650"},"modified":"2024-06-25T11:21:41","modified_gmt":"2024-06-25T11:21:41","slug":"guide-to-bash-functions-with-examples","status":"publish","type":"post","link":"\/my\/tutorials\/bash-scripting-tutorial","title":{"rendered":"Bash Scripting Tutorial for Beginners"},"content":{"rendered":"<p>If you&rsquo;ve ever used a Linux operating system, for example, a Linux-based VPS, you may have heard of bash. It&rsquo;s a Unix shell that reads and executes various commands.<\/p><p>When you need to run several bash commands, you don&rsquo;t have to execute them manually one at a time. Instead, it&rsquo;s possible to create a script file that contains bash functions to run those commands.<\/p><p>It may sound complicated, but by learning its basics, you will understand the bash scripting language and find out how it can help your workflow.<\/p><p>This article will cover the process of bash scripting. We&rsquo;ll go over everything from bash commands to running a bash program on a Linux terminal.<\/p><p><strong>A Brief Overview of Bash Scripting:<\/strong><\/p><figure tabindex=\"0\" class=\"wp-block-table\"><table><tbody><tr><td>Required Knowledge<\/td><td>Basic understanding of Linux, command line usage<\/td><\/tr><tr><td>Privileges Required<\/td><td>User privileges, root for certain operations<\/td><\/tr><tr><td>Difficulty<\/td><td>Beginner to Intermediate<\/td><\/tr><tr><td>Main Goal<\/td><td>Introduction to Bash scripting, basic commands, script examples<\/td><\/tr><tr><td>OS Compatibility<\/td><td>Any Linux-based system<\/td><\/tr><\/tbody><\/table><\/figure><p class=\"has-text-align-center\"><a href=\"https:\/\/assets.hostinger.com\/content\/tutorials\/pdf\/Bash-Cheat-Sheet.pdf\" target=\"_blank\" rel=\"noreferrer noopener\">Download comprehensive bash cheat sheet<\/a><\/p><h2 class=\"wp-block-heading\" id=\"h-what-is-bash\">What Is Bash<\/h2><p>Bash, short for <strong>Bourne-Again Shell<\/strong>, is a Unix shell and a command language interpreter. It reads shell commands and interacts with the operating system to execute them.<\/p><p>To fully understand bash shell scripting, you need to know two concepts &ndash; <strong>shell<\/strong> and <strong>scripting<\/strong>.<\/p><p>Shell is a macro processor that uses commands to interact with the operating system. This means that it can retrieve, process, and store information on a computer.<\/p><p>Meanwhile, scripting is the process of compiling shell commands into a new file using a text editor.<\/p><p>When you write bash in a text editor, you&rsquo;re compiling <strong>bash commands<\/strong> or<strong> bash functions<\/strong> &ndash; a set of commands that can be called numerous times by only using the function name. The text is then saved as an executable <strong>bash script<\/strong> file with the <strong>.sh <\/strong>extension.&nbsp;<\/p><?xml encoding=\"utf-8\" ?><figure class=\"wp-block-image size-large\"><a href=\"\/my\/vps-hosting\" target=\"_blank\" rel=\"noreferrer noopener\"><img decoding=\"async\" width=\"1024\" height=\"300\" src=\"https:\/\/www.hostinger.com\/tutorials\/wp-content\/uploads\/sites\/2\/2023\/02\/VPS-hosting-banner-1024x300.png\" alt=\"\" class=\"wp-image-77934\" srcset=\"https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2023\/02\/VPS-hosting-banner.png 1024w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2023\/02\/VPS-hosting-banner-300x88.png 300w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2023\/02\/VPS-hosting-banner-150x44.png 150w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2023\/02\/VPS-hosting-banner-768x225.png 768w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure><h2 class=\"wp-block-heading\" id=\"h-why-use-bash-scripting\">Why Use Bash Scripting<\/h2><p>Bash scripts can help with your workflow as they compile many lengthy commands into a single executable script file.<\/p><p>For example, if you have multiple commands that you have to run at a specific time interval, you can compile a bash script instead of typing and executing the commands manually one by one. You only need to execute the script file when it&rsquo;s necessary.<\/p><p>Here are some other advantages of using bash scripts:<\/p><ul class=\"wp-block-list\">\n<li><strong>Well-structured commands <\/strong>&ndash; structure the commands in a sequence so that every time you execute the script, it will run in the right order.<\/li>\n\n\n\n<li><strong>Task automation <\/strong>&ndash; automate the script execution at any defined time using cron&rsquo;s time-based scheduler.<\/li>\n\n\n\n<li><strong>Transparency <\/strong>&ndash; people can check the content of a script since it&rsquo;s in a readable text file. However, if you run the commands using another program written in a different programming language, such as C++, you&rsquo;ll need to access the source code.<\/li>\n\n\n\n<li><strong>Transferable <\/strong>&ndash; if you transfer a script to other Linux distributions, it&rsquo;ll still work, providing that shell commands are available on that particular operating system.<\/li>\n<\/ul><p>\n\n\n<div class=\"protip\">\n                    <h4 class=\"title\">Pro Tip<\/h4>\n                    <p>Linux has a bash shell command manual. It contains descriptions of all technical terms and standard shell variables. Type and execute the <strong>man<\/strong> bash command to display the manual on the terminal.<\/p>\n                <\/div>\n\n\n\n<\/p><h2 class=\"wp-block-heading\" id=\"h-get-familiar-with-bash-commands\">Get Familiar With Bash Commands<\/h2><p>Bash is available on almost all types of Unix-based operating systems and doesn&rsquo;t require a separate installation. You will need a Linux command line, also known as the Linux terminal. It&rsquo;s a program that contains the shell and lets you execute bash scripts.&nbsp;<\/p><p>Use this command to check the list of available shells on your Unix operating system:<\/p><pre class=\"wp-block-preformatted\">cat \/etc\/shells<\/pre><p>The output should show a list like this:<\/p><pre class=\"wp-block-preformatted\">\/bin\/bash\n\/bin\/sh\n\/bin\/tcsh\n\/bin\/csh<\/pre><p>Each bash shell script needs to start with <strong>#! <\/strong>followed by the absolute path to the bash interpreter. To view the path, enter this command:<\/p><pre class=\"wp-block-preformatted\">which bash<\/pre><p>It should produce the following output:<\/p><pre class=\"wp-block-preformatted\">\/bin\/bash<\/pre><p>This is the standard path to the bash interpreter on most Unix operating systems. To let the shell know that it should run commands using the bash interpreter, start the script with this line:<\/p><pre class=\"wp-block-preformatted\">#!\/bin\/bash<\/pre><p>\n\n\n<div><p class=\"important\"><strong>Important!<\/strong> If you want to run bash scripts on a virtual private server, connect to it via an SSH client. <\/p><\/div>\n\n\n\n<\/p><p>The next step is to write and compile the commands in a <strong>.sh<\/strong> file using a text editor. You will need a Unix text editor such as <a href=\"https:\/\/www.vim.org\/\" target=\"_blank\" rel=\"noopener\">VIM<\/a> or <a href=\"https:\/\/www.nano-editor.org\/\" target=\"_blank\" rel=\"noopener\">GNU Nano<\/a>. In this tutorial, we&rsquo;ll use the <strong>Nano <\/strong>text editor to create the file by inputting this command:<\/p><pre class=\"wp-block-preformatted\">nano function.sh<\/pre><p>This will open a new <strong>.sh<\/strong> file for you to edit. Begin by writing <strong>#!\/bin\/bash<\/strong> followed by bash commands.<\/p><p>\n\n\n<div><p class=\"important\"><strong>Important!<\/strong> Once you&rsquo;re done using the Nano text editor, press <strong>Ctrl+X<\/strong> to close it, then press <strong>Y<\/strong> and <strong>Enter<\/strong> to save the changes.<\/p><\/div>\n\n\n\n<\/p><h2 class=\"wp-block-heading\" id=\"h-basic-bash-commands-for-your-first-bash-script\">Basic Bash Commands for Your First Bash Script<\/h2><p>In order to successfully create your first bash script, you need to understand the essential bash commands. They are the main elements of a script, and you must know what they do and how to write them properly.<\/p><p>There are a lot of bash commands on Linux. To start things off, we&rsquo;ll cover seven basic ones.<\/p><h3 class=\"wp-block-heading\" id=\"h-1-comments\">1. Comments<\/h3><p>Comments feature a description on certain lines in the script. The terminal doesn&rsquo;t parse comments during execution, so they won&rsquo;t affect the output.<\/p><p>There are two ways to add comments to a script. The first method is by typing <strong>#<\/strong> at the beginning of a single-line comment.<\/p><pre class=\"wp-block-preformatted\">#!\/bin\/bash\n#Command below prints a Hello World text\necho &ldquo;Hello, world!&rdquo;<\/pre><p>The second method is by using <strong>:<\/strong> followed by <strong>&lsquo;<\/strong>. This method works for multiple-line comments.<\/p><pre class=\"wp-block-preformatted\">#!\/bin\/bash\nread a\n: &lsquo; \nThe following commands prints\nHello, world!\n&lsquo;\necho &ldquo;Hello, World!&rdquo;\n<\/pre><h3 class=\"wp-block-heading\" id=\"h-2-variables\">2. Variables<\/h3><p>Variables are symbols that represent a character, strings of characters, or numbers. You only need to type the variable name in a command line to use the defined strings or numbers.<\/p><p>To assign a variable, type the variable name and the string value like here:<\/p><pre class=\"wp-block-preformatted\">testvar=&ldquo;This is a test variable&rdquo;<\/pre><p>In this case, <strong>testvar<\/strong> is the variable name, and <strong>This is a test variable <\/strong>is the string value. When assigning a variable, we recommend using a variable name that&rsquo;s easy to remember and represents its value.<\/p><p>To read the variable value in the command line, use the <strong>$ <\/strong>symbol before the variable name. Take a look at the example below:<\/p><pre class=\"wp-block-preformatted\">#!\/bin\/bash\ntestvar=&ldquo;This is a test variable&rdquo;\necho $testvar<\/pre><p>The second command line uses <strong>echo<\/strong> to print out the value of <strong>testvar<\/strong>. The output of that script will be:<\/p><pre class=\"wp-block-preformatted\">This is a test variable<\/pre><p>Let&rsquo;s take a look at how you can enter a string value by using the <strong>read<\/strong> command and make the script compare two string values from different variables:<\/p><pre class=\"wp-block-preformatted\">#!\/bin\/bash<br>echo &ldquo;Enter a number&rdquo;&nbsp;<br>read a #The user input in this command line will be stored as variable a<br>b=50 #The value of variable b<br>if [[$a -eq $b]]<br>then<br>echo &ldquo;Same number&rdquo;<br>else<br>echo &ldquo;Different number&rdquo;<br>fi<\/pre><p>The output of that script should be as follows:<\/p><pre class=\"wp-block-preformatted\">Enter a number<br>20<br>Different number<\/pre><p>Note that <strong>line 3<\/strong> is the value that becomes variable <strong>a<\/strong>.<\/p><p>The script compares the variable <strong>a<\/strong> with the value <strong>20<\/strong> and the variable <strong>b<\/strong> with the value <strong>50<\/strong>. Since the values are different, the script prints out <strong>Different number<\/strong>.<\/p><p>However, if the user inputs <strong>50<\/strong>, this will be the output:<\/p><pre class=\"wp-block-preformatted\">Enter a number<br>50<br>Same number<\/pre><p>This example also uses conditional statements, which we will discuss later.<\/p><h3 class=\"wp-block-heading\" id=\"h-3-echo-command\">3. echo Command<\/h3><p><strong>echo<\/strong> is a well-known command used in many programming languages. There are various options you can use with echo to print the output on the terminal.<\/p><p>The first and most common use of echo is to output standard text:<\/p><pre class=\"wp-block-preformatted\">#!\/bin\/bash<br>echo &ldquo;Hello, world!&rdquo;<\/pre><p>The output of that command is <strong>Hello, World! <\/strong>By default, when using the echo command like this, the terminal will input a new line underneath that. If you want to echo an output without a new line, you can do so by using <strong>-n<\/strong>.<\/p><pre class=\"wp-block-preformatted\">#!\/bin\/bash<br>echo -n &ldquo;Hello, world!&rdquo;<\/pre><p>Use the option <strong>\\n <\/strong>to introduce a line break into the output. To enable the <strong>backslash (\\)<\/strong>, you need to include <strong>-e<\/strong>.<\/p><pre class=\"wp-block-preformatted\">#!\/bin\/bash<br>echo -e &ldquo;Hello, \\nworld!&rdquo;<\/pre><p>The output of that command will look like this:<\/p><pre class=\"wp-block-preformatted\">Hello,<br>world!<\/pre><p>The option <strong>\\t <\/strong>adds a horizontal tab space:<\/p><pre class=\"wp-block-preformatted\">#!\/bin\/bash<br>echo -e &ldquo;\\tHello, world!&rdquo;<\/pre><p>This command&rsquo;s output will indent the text to the right:<\/p><pre class=\"wp-block-preformatted\">        Hello, world!<\/pre><p>You can also combine several options. For example, combine <strong>\\n <\/strong>and <strong>\\t <\/strong>to break the text into lines and indent it to the right:<\/p><pre class=\"wp-block-preformatted\">#!\/bin\/bash<br>echo -e &ldquo;\\n\\tHello, \\n\\tworld!&rdquo;<\/pre><p>The output of that command will look like this:<\/p><pre class=\"wp-block-preformatted\">      Hello,\n      world!&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/pre><h3 class=\"wp-block-heading\" id=\"h-4-functions\">4. Functions<\/h3><p>A function compiles a set of commands into a group. If you need to execute the command again, simply write the function instead of the whole set of commands.<\/p><p>There are several ways of writing functions.<\/p><p>The first way is by starting with the function name and following it with parentheses and brackets:<\/p><pre class=\"wp-block-preformatted\">function_name () {<br>first command<br>second command<br>}<\/pre><p>Or, if you want to write it in a single line:<\/p><pre class=\"wp-block-preformatted\">function_name () { first command; second command; }<\/pre><p>The second method to write a function is using the reserved word <strong>function <\/strong>followed by the function name. This eliminates the need for parentheses:<\/p><pre class=\"wp-block-preformatted\">function function_name {<br>first command<br>second command<br>}<\/pre><p>This method also has a single-line version:<\/p><pre class=\"wp-block-preformatted\">function function_name { first command; second command; }<\/pre><p>For example, we can write two functions with multiple <strong>echo<\/strong> commands:<\/p><pre class=\"wp-block-preformatted\">#!\/bin\/bash<br>hello_world () {<br>echo &ldquo;Hello, World!&rdquo;<br>echo &ldquo;This is a test function&rdquo;<br>}<br>print_message () {<br>echo &ldquo;Let&rsquo;s learn bash programming&rdquo;<br>echo &ldquo;Enjoy this tutorial&rdquo;<br>}<\/pre><p>Note that writing the functions as in the example above only defines them and doesn&rsquo;t execute the contained commands. To execute a function, enter its name into the command line.<\/p><p>Now, let&rsquo;s use the two examples above in a complete bash function, including its execution:<\/p><pre class=\"wp-block-preformatted\">#!\/bin\/bash<br>#Define a hello world function<br>hello_world () {<br>echo &ldquo;Hello, World!&rdquo;<br>echo &ldquo;This is a test function&rdquo;<br>}<br>#Define a print message function<br>print_message () {<br>echo &ldquo;Let&rsquo;s learn bash programming&rdquo;<br>echo &ldquo;Enjoy this tutorial&rdquo;<br>}<br>#Execute the hello world function<br>hello_world<br>#Execute the print message function<br>print_message<\/pre><p>This is the output of the script above:<\/p><pre class=\"wp-block-preformatted\">Hello, World!<br>This is a test function<br>Let&rsquo;s learn bash programming<br>Enjoy this tutorial<\/pre><h3 class=\"wp-block-heading\" id=\"h-5-loops\">5. Loops<\/h3><p><a href=\"\/my\/tutorials\/bash-for-loop-guide-and-examples\/\">Loop bash commands<\/a> are useful if you want to execute commands multiple times. There are three types of them you can run in bash &ndash; <strong>for<\/strong>, <strong>while<\/strong>, and <strong>until<\/strong>.<\/p><p>The <strong>for <\/strong>loop runs the command for a list of items:<\/p><pre class=\"wp-block-preformatted\">#!\/bin\/bash<br>for item in [list]<br>do<br>[commands]<br>done<\/pre><p>The following example uses a <strong>for <\/strong>loop to print all the days of the week:<\/p><pre class=\"wp-block-preformatted\">#!\/bin\/bash<br>for days in Monday Tuesday Wednesday Thursday Friday Saturday Sunday<br>do<br>echo &ldquo;Day: $days&rdquo;<br>done<\/pre><p>On <strong>line 2<\/strong>, <strong>&ldquo;days&rdquo;<\/strong> automatically becomes a variable, with the values being the day names that follow. Then, in the <strong>echo<\/strong> command, we use the <strong>$<\/strong> symbol to call the variable values.<\/p><p>The output of that script will be as follows:<\/p><pre class=\"wp-block-preformatted\">Day: Monday<br>Day: Tuesday<br>Day: Wednesday<br>Day: Thursday<br>Day: Friday<br>Day: Saturday<br>Day: Sunday<\/pre><p>Notice that even with just one command line in the loop script, it prints out seven <strong>echo <\/strong>outputs.<\/p><p>The next type of loop is <strong>while<\/strong>. The script will evaluate a condition. If the condition is <strong>true<\/strong>, it will keep executing the commands until the output no longer meets the defined condition.<\/p><pre class=\"wp-block-preformatted\">#!\/bin\/bash<br>while [condition]<br>do<br>[commands]<br>done<\/pre><p>Let&rsquo;s take a look at a simple example that involves a variable and increment operator, denoted as <strong>((++))<\/strong>:<\/p><pre class=\"wp-block-preformatted\">#!\/bin\/bash<br>i=0<br>while [ $i -le 5 ]<br>do&nbsp;<br>echo $i<br>((i++))<br>done<\/pre><p>The variable starts with a <strong>0 <\/strong>value, and the increment operator will increase it by one. The condition set is less than or equal to five, so the command will keep iterating until the output reaches five. The output of that script will be as follows:<\/p><pre class=\"wp-block-preformatted\">0<br>1<br>2<br>3<br>4<br>5<\/pre><p>The last type of loop, <strong>until<\/strong>, is the opposite of <strong>while<\/strong>. It will iterate the command until the condition becomes true.<\/p><p>If we want the same output as the <strong>while<\/strong> example above using the <strong>until<\/strong> loop, we can write the script like this:<\/p><pre class=\"wp-block-preformatted\">#!\/bin\/bash<br>i=0<br>until [ $i -gt 5 ]<br>do&nbsp;<br>echo $i<br>((i++))<br>done<\/pre><p>Now, this command will iterate until the output value reaches five. The output will be the same as our example with the <strong>while<\/strong> loop:<\/p><pre class=\"wp-block-preformatted\">0<br>1<br>2<br>3<br>4<br>5<\/pre><h3 class=\"wp-block-heading\" id=\"h-6-conditional-statements\">6. Conditional Statements<\/h3><p>Many programming languages, including bash, use conditional statements like <strong>if<\/strong>, <strong>then<\/strong>, and <strong>else <\/strong>for decision-making. They execute commands and print out outputs depending on the conditions.<\/p><p>The <strong>if <\/strong>statement is followed by a conditional expression. After that, it&rsquo;s followed by <strong>then <\/strong>and the command to define the output of the condition. The script will execute the command if the condition expressed in the <strong>if <\/strong>statement is true.<\/p><p>However, if you want to execute a different command if the condition is false, add an <strong>else <\/strong>statement to the script and follow it with the command.<\/p><p>Let&rsquo;s take a look at simple <strong>if<\/strong>, <strong>then<\/strong>, and <strong>else <\/strong>statements. Before the statement, we will include a variable so the user can input a value:<\/p><pre class=\"wp-block-preformatted\">#!\/bin\/bash<br>echo &ldquo;Enter a number&rdquo;<br>read num<br>if [[$num -gt 10]]<br>then<br>echo &ldquo;The number is greater than 10&rdquo;<br>else<br>echo &ldquo;The number is not greater than 10&rdquo;<\/pre><h3 class=\"wp-block-heading\" id=\"h-7-reading-and-writing-files\">7. Reading and Writing Files<\/h3><p>There are several methods to read a file, with the <a href=\"\/my\/tutorials\/linux-cat-command-tutorial-and-examples\/\"><strong>cat <\/strong>command<\/a> being the most popular one. Note that this command reads the <strong>whole<\/strong> file content.<\/p><p>To read the content line by line, use the <strong>read <\/strong>command and a loop. Before writing a script to read a file, make sure that the file exists first.<\/p><p>In the following example, we have a <strong>to-do.txt <\/strong>file that contains a to-do list:<\/p><pre class=\"wp-block-preformatted\">Reply email<br>Finish report<br>Call clients<br>Team evaluation<\/pre><p>We&rsquo;ll use the <strong>cat<\/strong> and <strong>read <\/strong>commands in our bash function to read and print the content of the <strong>to-do.txt <\/strong>file. The first part of the script will use the <strong>cat <\/strong>command, while the second part will use the <strong>read<\/strong> command in a loop.<\/p><pre class=\"wp-block-preformatted\">#!\/bin\/bash<br>echo &ldquo;Reading the file using cat command&rdquo;<br>content=&rsquo;cat to-do.txt&rsquo;<br>echo $content<br>echo &ldquo;Reading the file using read command and loop&rdquo;<br>filename=&rsquo;to-do.txt&rsquo;<br>while read line<br>do<br>echo $line<br>done&lt;$filename<\/pre><p>The output of the script will be as follows:<\/p><pre class=\"wp-block-preformatted\">Reading the file using cat command<br>Reply email Finish report Call clients Team evaluation<br>Reading the file using read command and loop<br>Reply email<br>Finish report<br>Call clients<br>Team evaluation<\/pre><p>To write a command output into a file, use the redirection operators, represented with the <strong>&gt; <\/strong>and <strong>&gt;&gt; <\/strong>symbols, and follow them with the file name:&nbsp;<\/p><pre class=\"wp-block-preformatted\">output &gt; filename<br>output &gt;&gt; filename<\/pre><p>Be careful when choosing the operator. If the file exists, the <strong>&gt;<\/strong> operator will overwrite its content with a zero-length string. It means you&rsquo;ll lose the existing file content. If the inputted file name doesn&rsquo;t exist, it will create it.<\/p><p>The <strong>&gt;&gt; <\/strong>operator, on the other hand, will add the output to the given file.&nbsp;<\/p><p>Here&rsquo;s a simple redirection to write the output into a text file:<\/p><pre class=\"wp-block-preformatted\">echo &ldquo;Hello, world!&rdquo; &gt;&gt; hello_world.txt<\/pre><p>Redirection also works with the <strong>read<\/strong> command to write any user input. This script example will add the input value into the <strong>name.txt<\/strong> file:<\/p><pre class=\"wp-block-preformatted\">#!\/bin\/bash<br>echo &ldquo;Enter your name&rdquo;<br>read Name<br>echo $Name &gt;&gt; name.txt<\/pre><p>Because the script redirects the variable output into the file, you won&rsquo;t see any output printed. To see the output by printing the file content, add the following command line to read the file:<\/p><pre class=\"wp-block-preformatted\">echo &lsquo;cat name.txt&rsquo;<\/pre><p>Make sure that you <a href=\"\/my\/tutorials\/vps\/change-linux-permissions-and-owners\">change the file permission using the chmod command<\/a> to make it readable and writable, preventing the <strong>permission denied <\/strong>error. If you want to add the output to existing files, make sure to type in the correct file names.<\/p><?xml encoding=\"utf-8\" ?><figure class=\"wp-block-image size-large\"><a href=\"\/my\/vps-hosting\" target=\"_blank\" rel=\"noreferrer noopener\"><img decoding=\"async\" width=\"1024\" height=\"300\" src=\"https:\/\/www.hostinger.com\/tutorials\/wp-content\/uploads\/sites\/2\/2023\/02\/VPS-hosting-banner-1024x300.png\" alt=\"\" class=\"wp-image-77934\" srcset=\"https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2023\/02\/VPS-hosting-banner.png 1024w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2023\/02\/VPS-hosting-banner-300x88.png 300w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2023\/02\/VPS-hosting-banner-150x44.png 150w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2023\/02\/VPS-hosting-banner-768x225.png 768w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure><h2 class=\"wp-block-heading\" id=\"h-4-easy-functions-to-try-on-your-first-bash-script\">4 Easy Functions to Try On Your First Bash Script<\/h2><p>Now that we know some bash commands, we&rsquo;ll look at more basic bash function examples for your first script.<\/p><p>As mentioned earlier, when you want to write a bash script file, use the nano filename.sh command to create and open a <strong>.sh <\/strong>file and start writing your bash functions. Don&rsquo;t forget to exit and save the file when you&rsquo;re done.<\/p><h3 class=\"wp-block-heading\" id=\"h-start-with-a-simple-echo-function\">Start with a Simple echo Function<\/h3><p>Let&rsquo;s start with a simple echo function. Start by defining the function name followed by the echo command on the next line, just like in the example below:&nbsp;<\/p><pre class=\"wp-block-preformatted\">#!\/bin\/bash\ntestfunction () {\necho &ldquo;My first function&rdquo;\n}\ntestfunction<\/pre><p>Save this script in <strong>testFunction.sh<\/strong>. When you execute the script on the command line, you should see the following output:<\/p><pre class=\"wp-block-preformatted\">My first function<\/pre><p>Keep in mind that if you swap the position of the function definition with the function call, it will result in an error. Let&rsquo;s see the example below:<\/p><pre class=\"wp-block-preformatted\">#!\/bin\/bash<br>testfunction<br>testfunction(){<br>echo &ldquo;My first function&rdquo;<br>}<\/pre><p>This snippet won&rsquo;t work. It calls the function in the second command line and defines the function later. In this case, the interpreter can&rsquo;t find the function when it executes the script, resulting in a <strong>command not found <\/strong>error.<\/p><figure data-wp-context='{\"imageId\":\"69ee3e6d52097\"}' data-wp-interactive=\"core\/image\" class=\"wp-block-image size-full wp-lightbox-container\"><img decoding=\"async\" width=\"705\" height=\"47\" data-wp-class--hide=\"state.isContentHidden\" data-wp-class--show=\"state.isContentVisible\" data-wp-init=\"callbacks.setButtonStyles\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-on-async--load=\"callbacks.setButtonStyles\" data-wp-on-async-window--resize=\"callbacks.setButtonStyles\" src=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2021\/09\/command-not-found-2.png\" alt=\"command line error showing the command not found\" class=\"wp-image-43693\" srcset=\"https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2021\/09\/command-not-found-2.png 705w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2021\/09\/command-not-found-2-300x20.png 300w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2021\/09\/command-not-found-2-150x10.png 150w\" sizes=\"(max-width: 705px) 100vw, 705px\" \/><button class=\"lightbox-trigger\" type=\"button\" aria-haspopup=\"dialog\" aria-label=\"Enlarge\" data-wp-init=\"callbacks.initTriggerButton\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-style--right=\"state.imageButtonRight\" data-wp-style--top=\"state.imageButtonTop\">\n\t\t\t<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"12\" height=\"12\" fill=\"none\" viewbox=\"0 0 12 12\">\n\t\t\t\t<path fill=\"#fff\" d=\"M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z\"><\/path>\n\t\t\t<\/svg>\n\t\t<\/button><\/figure><h3 class=\"wp-block-heading\" id=\"h-use-a-few-parameters\">Use a Few Parameters<\/h3><p>Bash functions accept any number of parameters. The example below accepts two parameters:<\/p><pre class=\"wp-block-preformatted\">#!\/bin\/bash<br>testfunction () {<br>echo $1<br>echo $2<br>}<br>testfunction &ldquo;Hello&rdquo; &ldquo;World&rdquo;<\/pre><p><strong>$1 <\/strong>represents the first argument while<strong> $2<\/strong> represents the second argument in the function execution line. As we used <strong>&ldquo;Hello&rdquo; <\/strong>and <strong>&ldquo;World&rdquo;<\/strong> for the arguments, the output will look like this:<\/p><figure data-wp-context='{\"imageId\":\"69ee3e6d534b0\"}' data-wp-interactive=\"core\/image\" class=\"wp-block-image size-full wp-lightbox-container\"><img decoding=\"async\" width=\"569\" height=\"69\" data-wp-class--hide=\"state.isContentHidden\" data-wp-class--show=\"state.isContentVisible\" data-wp-init=\"callbacks.setButtonStyles\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-on-async--load=\"callbacks.setButtonStyles\" data-wp-on-async-window--resize=\"callbacks.setButtonStyles\" src=\"https:\/\/www.hostinger.com\/tutorials\/wp-content\/uploads\/sites\/2\/2024\/04\/image-6.png\" alt=\"\" class=\"wp-image-108421\" srcset=\"https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2024\/04\/image-6.png 569w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2024\/04\/image-6-300x36.png 300w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2024\/04\/image-6-150x18.png 150w\" sizes=\"(max-width: 569px) 100vw, 569px\" \/><button class=\"lightbox-trigger\" type=\"button\" aria-haspopup=\"dialog\" aria-label=\"Enlarge\" data-wp-init=\"callbacks.initTriggerButton\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-style--right=\"state.imageButtonRight\" data-wp-style--top=\"state.imageButtonTop\">\n\t\t\t<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"12\" height=\"12\" fill=\"none\" viewbox=\"0 0 12 12\">\n\t\t\t\t<path fill=\"#fff\" d=\"M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z\"><\/path>\n\t\t\t<\/svg>\n\t\t<\/button><\/figure><p>You can also use the command line arguments and perform bash functions. One such example is shown below:<\/p><pre class=\"wp-block-preformatted\">#!\/bin\/bash<br>addition () {<br>sum=$(($1+$2))<br>return $sum<br>}<br>read -p \"Enter a number: \" int1<br>read -p \"Enter a number: \" int2<br>addition $int1 $int2<br>echo \"The result is : \" $?<\/pre><p>The addition is assigned in a variable <strong>sum<\/strong>, and this is returned from the function. Bash functions always return one single value. User input is taken by using <strong>read<\/strong> for both numbers. Finally, the result is printed using <strong>$?<\/strong> which stores the return value <strong>$sum<\/strong> from the function.<\/p><h3 class=\"wp-block-heading\" id=\"h-create-directories-and-change-paths\">Create Directories and Change Paths<\/h3><p>Now, let&rsquo;s look at another function example where we first create a directory and then change the path to point to a new location. This function will contain <strong>mkdir <\/strong>and <strong>cd <\/strong><a href=\"\/my\/tutorials\/linux-commands\">Linux commands<\/a> to create a new directory and change the current directory:<\/p><pre class=\"wp-block-preformatted\">#!\/bin\/bash\nsampleFunction () {\nmkdir -p $1\ncd $1\n}\nsampleFunction myDir<\/pre><p>Function will read the first argument and create a directory with that name. After you execute the script, check the present working path using the <strong>pwd<\/strong> command on the terminal. You&rsquo;ll see that you are currently within the newly created <strong>myDir<\/strong>.<\/p><h3 class=\"wp-block-heading\" id=\"h-combine-loops-and-conditionals\">Combine Loops and Conditionals<\/h3><p>Loops and conditional statements are also popular in bash scripting. We&rsquo;ll look at a few instances of using both in the same script:<\/p><pre class=\"wp-block-preformatted\">#!\/bin\/bash<br>isvalid=true<br>count=1<br>while [ $isvalid ]<br>do<br>echo $count<br>if [ $count -eq 5 ];<br>then<br>break<br>fi<br>((count++))<br>done<\/pre><p>The example above uses <strong>while <\/strong>and <strong>if<\/strong> statements. This executes the <strong>while<\/strong> loop five times after checking the conditional statement.<\/p><p>The output of this script will be:<\/p><pre class=\"wp-block-preformatted\">1<br>2<br>3<br>4<br>5<\/pre><p>The <strong>for<\/strong> loop can increment and decrement the counters. An example of a <strong>for<\/strong> loop is shown below:<\/p><pre class=\"wp-block-preformatted\">#!\/bin\/bash<br>for (( count=10; count&gt;0; count-- ))<br>do<br>echo -n \"$count \"<br>done<\/pre><p>The output of this for loop should be:<\/p><pre class=\"wp-block-preformatted\">10 9 8 7 6 5 4 3 2 1<\/pre><p>With <strong>if<\/strong> statements, we can also define <strong>else if <\/strong>by using<strong> elif <\/strong>statement:<\/p><pre class=\"wp-block-preformatted\">#!\/bin\/bash<br>echo \"Enter a valid number\"<br>read n<br>if [ $n -eq 101 ];<br>then<br>echo \"This is the first number\"<br>elif [ $n -eq 510 ];<br>then<br>echo \"This is the second number\"<br>elif [ $n -eq 999 ];<br>then<br>echo \"This is the third number\"<br>else<br>echo \"No numbers over here\"<br>fi<\/pre><p>We can also write that script using the <strong>case <\/strong>statement. In the <strong>case <\/strong>statements, <strong>;;<\/strong> represents a case break, so if the variable value meets any of the conditions, it jumps to the end of the script:<\/p><pre class=\"wp-block-preformatted\">#!\/bin\/bash<br>echo \"Enter a valid number\"<br>read n<br>case $n in<br>101)<br>echo \"This is the first number\" ;;<br>510)<br>echo \"This is the second number\" ;;<br>999)<br>echo \"This is the third number\" ;;<br>*)<br>echo \"No numbers over here\" ;;<br>esac<\/pre><h2 class=\"wp-block-heading\" id=\"h-how-to-run-bash-script\">How to Run Bash Script<\/h2><p>Now that we have written a bash script, let&rsquo;s learn how to run it from the terminal. There are three methods to do it &ndash; using the <strong>bash <\/strong>command, using the <strong>.\/ <\/strong>command, and running the script from a different directory.<\/p><h3 class=\"wp-block-heading\" id=\"h-using-the-bash-command\">Using the Bash Command<\/h3><p>The first method is by using the <strong>bash<\/strong> command from the appropriate directory. For example, you may have a <strong>function.sh <\/strong>bash script containing simple echo functions in the <strong>Test\/Bash <\/strong>directory. You have to open the directory first by using this command:<\/p><pre class=\"wp-block-preformatted\">cd Test\/Bash<\/pre><p>Then, execute the following <strong>bash <\/strong>command to run the bash script:<\/p><pre class=\"wp-block-preformatted\">bash function.sh<\/pre><p>You should see the output like this:<\/p><figure data-wp-context='{\"imageId\":\"69ee3e6d54db1\"}' data-wp-interactive=\"core\/image\" class=\"wp-block-image size-full wp-lightbox-container\"><img decoding=\"async\" width=\"571\" height=\"111\" data-wp-class--hide=\"state.isContentHidden\" data-wp-class--show=\"state.isContentVisible\" data-wp-init=\"callbacks.setButtonStyles\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-on-async--load=\"callbacks.setButtonStyles\" data-wp-on-async-window--resize=\"callbacks.setButtonStyles\" src=\"https:\/\/www.hostinger.com\/tutorials\/wp-content\/uploads\/sites\/2\/2024\/04\/image-7.png\" alt=\"\" class=\"wp-image-108423\" srcset=\"https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2024\/04\/image-7.png 571w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2024\/04\/image-7-300x58.png 300w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2024\/04\/image-7-150x29.png 150w\" sizes=\"(max-width: 571px) 100vw, 571px\" \/><button class=\"lightbox-trigger\" type=\"button\" aria-haspopup=\"dialog\" aria-label=\"Enlarge\" data-wp-init=\"callbacks.initTriggerButton\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-style--right=\"state.imageButtonRight\" data-wp-style--top=\"state.imageButtonTop\">\n\t\t\t<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"12\" height=\"12\" fill=\"none\" viewbox=\"0 0 12 12\">\n\t\t\t\t<path fill=\"#fff\" d=\"M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z\"><\/path>\n\t\t\t<\/svg>\n\t\t<\/button><\/figure><p>If you try to run the script without the <strong>bash <\/strong>command, you&rsquo;ll receive a <strong>command not found <\/strong>error message.<\/p><figure data-wp-context='{\"imageId\":\"69ee3e6d561d5\"}' data-wp-interactive=\"core\/image\" class=\"wp-block-image size-full wp-lightbox-container\"><img decoding=\"async\" width=\"511\" height=\"48\" data-wp-class--hide=\"state.isContentHidden\" data-wp-class--show=\"state.isContentVisible\" data-wp-init=\"callbacks.setButtonStyles\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-on-async--load=\"callbacks.setButtonStyles\" data-wp-on-async-window--resize=\"callbacks.setButtonStyles\" src=\"https:\/\/www.hostinger.com\/tutorials\/wp-content\/uploads\/sites\/2\/2024\/04\/image-8.png\" alt=\"\" class=\"wp-image-108424\" srcset=\"https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2024\/04\/image-8.png 511w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2024\/04\/image-8-300x28.png 300w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2024\/04\/image-8-150x14.png 150w\" sizes=\"(max-width: 511px) 100vw, 511px\" \/><button class=\"lightbox-trigger\" type=\"button\" aria-haspopup=\"dialog\" aria-label=\"Enlarge\" data-wp-init=\"callbacks.initTriggerButton\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-style--right=\"state.imageButtonRight\" data-wp-style--top=\"state.imageButtonTop\">\n\t\t\t<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"12\" height=\"12\" fill=\"none\" viewbox=\"0 0 12 12\">\n\t\t\t\t<path fill=\"#fff\" d=\"M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z\"><\/path>\n\t\t\t<\/svg>\n\t\t<\/button><\/figure><h3 class=\"wp-block-heading\" id=\"h-using-the-command\">Using the .\/ Command<\/h3><p>You can run a bash script without the <strong>bash <\/strong>command. However, you have to set the file to have the <strong>execute <\/strong>permission using the following command from the appropriate directory:<\/p><pre class=\"wp-block-preformatted\">chmod +x function.sh<\/pre><p>This command modifies the file permissions so that everyone can execute the file. Once you&rsquo;ve done that, execute the bash script by using this command:<\/p><pre class=\"wp-block-preformatted\">.\/function.sh<\/pre><p>If you don&rsquo;t set the permission correctly, the terminal will print a <strong>Permission denied <\/strong>error message:<\/p><figure data-wp-context='{\"imageId\":\"69ee3e6d57ee5\"}' data-wp-interactive=\"core\/image\" class=\"wp-block-image size-full wp-lightbox-container\"><img decoding=\"async\" width=\"530\" height=\"49\" data-wp-class--hide=\"state.isContentHidden\" data-wp-class--show=\"state.isContentVisible\" data-wp-init=\"callbacks.setButtonStyles\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-on-async--load=\"callbacks.setButtonStyles\" data-wp-on-async-window--resize=\"callbacks.setButtonStyles\" src=\"https:\/\/www.hostinger.com\/tutorials\/wp-content\/uploads\/sites\/2\/2024\/04\/image-9.png\" alt=\"Bash error message permission denied\" class=\"wp-image-108425\" srcset=\"https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2024\/04\/image-9.png 530w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2024\/04\/image-9-300x28.png 300w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2024\/04\/image-9-150x14.png 150w\" sizes=\"(max-width: 530px) 100vw, 530px\" \/><button class=\"lightbox-trigger\" type=\"button\" aria-haspopup=\"dialog\" aria-label=\"Enlarge\" data-wp-init=\"callbacks.initTriggerButton\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-style--right=\"state.imageButtonRight\" data-wp-style--top=\"state.imageButtonTop\">\n\t\t\t<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"12\" height=\"12\" fill=\"none\" viewbox=\"0 0 12 12\">\n\t\t\t\t<path fill=\"#fff\" d=\"M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z\"><\/path>\n\t\t\t<\/svg>\n\t\t<\/button><\/figure><p>Like the <strong>bash <\/strong>command, you&rsquo;ll also get a <strong>command not found <\/strong>error if you don&rsquo;t use <strong>.\/ <\/strong>in your command.<\/p><h3 class=\"wp-block-heading\" id=\"h-run-the-script-from-a-different-directory\">Run the Script From a Different Directory<\/h3><p>Another alternative is to run the script from a different directory. After you create the bash script, use the <strong>pwd <\/strong>command to find your current directory. Once you know the path, you can run the script from any directory. For example, use the following command to run <strong>function.sh<\/strong> from the home directory:<\/p><pre class=\"wp-block-preformatted\">bash Test\/Bash\/function.sh<\/pre><p>\n\n\n<div class=\"protip\">\n                    <h4 class=\"title\">Pro Tip<\/h4>\n                    <p>Use the <strong>cd<\/strong> command to go to the home directory straight away regardless of the directory you are in. <\/p>\n                <\/div>\n\n\n\n<\/p><h2 class=\"wp-block-heading\" id=\"h-conclusion\">Conclusion<\/h2><p>Bash reads shell commands and interacts with the operating system to execute them. The great aspect of bash is that you can compile many bash commands and functions into a single executable script, helping you streamline your workflow.<\/p><p>To create a bash script file, you need to understand the various shell commands and their proper syntax. In this tutorial, we&rsquo;ve covered seven basic commands:<\/p><ul class=\"wp-block-list\">\n<li>Comments<\/li>\n\n\n\n<li>Variables<\/li>\n\n\n\n<li>Echo<\/li>\n\n\n\n<li>Functions<\/li>\n\n\n\n<li>Loops<\/li>\n\n\n\n<li>Conditional statements<\/li>\n\n\n\n<li>Reading and writing files<\/li>\n<\/ul><p>However, there is much more to learn if you want to be able to utilize the full potential of bash. Practice with the examples we have provided and continue exploring bash so you can write better and more efficient scripts.<\/p><p>We&rsquo;ve covered the three ways to execute a bash script. If you have any questions or comments, leave them in the section below.&nbsp;<\/p><p>\n\n\n<div class=\"protip\">\n                    <h4 class=\"title\">Looking for more bash guides?<\/h4>\n                    <p><a href=\"\/my\/tutorials\/bash-array\">Beginner's Guide to Bash Array<\/a><br>\n<a href=\"\/my\/tutorials\/bash-script-example\">25 Common Linux Bash Script Examples<\/a><\/p>\n                <\/div>\n\n\n\n<\/p><h2 class=\"wp-block-heading\" id=\"h-bash-tutorial-faq\">Bash Tutorial FAQ<\/h2><div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1666860299007\"><h3 class=\"schema-faq-question\">Is Bash Easy to Learn?<\/h3> <p class=\"schema-faq-answer\">Bash is quite easy to learn, with many learners mastering both basic and advanced commands in about half a year.<\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1666860305365\"><h3 class=\"schema-faq-question\">Is Bash Easier Than Python?<\/h3> <p class=\"schema-faq-answer\">No, Python remains easier and simpler than Bash. Since Bash was originally a software replacement for Bourne shell, Bash remains tough to write compared to Python. Python is also more powerful than Bash.<\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1666860321514\"><h3 class=\"schema-faq-question\">What Is Bash Used For?<\/h3> <p class=\"schema-faq-answer\">Bash is most commonly used to automate development tasks like code compilation, debugging, change management and testing. Bash is also used by network engineers to test, optimize and configure organizational networks.<\/p> <\/div> <\/div>\n","protected":false},"excerpt":{"rendered":"<p>If you&rsquo;ve ever used a Linux operating system, for example, a Linux-based VPS, you may have heard of bash. It&rsquo;s a Unix shell that reads and executes various commands. When you need to run several bash commands, you don&rsquo;t have to execute them manually one at a time. Instead, it&rsquo;s possible to create a script [&#8230;]<\/p>\n<p><a class=\"btn btn-secondary understrap-read-more-link\" href=\"\/my\/tutorials\/bash-scripting-tutorial\">Read More&#8230;<\/a><\/p>\n","protected":false},"author":172,"featured_media":48509,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"rank_math_title":"Bash Scripting for Beginners + Downloadable Free Cheat Sheet","rank_math_description":"If you're a Unix user, learn Bash scripting to streamline your workflow, eliminate repetitive tasks, and improve the quality of your work!","rank_math_focus_keyword":"bash tutorial","footnotes":""},"categories":[],"tags":[],"class_list":["post-15650","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry"],"hreflangs":[],"_links":{"self":[{"href":"https:\/\/www.hostinger.com\/my\/tutorials\/wp-json\/wp\/v2\/posts\/15650","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\/172"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/my\/tutorials\/wp-json\/wp\/v2\/comments?post=15650"}],"version-history":[{"count":32,"href":"https:\/\/www.hostinger.com\/my\/tutorials\/wp-json\/wp\/v2\/posts\/15650\/revisions"}],"predecessor-version":[{"id":111890,"href":"https:\/\/www.hostinger.com\/my\/tutorials\/wp-json\/wp\/v2\/posts\/15650\/revisions\/111890"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/my\/tutorials\/wp-json\/wp\/v2\/media\/48509"}],"wp:attachment":[{"href":"https:\/\/www.hostinger.com\/my\/tutorials\/wp-json\/wp\/v2\/media?parent=15650"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hostinger.com\/my\/tutorials\/wp-json\/wp\/v2\/categories?post=15650"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hostinger.com\/my\/tutorials\/wp-json\/wp\/v2\/tags?post=15650"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}