{"id":139361,"date":"2026-01-26T04:22:57","date_gmt":"2026-01-26T04:22:57","guid":{"rendered":"\/tutorials\/?p=139361"},"modified":"2026-03-09T19:16:03","modified_gmt":"2026-03-09T19:16:03","slug":"bash-scripting-tutorial","status":"publish","type":"post","link":"\/ng\/tutorials\/bash-scripting-tutorial","title":{"rendered":"What is a bash script and how to use it?"},"content":{"rendered":"<?xml encoding=\"utf-8\" ?><p>If you&rsquo;ve ever used a Linux operating system, 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><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><h2 class=\"wp-block-heading\" id=\"h-why-should-you-use-a-bash-script\">Why should you use a bash script?<\/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><h2 class=\"wp-block-heading\" id=\"h-understanding-bash-commands\">Understanding 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 <a href=\"\/ng\/tutorials\/linux-commands\">Linux command<\/a> 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=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">cat \/etc\/shells<\/pre><p>The output should show a list 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\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=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">which bash<\/pre><p>It should produce the following output:<\/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<\/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><p>#!\/bin\/bash<\/p><p>\n[pro tip]Linux has a bash shell command manual. It contains descriptions of all technical terms and standard shell variables. Type and execute the man bash command to display the manual on the terminal.[\/protip]<\/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 VIM or GNU Nano. In this tutorial, we&rsquo;ll use the <strong>Nano <\/strong>text editor to create the file by inputting this command:<\/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=\"\">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><div><p class=\"important\">Once you&rsquo;re done using the Nano text editor, press CTRL+X to close it, then press Y and Enter to save the changes.<\/p><\/div>\n\n\n\n<\/p><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>Read our <a href=\"\/ng\/tutorials\/bash-script-example\">bash commands and script examples<\/a> to help you get started.<\/p><h2 class=\"wp-block-heading\" id=\"h-how-to-run-a-bash-script\">How to run a 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\">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=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">cd Test\/Bash<\/pre><p>Then, execute the following <strong>bash <\/strong>command to run the 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=\"\">bash function.sh<\/pre><p>You should see the output like this:<\/p><div class=\"wp-block-image\"><figure data-wp-context='{\"imageId\":\"69e1ab01caa28\"}' data-wp-interactive=\"core\/image\" class=\"aligncenter size-full wp-lightbox-container\"><img loading=\"lazy\" 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:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2026\/01\/bash-hello-world.png\/public\" alt=\"Output of bash function in the terminal\" class=\"wp-image-139379\" srcset=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2026\/01\/bash-hello-world.png\/w=571,fit=scale-down 571w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2026\/01\/bash-hello-world.png\/w=300,fit=scale-down 300w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2026\/01\/bash-hello-world.png\/w=150,fit=scale-down 150w\" sizes=\"auto, (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><\/div><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><div class=\"wp-block-image\"><figure data-wp-context='{\"imageId\":\"69e1ab01cbdd4\"}' data-wp-interactive=\"core\/image\" class=\"aligncenter size-full wp-lightbox-container\"><img loading=\"lazy\" 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:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2026\/01\/bash-command-not-found.png\/public\" alt=\"Command not found error output due to the non-existent bash command\" class=\"wp-image-139380\" srcset=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2026\/01\/bash-command-not-found.png\/w=511,fit=scale-down 511w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2026\/01\/bash-command-not-found.png\/w=300,fit=scale-down 300w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2026\/01\/bash-command-not-found.png\/w=150,fit=scale-down 150w\" sizes=\"auto, (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><\/div><h3 class=\"wp-block-heading\">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=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">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=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">.\/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><div class=\"wp-block-image\"><figure data-wp-context='{\"imageId\":\"69e1ab01cd362\"}' data-wp-interactive=\"core\/image\" class=\"aligncenter size-full wp-lightbox-container\"><img loading=\"lazy\" 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:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2026\/01\/bash-denied.png\/public\" alt=\"Bash permission denied output\" class=\"wp-image-139381\" srcset=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2026\/01\/bash-denied.png\/w=530,fit=scale-down 530w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2026\/01\/bash-denied.png\/w=300,fit=scale-down 300w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2026\/01\/bash-denied.png\/w=150,fit=scale-down 150w\" sizes=\"auto, (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><\/div><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\">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=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">bash Test\/Bash\/function.sh<\/pre><p><div class=\"protip\">\n                    <h4 class=\"title\"><\/h4>\n                    <p>Use the cd 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-next-step-practicing-and-mastering-bash-scripting\">Next step: Practicing and mastering bash scripting<\/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>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. We recommend continue learning about <a href=\"\/ng\/tutorials\/bash-variables\">bash variables<\/a> as they can make your script more flexible. Good luck!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you&rsquo;ve ever used a Linux operating system, 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 file that contains bash functions [&#8230;]<\/p>\n<p><a class=\"btn btn-secondary understrap-read-more-link\" href=\"\/ng\/tutorials\/bash-scripting-tutorial\">Read More&#8230;<\/a><\/p>\n","protected":false},"author":411,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"rank_math_title":"What is a bash scripting and how to use it?","rank_math_description":"Bash is a Unix shell that reads and executes various commands. Read this article if you want to learn how to use Bash to manage your server.\n","rank_math_focus_keyword":"bash scripting tutorial","footnotes":""},"categories":[22644],"tags":[],"class_list":["post-139361","post","type-post","status-publish","format-standard","hentry","category-vps"],"hreflangs":[{"locale":"en-US","link":"https:\/\/www.hostinger.com\/tutorials\/bash-scripting-tutorial","default":0},{"locale":"en-PH","link":"https:\/\/www.hostinger.com\/ph\/tutorials\/bash-scripting-tutorial","default":0},{"locale":"en-MY","link":"https:\/\/www.hostinger.com\/my\/tutorials\/bash-scripting-tutorial","default":0},{"locale":"en-UK","link":"https:\/\/www.hostinger.com\/uk\/tutorials\/bash-scripting-tutorial","default":0},{"locale":"en-IN","link":"https:\/\/www.hostinger.com\/in\/tutorials\/bash-scripting-tutorial","default":0},{"locale":"en-CA","link":"https:\/\/www.hostinger.com\/ca\/tutorials\/bash-scripting-tutorial","default":0},{"locale":"en-AU","link":"https:\/\/www.hostinger.com\/au\/tutorials\/bash-scripting-tutorial","default":0},{"locale":"en-NG","link":"https:\/\/www.hostinger.com\/ng\/tutorials\/bash-scripting-tutorial","default":0}],"_links":{"self":[{"href":"https:\/\/www.hostinger.com\/ng\/tutorials\/wp-json\/wp\/v2\/posts\/139361","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hostinger.com\/ng\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hostinger.com\/ng\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/ng\/tutorials\/wp-json\/wp\/v2\/users\/411"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/ng\/tutorials\/wp-json\/wp\/v2\/comments?post=139361"}],"version-history":[{"count":2,"href":"https:\/\/www.hostinger.com\/ng\/tutorials\/wp-json\/wp\/v2\/posts\/139361\/revisions"}],"predecessor-version":[{"id":142940,"href":"https:\/\/www.hostinger.com\/ng\/tutorials\/wp-json\/wp\/v2\/posts\/139361\/revisions\/142940"}],"wp:attachment":[{"href":"https:\/\/www.hostinger.com\/ng\/tutorials\/wp-json\/wp\/v2\/media?parent=139361"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hostinger.com\/ng\/tutorials\/wp-json\/wp\/v2\/categories?post=139361"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hostinger.com\/ng\/tutorials\/wp-json\/wp\/v2\/tags?post=139361"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}