{"id":10401,"date":"2024-10-12T18:00:00","date_gmt":"2024-10-12T13:00:00","guid":{"rendered":"https:\/\/arzhost.com\/blogs\/?p=10401"},"modified":"2024-09-27T21:24:14","modified_gmt":"2024-09-27T16:24:14","slug":"how-to-run-a-bash-script-in-terminal","status":"publish","type":"post","link":"https:\/\/arzhost.com\/blogs\/how-to-run-a-bash-script-in-terminal\/","title":{"rendered":"How to Run a Bash Script in Terminal? A Step-by-Step Tutorial"},"content":{"rendered":"\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_74 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/arzhost.com\/blogs\/how-to-run-a-bash-script-in-terminal\/#Introduction_to_Bash_Scripting_An_Overview\" >Introduction to Bash Scripting: An Overview<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/arzhost.com\/blogs\/how-to-run-a-bash-script-in-terminal\/#Prerequisites_for_Writing_and_Running_Bash_Scripts\" >Prerequisites for Writing and Running Bash Scripts<\/a><\/li><\/ul><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/arzhost.com\/blogs\/how-to-run-a-bash-script-in-terminal\/#What_is_a_Bash_Script_Understanding_Its_Purpose_and_Functionality\" >What is a Bash Script? Understanding Its Purpose and Functionality<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/arzhost.com\/blogs\/how-to-run-a-bash-script-in-terminal\/#Boost_Your_Sites_Performance_with_VPS_Hosting_%E2%80%93_Best_Plans_Available\" >Boost Your Site\u2019s Performance with VPS Hosting &#8211; Best Plans Available<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/arzhost.com\/blogs\/how-to-run-a-bash-script-in-terminal\/#How_to_Run_a_Bash_Script_in_Terminal_4_Effective_Steps_Guide\" >How to Run a Bash Script in Terminal: 4 Effective Steps Guide<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/arzhost.com\/blogs\/how-to-run-a-bash-script-in-terminal\/#Creating_a_Bash_Script_Step-by-Step_Guide\" >Creating a Bash Script: Step-by-Step Guide<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/arzhost.com\/blogs\/how-to-run-a-bash-script-in-terminal\/#1_Open_a_Text_Editor_for_Writing_Your_Script\" >1: Open a Text Editor for Writing Your Script<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/arzhost.com\/blogs\/how-to-run-a-bash-script-in-terminal\/#2_Write_Your_Script_Best_Practices_and_Tips\" >2: Write Your Script: Best Practices and Tips<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/arzhost.com\/blogs\/how-to-run-a-bash-script-in-terminal\/#3_Save_and_Exit_the_Text_Editor_Correctly\" >3: Save and Exit the Text Editor Correctly<\/a><\/li><\/ul><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/arzhost.com\/blogs\/how-to-run-a-bash-script-in-terminal\/#Making_the_Bash_Script_Executable_Essential_Steps\" >Making the Bash Script Executable: Essential Steps<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/arzhost.com\/blogs\/how-to-run-a-bash-script-in-terminal\/#1_Check_the_Current_Permissions_of_Your_Script\" >1: Check the Current Permissions of Your Script<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/arzhost.com\/blogs\/how-to-run-a-bash-script-in-terminal\/#2_Change_the_Permissions_to_Make_It_Executable\" >2: Change the Permissions to Make It Executable<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/arzhost.com\/blogs\/how-to-run-a-bash-script-in-terminal\/#3_Verify_the_Permissions_to_Ensure_Proper_Access\" >3: Verify the Permissions to Ensure Proper Access<\/a><\/li><\/ul><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/arzhost.com\/blogs\/how-to-run-a-bash-script-in-terminal\/#Running_the_Bash_Script_in_Terminal_Different_Approaches\" >Running the Bash Script in Terminal: Different Approaches<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-15\" href=\"https:\/\/arzhost.com\/blogs\/how-to-run-a-bash-script-in-terminal\/#1_Running_the_Script_Directly_from_the_Terminal\" >1: Running the Script Directly from the Terminal<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-16\" href=\"https:\/\/arzhost.com\/blogs\/how-to-run-a-bash-script-in-terminal\/#2_Using_the_bash_Command_for_Execution\" >2: Using the bash Command for Execution<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-17\" href=\"https:\/\/arzhost.com\/blogs\/how-to-run-a-bash-script-in-terminal\/#3_Using_the_sh_Command_to_Run_Your_Script\" >3: Using the sh Command to Run Your Script<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-18\" href=\"https:\/\/arzhost.com\/blogs\/how-to-run-a-bash-script-in-terminal\/#4_Running_with_a_Relative_or_Absolute_Path_for_Access\" >4: Running with a Relative or Absolute Path for Access<\/a><\/li><\/ul><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-19\" href=\"https:\/\/arzhost.com\/blogs\/how-to-run-a-bash-script-in-terminal\/#Debugging_Bash_Scripts_Tips_for_Troubleshooting\" >Debugging Bash Scripts: Tips for Troubleshooting<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-20\" href=\"https:\/\/arzhost.com\/blogs\/how-to-run-a-bash-script-in-terminal\/#1_Use_set_-x_for_Debugging_Enabling_Trace_Mode\" >1: Use set -x for Debugging: Enabling Trace Mode<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-21\" href=\"https:\/\/arzhost.com\/blogs\/how-to-run-a-bash-script-in-terminal\/#2_Check_for_Syntax_Errors_Common_Issues_and_Fixes\" >2: Check for Syntax Errors: Common Issues and Fixes<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-22\" href=\"https:\/\/arzhost.com\/blogs\/how-to-run-a-bash-script-in-terminal\/#3_Use_echo_Statements_for_Output_Debugging\" >3: Use echo Statements for Output Debugging<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-23\" href=\"https:\/\/arzhost.com\/blogs\/how-to-run-a-bash-script-in-terminal\/#4_Review_Exit_Statuses_to_Understand_Script_Behavior\" >4: Review Exit Statuses to Understand Script Behavior<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-24\" href=\"https:\/\/arzhost.com\/blogs\/how-to-run-a-bash-script-in-terminal\/#5_Consult_the_Bash_Manual_for_Reference_and_Guidance\" >5: Consult the Bash Manual for Reference and Guidance<\/a><\/li><\/ul><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-25\" href=\"https:\/\/arzhost.com\/blogs\/how-to-run-a-bash-script-in-terminal\/#Launch_Your_Own_Hosting_Business%E2%80%94Sign_Up_Now\" >Launch Your Own Hosting Business\u2014Sign Up Now!<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-26\" href=\"https:\/\/arzhost.com\/blogs\/how-to-run-a-bash-script-in-terminal\/#Conclusion_Summary_of_Key_Points_and_Best_Practices\" >Conclusion: Summary of Key Points and Best Practices<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-27\" href=\"https:\/\/arzhost.com\/blogs\/how-to-run-a-bash-script-in-terminal\/#FAQs_Frequently_Asked_Questions\" >FAQs (Frequently Asked Questions)<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-28\" href=\"https:\/\/arzhost.com\/blogs\/how-to-run-a-bash-script-in-terminal\/#1_What_is_a_Bash_script_and_why_would_I_want_to_run_it_in_the_terminal\" >1: What is a Bash script, and why would I want to run it in the terminal?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-29\" href=\"https:\/\/arzhost.com\/blogs\/how-to-run-a-bash-script-in-terminal\/#2_How_do_I_create_a_Bash_script_to_run_in_the_terminal\" >2: How do I create a Bash script to run in the terminal?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-30\" href=\"https:\/\/arzhost.com\/blogs\/how-to-run-a-bash-script-in-terminal\/#3_How_can_I_make_my_Bash_script_executable_and_why_is_this_step_necessary\" >3: How can I make my Bash script executable, and why is this step necessary?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-31\" href=\"https:\/\/arzhost.com\/blogs\/how-to-run-a-bash-script-in-terminal\/#4_How_do_I_run_a_Bash_script_in_the_terminal_and_what_happens_when_I_do\" >4: How do I run a Bash script in the terminal, and what happens when I do?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-32\" href=\"https:\/\/arzhost.com\/blogs\/how-to-run-a-bash-script-in-terminal\/#5_What_should_I_do_if_my_Bash_script_doesnt_run_as_expected\" >5: What should I do if my Bash script doesn\u2019t run as expected?<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Introduction_to_Bash_Scripting_An_Overview\"><\/span><strong>Introduction to Bash Scripting: An Overview<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Running Bash scripts in the terminal is a powerful way to automate tasks, streamline processes, and manage system operations more efficiently.\u00a0 Whether you&#8217;re a beginner or an experienced programmer, understanding how to run a Bash script in the terminal can enhance your productivity.\u00a0<\/p>\n\n\n\n<p>This article will guide you through the <strong>step-by-step process of How to Run a Bash Script in Terminal<\/strong>, including writing a basic script, setting permissions, and executing it.<\/p>\n\n\n\n<p>For more Tutorials and Guides, check out our <a href=\"https:\/\/arzhost.com\/blogs\/\"><strong>Blog Page at ARZ Host<\/strong><\/a>.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Prerequisites_for_Writing_and_Running_Bash_Scripts\"><\/span><strong>Prerequisites for Writing and Running Bash Scripts<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p>Before running a Bash script in the terminal, make sure you have the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>A Unix-like operating system:<\/strong> Bash scripts are commonly used in Linux and macOS environments. Windows users can use <strong><a href=\"https:\/\/learn.microsoft.com\/en-us\/windows\/wsl\/install\" data-type=\"link\" data-id=\"https:\/\/learn.microsoft.com\/en-us\/windows\/wsl\/install\" target=\"_blank\" rel=\"noopener\">Windows Subsystem for Linux (WSL)<\/a><\/strong> to run Bash scripts.<\/li>\n\n\n\n<li><strong>A terminal emulator:<\/strong> This could be a Terminal on macOS, GNOME Terminal on Linux, or any other terminal emulator.<\/li>\n\n\n\n<li><strong>Basic knowledge of shell commands:<\/strong> Familiarity with commands like echo, cd, ls, and chmod will be helpful.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"What_is_a_Bash_Script_Understanding_Its_Purpose_and_Functionality\"><\/span><strong>What is a Bash Script? Understanding Its Purpose and Functionality<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>A <strong>Bash script is a plain text file containing a series of commands<\/strong> that can be executed by the <strong>Bash shell<\/strong>, a command-line interpreter used in UNIX-based operating systems like Linux and macOS.\u00a0<\/p>\n\n\n\n<p>These scripts are a way to automate tasks that would otherwise need to be performed manually in a terminal, allowing users to streamline complex processes, save time, and minimize the potential for human error.&nbsp;<\/p>\n\n\n\n<p>By writing a Bash script, you can instruct the system to execute a sequence of commands in the exact order you specify, making it a powerful tool for system administration, software development, and various other technical tasks.<\/p>\n\n\n\n<p><strong>Bash scripts are widely used in the programming and IT fields<\/strong> due to their versatility and ease of use. They can be used to perform a wide range of tasks, such as automating backups, managing user accounts, or deploying applications.\u00a0<\/p>\n\n\n\n<p>Bash scripts support variables, loops, conditionals, and functions, enabling users to create sophisticated and dynamic scripts that can respond to different conditions and handle complex workflows.&nbsp;<\/p>\n\n\n\n<p>Additionally, because Bash is the default shell on most UNIX-like systems, Bash scripts are highly portable and can be executed on a variety of systems without modification.<\/p>\n\n\n\n<p>One of the key advantages of using Bash scripts is their simplicity. Writing a Bash script does not require any special software beyond a basic text editor and a terminal.&nbsp;<\/p>\n\n\n\n<p>The syntax is straightforward, making it accessible even to those with minimal programming experience. As a result, Bash scripting is often one of the first skills that new Linux users learn, serving as an introduction to the world of programming and automation.&nbsp;<\/p>\n\n\n\n<p>However, despite its simplicity, Bash scripting can be extremely powerful, capable of handling everything from basic file manipulation to complex system administration tasks.<\/p>\n\n\n\n<div class=\"wp-block-uagb-call-to-action uagb-block-1e518341 wp-block-button uag-blocks-common-selector\" style=\"--z-index-desktop:479;;--z-index-tablet:undefined;;--z-index-mobile:undefined;\"><div class=\"uagb-cta__wrap\"><h2 class=\"uagb-cta__title\"><span class=\"ez-toc-section\" id=\"Boost_Your_Sites_Performance_with_VPS_Hosting_%E2%80%93_Best_Plans_Available\"><\/span><a href=\"https:\/\/arzhost.com\/vps\/\" data-type=\"link\" data-id=\"https:\/\/arzhost.com\/vps\/\">Boost Your Site\u2019s Performance with VPS Hosting &#8211; Best Plans Available<\/a><span class=\"ez-toc-section-end\"><\/span><\/h2><p class=\"uagb-cta__desc\">Unlock the Potential of VPS Hosting &#8211; Starter Plan starts at just <strong>$12.50\/month<\/strong><\/p><\/div><div class=\"uagb-cta__buttons\"><a href=\"https:\/\/arzhost.com\/vps\/\" class=\"uagb-cta__button-link-wrapper wp-block-button__link\" target=\"_self\" rel=\"noopener noreferrer\">Read More<\/a><\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"How_to_Run_a_Bash_Script_in_Terminal_4_Effective_Steps_Guide\"><\/span><strong>How to Run a Bash Script in Terminal: 4 Effective Steps Guide<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Running a Bash script in Terminal is a straightforward process, with four primary methods available: using the bash command, executing the script directly by making it executable, using the sh command, or invoking the script with the source.\u00a0<\/p>\n\n\n\n<p>Each method has its unique advantages depending on your needs, such as running the script in the current shell or as a separate process.&nbsp;<\/p>\n\n\n\n<p>By understanding these options, you can choose the best approach for your scripting needs, ensuring efficiency and flexibility in your workflow. Check out our <a href=\"https:\/\/arzhost.com\/blogs\/bash-math-operations\/\"><strong>Comprehensive Guide on Bash Math Operations<\/strong><\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Creating_a_Bash_Script_Step-by-Step_Guide\"><\/span><strong>Creating a Bash Script: Step-by-Step Guide<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>To create a Bash script, start by opening a text editor like Nano, Vim, or even a graphical editor such as VS Code.&nbsp;<\/p>\n\n\n\n<p>The first line of your script should include the shebang (#! \/bin\/bash), which tells the system to use Bash to interpret the script. Next, you can write your commands. Bash scripts can include loops, conditionals, functions, and more, allowing for the automation of complex tasks.\u00a0<\/p>\n\n\n\n<p>Creating a Bash script is the first step in automating tasks. Follow these instructions to create your first script:<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"1_Open_a_Text_Editor_for_Writing_Your_Script\"><\/span><strong>1: Open a Text Editor for Writing Your Script<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p>You can use any text editor, such as <strong>Nano, vim, or Gedit<\/strong>. For this tutorial, we&#8217;ll use <strong>nano <\/strong>for simplicity.<\/p>\n\n\n\n<p>Open the terminal and type:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"nano myscript.sh\n\nThis command will open the nano editor and create a new file named myscript.sh.\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #D8DEE9\">nano<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">myscript<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #D8DEE9\">sh<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9\">This<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">command<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">will<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">open<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">the<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">nano<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">editor<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">and<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">create<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">a<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">new<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">file<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">named<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">myscript<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #D8DEE9\">sh<\/span><span style=\"color: #ECEFF4\">.<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"2_Write_Your_Script_Best_Practices_and_Tips\"><\/span><strong>2: Write Your Script: Best Practices and Tips<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p>Start by writing a simple script. For example:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"#! \/bin\/bash\n\necho &quot;Hello, World!&quot;\n\nThe #! \/bin\/bash line is called a &quot;shebang.&quot; It tells the system that the script should be run in the Bash shell.\n\nThe echo command is used to print text to the terminal.\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #616E88\">#! \/bin\/bash<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9\">echo<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">Hello, World!<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9\">The<\/span><span style=\"color: #D8DEE9FF\"> #<\/span><span style=\"color: #81A1C1\">!<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">\/<\/span><span style=\"color: #D8DEE9\">bin<\/span><span style=\"color: #81A1C1\">\/<\/span><span style=\"color: #D8DEE9\">bash<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">line<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">is<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">called<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">a<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">shebang.<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">It<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">tells<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">the<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">system<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">that<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">the<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">script<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">should<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">be<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">run<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">in<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">the<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">Bash<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">shell<\/span><span style=\"color: #ECEFF4\">.<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9\">The<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">echo<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">command<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">is<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">used<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">to<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">print<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">text<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">to<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">the<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">terminal<\/span><span style=\"color: #ECEFF4\">.<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"3_Save_and_Exit_the_Text_Editor_Correctly\"><\/span><strong>3: Save and Exit the Text Editor Correctly<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p>Save the script by pressing <strong>CTRL + O,<\/strong> then press <strong>ENTER<\/strong>. Exit the editor by pressing <strong>CTRL + X.<\/strong><\/p>\n\n\n\n<p>Congratulations! You&#8217;ve created your first Bash script. Now, let&#8217;s move on to making it executable.<\/p>\n\n\n\n<p>Save the file with a .sh extension to indicate that it is a shell script. Once saved, your script is ready for the next steps, which involve making it executable and running it in the Terminal.&nbsp;<\/p>\n\n\n\n<p>See <a href=\"https:\/\/arzhost.com\/blogs\/how-to-search-to-find-a-word-in-vim-or-vi-text-editor\/\"><strong>How to Search to Find a Word in Vim or Vi Text Editor<\/strong><\/a> to help you in fixing your script.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Making_the_Bash_Script_Executable_Essential_Steps\"><\/span><strong>Making the Bash Script Executable: Essential Steps<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Before running your Bash script, you need to make it executable. This is done using the chmod command in the Terminal.&nbsp;<\/p>\n\n\n\n<p>Navigate to the directory where your script is located and use the command chmod +x yourscript.sh.&nbsp;<\/p>\n\n\n\n<p>This command changes the file permissions, allowing the script to be executed. Without this step, you would have to manually specify the interpreter every time you run the script.&nbsp;<\/p>\n\n\n\n<p>Before you can run your <strong>Bash script directly<\/strong>, you need to make it executable. Here&#8217;s how:<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"1_Check_the_Current_Permissions_of_Your_Script\"><\/span><strong>1: Check the Current Permissions of Your Script<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"ls -l myscript.sh\n\nIf the script is not executable, you'll see something like -rw-r--r--. The x (executable) permission is missing.\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #D8DEE9\">ls<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">-<\/span><span style=\"color: #D8DEE9\">l<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">myscript<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #D8DEE9\">sh<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9\">If<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">the<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">script<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">is<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">not<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">executable<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">you<\/span><span style=\"color: #ECEFF4\">&#39;<\/span><span style=\"color: #A3BE8C\">ll see something like -rw-r--r--. The x (executable) permission is missing<\/span><span style=\"color: #D8DEE9\">.<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"2_Change_the_Permissions_to_Make_It_Executable\"><\/span><strong>2: Change the Permissions to Make It Executable<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p>To make the script executable, use the chmod command:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"chmod +x myscript.sh\n\nThis command adds the execute permission to the script.\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #D8DEE9\">chmod<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">+<\/span><span style=\"color: #D8DEE9\">x<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">myscript<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #D8DEE9\">sh<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9\">This<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">command<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">adds<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">the<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">execute<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">permission<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">to<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">the<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">script<\/span><span style=\"color: #ECEFF4\">.<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"3_Verify_the_Permissions_to_Ensure_Proper_Access\"><\/span><strong>3: Verify the Permissions to Ensure Proper Access<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p>Check the permissions again using ls -l:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"ls -l myscript.sh\n\nYou should now see something like -rwxr-xr-x, indicating that the script is executable.\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #D8DEE9\">ls<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">-<\/span><span style=\"color: #D8DEE9\">l<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">myscript<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #D8DEE9\">sh<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9\">You<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">should<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">now<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">see<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">something<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">like<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">-<\/span><span style=\"color: #D8DEE9\">rwxr<\/span><span style=\"color: #81A1C1\">-<\/span><span style=\"color: #D8DEE9\">xr<\/span><span style=\"color: #81A1C1\">-<\/span><span style=\"color: #D8DEE9\">x<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">indicating<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">that<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">the<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">script<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">is<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">executable<\/span><span style=\"color: #ECEFF4\">.<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>With the script now executable, you&#8217;re ready to run it in the terminal.<\/p>\n\n\n\n<p>By making the script executable, you streamline the process, allowing you to run it with a simple command.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Running_the_Bash_Script_in_Terminal_Different_Approaches\"><\/span><strong>Running the Bash Script in Terminal: Different Approaches<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Once your Bash script is executable, there are several ways to run it in the Terminal. The simplest method is to use <strong>.\/yourscript.sh,<\/strong> which runs the script in the current directory.\u00a0<\/p>\n\n\n\n<p>Now that your script is executable, you can run it in the terminal. Here are a few ways to do that:<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"1_Running_the_Script_Directly_from_the_Terminal\"><\/span><strong>1: Running the Script Directly from the Terminal<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p>Simply type:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\".\/myscript.sh\n\nThis will execute the script, and you should see the output Hello, World! in the terminal.\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #81A1C1\">\/<\/span><span style=\"color: #D8DEE9\">myscript<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #D8DEE9\">sh<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9\">This<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">will<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">execute<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">the<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">script<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">and<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">you<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">should<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">see<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">the<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">output<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">Hello<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">World<\/span><span style=\"color: #81A1C1\">!<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">in<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">the<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">terminal<\/span><span style=\"color: #ECEFF4\">.<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"2_Using_the_bash_Command_for_Execution\"><\/span>2: Using the bash Command for Execution<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p>You can also run the script by typing:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"bash myscript.sh\n\nThis method works even if the script is not executable.\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #D8DEE9\">bash<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">myscript<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #D8DEE9\">sh<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9\">This<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">method<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">works<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">even<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">if<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">the<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">script<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">is<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">not<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">executable<\/span><span style=\"color: #ECEFF4\">.<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"3_Using_the_sh_Command_to_Run_Your_Script\"><\/span><strong>3: Using the sh Command to Run Your Script<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p>Similarly, you can run the script with the sh command:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"sh myscript.sh\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #D8DEE9\">sh<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">myscript<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #D8DEE9\">sh<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"4_Running_with_a_Relative_or_Absolute_Path_for_Access\"><\/span><strong>4: Running with a Relative or Absolute Path for Access<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p>If your script is located in a different directory, you can specify the relative or absolute path to run it:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"\/path\/to\/myscript.sh\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #81A1C1\">\/<\/span><span style=\"color: #D8DEE9\">path<\/span><span style=\"color: #81A1C1\">\/<\/span><span style=\"color: #D8DEE9\">to<\/span><span style=\"color: #81A1C1\">\/<\/span><span style=\"color: #D8DEE9\">myscript<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #D8DEE9\">sh<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>These methods allow you to execute your Bash script in different scenarios, depending on your preferences and requirements.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/arzhost.com\/blogs\/wp-content\/uploads\/2024\/09\/How-to-Run-a-Bash-Script-in-Terminal-Steps.jpg\"><img fetchpriority=\"high\" decoding=\"async\" width=\"1024\" height=\"536\" title=\"How to Run a Bash Script in Terminal Steps\" src=\"https:\/\/arzhost.com\/blogs\/wp-content\/uploads\/2024\/09\/How-to-Run-a-Bash-Script-in-Terminal-Steps-1024x536.jpg\" alt=\"How to Run a Bash Script in Terminal Steps\" class=\"wp-image-10403\" srcset=\"https:\/\/arzhost.com\/blogs\/wp-content\/uploads\/2024\/09\/How-to-Run-a-Bash-Script-in-Terminal-Steps-1024x536.jpg 1024w, https:\/\/arzhost.com\/blogs\/wp-content\/uploads\/2024\/09\/How-to-Run-a-Bash-Script-in-Terminal-Steps-300x157.jpg 300w, https:\/\/arzhost.com\/blogs\/wp-content\/uploads\/2024\/09\/How-to-Run-a-Bash-Script-in-Terminal-Steps-768x402.jpg 768w, https:\/\/arzhost.com\/blogs\/wp-content\/uploads\/2024\/09\/How-to-Run-a-Bash-Script-in-Terminal-Steps-150x79.jpg 150w, https:\/\/arzhost.com\/blogs\/wp-content\/uploads\/2024\/09\/How-to-Run-a-Bash-Script-in-Terminal-Steps-450x236.jpg 450w, https:\/\/arzhost.com\/blogs\/wp-content\/uploads\/2024\/09\/How-to-Run-a-Bash-Script-in-Terminal-Steps.jpg 1200w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n\n\n\n<p>Alternatively, you can run it using bash yourscript.sh, which does not require making the script executable but will run the script in a new shell. Another option is to use sh yourscript.sh, which is similar to using bash but invokes a different shell.&nbsp;<\/p>\n\n\n\n<p>Finally, you can use source yourscript.sh, which runs the script in the current shell environment, useful for setting environment variables or modifying the current shell session. Learn <a href=\"https:\/\/arzhost.com\/blogs\/increase-disk-space-in-ubuntu-command-line\/\"><strong>How to Increase Disk Space in the Ubuntu Command-Line<\/strong><\/a> to increase your device speed and storage.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Debugging_Bash_Scripts_Tips_for_Troubleshooting\"><\/span><strong>Debugging Bash Scripts: Tips for Troubleshooting<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Debugging Bash scripts can be done using several built-in tools and techniques. The bash -x yourscript.sh command runs your script in debug mode, printing each command and its arguments as they are executed.&nbsp;<\/p>\n\n\n\n<p>Even with simple scripts, you might encounter errors or unexpected behavior. Here are some tips to debug your Bash scripts:<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"1_Use_set_-x_for_Debugging_Enabling_Trace_Mode\"><\/span><strong>1: Use set -x for Debugging: Enabling Trace Mode<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p>Add set -x at the beginning of your script to see a trace of commands as they execute:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"#!\/bin\/bash\n\nset -x\n\necho &quot;Debugging mode enabled&quot;\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #616E88\">#!\/bin\/bash<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9\">set<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">-<\/span><span style=\"color: #D8DEE9\">x<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9\">echo<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">Debugging mode enabled<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"2_Check_for_Syntax_Errors_Common_Issues_and_Fixes\"><\/span><strong>2: Check for Syntax Errors: Common Issues and Fixes<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"bash -n myscript.sh\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #D8DEE9\">bash<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">-<\/span><span style=\"color: #D8DEE9\">n<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">myscript<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #D8DEE9\">sh<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"3_Use_echo_Statements_for_Output_Debugging\"><\/span><strong>3: Use echo Statements for Output Debugging<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Insert echo statements to print variables and checkpoints within the script. This helps you understand where the script is failing.<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"4_Review_Exit_Statuses_to_Understand_Script_Behavior\"><\/span><strong>4: Review Exit Statuses to Understand Script Behavior<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Check the exit status of commands using $. A non-zero exit status indicates an error.<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"5_Consult_the_Bash_Manual_for_Reference_and_Guidance\"><\/span><strong>5: Consult the Bash Manual for Reference and Guidance<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use the man bash command to access the Bash manual, which contains detailed information on debugging and scripting techniques.<\/li>\n<\/ul>\n\n\n\n<div class=\"wp-block-uagb-call-to-action uagb-block-cf5921a7 wp-block-button uag-blocks-common-selector\" style=\"--z-index-desktop:479;;--z-index-tablet:undefined;;--z-index-mobile:undefined;\"><div class=\"uagb-cta__wrap\"><h2 class=\"uagb-cta__title\"><span class=\"ez-toc-section\" id=\"Launch_Your_Own_Hosting_Business%E2%80%94Sign_Up_Now\"><\/span><a href=\"https:\/\/arzhost.com\/reseller-hosting\/\" data-type=\"link\" data-id=\"https:\/\/arzhost.com\/reseller-hosting\/\">Launch Your Own Hosting Business\u2014Sign Up Now!<\/a><span class=\"ez-toc-section-end\"><\/span><\/h2><p class=\"uagb-cta__desc\">Start Reselling Hosting with Confidence &#8211; Economy Plan starts at <strong>$12.99\/-<\/strong><\/p><\/div><div class=\"uagb-cta__buttons\"><a href=\"https:\/\/arzhost.com\/reseller-hosting\/\" class=\"uagb-cta__button-link-wrapper wp-block-button__link\" target=\"_self\" rel=\"noopener noreferrer\">Read More<\/a><\/div><\/div>\n\n\n\n<p><strong>Debugging is an essential skill for any programmer<\/strong>. With practice, you&#8217;ll become more proficient in identifying and fixing issues in your scripts.<\/p>\n\n\n\n<p>This is helpful for identifying errors in logic or syntax. You can also use <strong>echo statements<\/strong> within your script to print variable values at different stages of execution, allowing you to trace the script&#8217;s flow.\u00a0<\/p>\n\n\n\n<p>Additionally, you can use the trap command to capture and handle errors or signals, providing more control over your script\u2019s behavior during execution. By systematically debugging your script, you can ensure it runs as intended.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Conclusion_Summary_of_Key_Points_and_Best_Practices\"><\/span><strong>Conclusion: Summary of Key Points and Best Practices<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Running Bash scripts in the terminal is a fundamental skill for anyone working with Unix-based systems. By following this step-by-step tutorial, you can create, execute, and debug your Bash scripts with confidence.&nbsp;<\/p>\n\n\n\n<p>Whether you&#8217;re automating simple tasks or developing complex scripts, mastering these basics will greatly enhance your productivity and efficiency.<\/p>\n\n\n\n<p>Now that you&#8217;ve learned how to run a Bash script in the terminal, you can start experimenting with more advanced scripting techniques.\u00a0To get Reliable and Scalable Hosting Services and other Services, Visit <a href=\"https:\/\/arzhost.com\/\"><strong>ARZ Host<\/strong><\/a>.<\/p>\n\n\n\n<p>Happy scripting!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"FAQs_Frequently_Asked_Questions\"><\/span><strong>FAQs (Frequently Asked Questions)<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"1_What_is_a_Bash_script_and_why_would_I_want_to_run_it_in_the_terminal\"><\/span><strong>1: What is a Bash script, and why would I want to run it in the terminal?<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>A Bash script is a text file containing a series of commands that are executed by the Bash shell. Bash (Bourne Again SHell) is a command processor that typically runs in a text window where the user types commands.&nbsp;<\/p>\n\n\n\n<p>A script can automate repetitive tasks, manage system operations, or configure software environments, saving you time and reducing the risk of errors.&nbsp;<\/p>\n\n\n\n<p>Running a Bash script in the terminal allows you to execute these commands in sequence, enabling you to perform complex tasks quickly and efficiently. It&#8217;s a powerful tool for both system administrators and developers.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"2_How_do_I_create_a_Bash_script_to_run_in_the_terminal\"><\/span><strong>2: How do I create a Bash script to run in the terminal?<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>To create a Bash script, follow these steps:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Open a text editor:<\/strong> You can use any text editor like nano, vi, or gedit.<\/li>\n\n\n\n<li><strong>Write your script:<\/strong> Begin with a shebang line (#! \/bin\/bash) at the top of the file, which tells the system that the script should be run using the Bash shell. Then, add the commands you want to execute, each on a new line.<\/li>\n\n\n\n<li><strong>Save the file:<\/strong> Save your script with a .sh extension, for example, myscript.sh.<\/li>\n\n\n\n<li><strong>Make the script executable: <\/strong>To run the script directly, you need to make it executable by using the command chmod +x myscript.sh.<\/li>\n<\/ul>\n\n\n\n<p>This process creates a simple but functional script that you can run in the terminal to automate tasks.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"3_How_can_I_make_my_Bash_script_executable_and_why_is_this_step_necessary\"><\/span><strong>3: How can I make my Bash script executable, and why is this step necessary?<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Making your Bash script executable is essential because it allows the operating system to recognize the file as a program that can be run directly. Without this step, you would have to specify the interpreter manually each time you run the script (e.g., bash myscript.sh).&nbsp;<\/p>\n\n\n\n<p>To make the script executable, use the chmod command as follows:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>chmod +x myscript.sh<\/li>\n<\/ul>\n\n\n\n<p>This command modifies the file&#8217;s permissions, giving the user the right to execute the file. Once executable, you can run the script by typing .\/myscript.sh in the terminal. This step simplifies the execution process and makes your scripts easier to manage and use.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"4_How_do_I_run_a_Bash_script_in_the_terminal_and_what_happens_when_I_do\"><\/span><strong>4: How do I run a Bash script in the terminal, and what happens when I do?<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>To run a Bash script in the terminal, navigate to the directory where your script is located using the cd command.&nbsp;<\/p>\n\n\n\n<p>Once you&#8217;re in the correct directory, you can execute the script using the following command:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>.\/myscript.sh<\/li>\n<\/ul>\n\n\n\n<p>When you run the script, the Bash shell reads the file line by line and executes each command in sequence. If there are any errors in the script, they will be displayed in the terminal, allowing you to troubleshoot and debug.&nbsp;<\/p>\n\n\n\n<p>The output of each command is shown in real time, and the script continues running until it completes all the commands or encounters an error.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"5_What_should_I_do_if_my_Bash_script_doesnt_run_as_expected\"><\/span><strong>5: What should I do if my Bash script doesn\u2019t run as expected?<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>If your Bash script doesn\u2019t run as expected, follow these troubleshooting steps:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Check for syntax errors:<\/strong> Ensure that your script is free of syntax errors. Even a small mistake can cause the script to fail. Tools like shell check can help identify issues.<\/li>\n\n\n\n<li><strong>Verify permissions:<\/strong> Make sure the script is executable by running chmod +x myscript.sh.<\/li>\n\n\n\n<li><strong>Use debugging options:<\/strong> Run the script with the -x option <strong>(e.g., bash -x myscript.sh)<\/strong> to see each command executed and its output. This can help pinpoint where the script is failing.<\/li>\n\n\n\n<li><strong>Check the environment:<\/strong> Ensure that the script is being run in the correct environment, with all required variables and paths correctly set.<\/li>\n\n\n\n<li><strong>Review error messages:<\/strong> The terminal will display error messages if something goes wrong. These messages can provide usually useful clues about what needs to be fixed.<\/li>\n<\/ul>\n\n\n\n<p>By methodically checking each aspect of the script and using the built-in debugging tools, you can identify and correct issues to ensure your script runs smoothly.<\/p>\n\n\n\n<p><strong>Latest Posts<\/strong>:<\/p>\n\n\n<ul class=\"wp-block-latest-posts__list wp-block-latest-posts\"><li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/arzhost.com\/blogs\/how-to-fix-403-forbidden-error-wordpress\/\">How To Fix 403 Forbidden Error WordPress<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/arzhost.com\/blogs\/how-to-get-the-most-out-of-claude-ai\/\">How To Get The Most Out Of Claude Ai<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/arzhost.com\/blogs\/bad-gateway-error-502-the-ultimate-guide-to-quick-fixes\/\">Bad Gateway Error (502): The Ultimate Guide to Quick Fixes<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/arzhost.com\/blogs\/a-deep-dive-into-todays-best-linux-distros\/\">A Deep Dive Into Today\u2019s Best Linux Distros<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/arzhost.com\/blogs\/domain-investor-terms-powerful-strategy\/\">Domain Investor Terms: Expert Insight on Powerful Strategy<\/a><\/li>\n<\/ul>\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction to Bash Scripting: An Overview Running Bash scripts in the terminal is a powerful way to automate tasks, streamline processes, and manage system operations more efficiently.\u00a0 Whether you&#8217;re a beginner or an experienced programmer, understanding how to run a Bash script in the terminal can enhance your productivity.\u00a0 This article will guide you through [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":10402,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"table_tags":[],"class_list":["post-10401","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-general"],"_links":{"self":[{"href":"https:\/\/arzhost.com\/blogs\/wp-json\/wp\/v2\/posts\/10401","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/arzhost.com\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/arzhost.com\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/arzhost.com\/blogs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/arzhost.com\/blogs\/wp-json\/wp\/v2\/comments?post=10401"}],"version-history":[{"count":1,"href":"https:\/\/arzhost.com\/blogs\/wp-json\/wp\/v2\/posts\/10401\/revisions"}],"predecessor-version":[{"id":10404,"href":"https:\/\/arzhost.com\/blogs\/wp-json\/wp\/v2\/posts\/10401\/revisions\/10404"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/arzhost.com\/blogs\/wp-json\/wp\/v2\/media\/10402"}],"wp:attachment":[{"href":"https:\/\/arzhost.com\/blogs\/wp-json\/wp\/v2\/media?parent=10401"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/arzhost.com\/blogs\/wp-json\/wp\/v2\/categories?post=10401"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/arzhost.com\/blogs\/wp-json\/wp\/v2\/tags?post=10401"},{"taxonomy":"table_tags","embeddable":true,"href":"https:\/\/arzhost.com\/blogs\/wp-json\/wp\/v2\/table_tags?post=10401"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}