{"id":128040,"date":"2025-12-02T16:01:14","date_gmt":"2025-12-02T16:01:14","guid":{"rendered":"\/uk\/tutorials\/how-to-run-sh-file-in-linux"},"modified":"2026-05-07T11:22:28","modified_gmt":"2026-05-07T11:22:28","slug":"how-to-run-sh-file-in-linux","status":"publish","type":"post","link":"\/uk\/tutorials\/how-to-run-sh-file-in-linux\/","title":{"rendered":"How to run the .sh file in Linux"},"content":{"rendered":"<p>A <strong>.sh<\/strong> file in Linux is an executable shell script containing a series of commands that will run sequentially. It&rsquo;s commonly used to streamline complex operations, automate tasks, and run programs.<\/p><p>Understanding how to create and run a shell file is crucial when you&rsquo;re managing a Linux system. It helps make the administration process more efficient and less prone to errors.<\/p><p>In this tutorial, you&rsquo;ll learn how to run a .sh file in Linux using the command line and the graphical interface. We&rsquo;ll also explain how to create this file and set the correct permissions for seamless execution.<\/p><p>\n\n\n\n<\/p><h2 class=\"wp-block-heading\" id=\"h-tldr\">TL;DR<\/h2><ul class=\"wp-block-list\">\n<li>To run a <code>.sh<\/code> file in Linux, create the script, add a shebang like <code>#!\/bin\/bash<\/code>, and save it with the <code>.sh<\/code> extension.<\/li>\n\n\n\n<li>Before execution, grant the file executable permission with <code>sudo chmod +x script.sh<\/code> or the GUI Permissions menu.<\/li>\n\n\n\n<li>Then run it with <code>.\/script.sh<\/code>, pass it to <code>bash<\/code> or <code>sh<\/code>, execute it as another user with <code>runuser<\/code>, or keep it running in the background with <code>&amp;<\/code> or <code>nohup<\/code>.<\/li>\n<\/ul><h2 class=\"wp-block-heading\" id=\"h-what-is-a-sh-file\"><strong>What is a .sh file?<\/strong><\/h2><p>A<strong> .sh<\/strong> or<strong> shell script<\/strong> file is an executable item containing a series of commands. When you run it, your shell or command-line interpreter will read its content and iterate through the commands.<\/p><p>This file is commonly used in system administration because of its benefits, such as:<\/p><ul class=\"wp-block-list\">\n<li><strong>Efficiency<\/strong> &ndash; instead of running multiple commands individually, you can complete a complex task by executing a single shell script file.<\/li>\n\n\n\n<li><strong>Consistency<\/strong> &ndash; a .sh file always performs the same task and gives the same output, minimizing human errors.<\/li>\n\n\n\n<li><strong>Automation <\/strong>&ndash; using <a href=\"\/uk\/tutorials\/cron-job\/\">cron job<\/a>, you can automatically run a .sh file at a specific interval or a particular time to streamline repetitive tasks.&nbsp;<\/li>\n\n\n\n<li><strong>Reusability<\/strong> &ndash; you can easily share a .sh file with other team members or reuse it in another project to perform the same task.&nbsp;<\/li>\n\n\n\n<li><strong>Maintainability<\/strong> &ndash; .sh files are easily edited, tested, and debugged because they are similar to other text files.<\/li>\n<\/ul><h2 class=\"wp-block-heading\" id=\"h-how-to-create-a-sh-file\"><strong>How to create a .sh file?<\/strong><\/h2><p>The steps to create a .sh file are the same regardless of your chosen Linux operating system. However, they differ depending on whether you use a command line or a graphical user interface (GUI).<\/p><p><strong>Creating a .sh file with commands<\/strong><\/p><p>Before creating a .sh file, open your system&rsquo;s terminal to access its command-line interface (CLI). If you&rsquo;re working with a remote Linux server, <a href=\"\/uk\/tutorials\/how-to-use-putty-ssh\/\">connect to it via SSH using PuTTY<\/a> or terminal.<\/p><p>Alternatively, <a href=\"\/uk\/vps-hosting\">Hostinger Linux VPS hosting<\/a> users can easily connect to their server directly via their web browser using hPanel&rsquo;s <strong>Browser terminal<\/strong>.<\/p><figure class=\"wp-block-image size-large\"><a href=\"\/uk\/vps-hosting\" target=\"_blank\" rel=\"noreferrer noopener\"><img decoding=\"async\" width=\"1024\" height=\"300\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/02\/VPS-hosting-banner.png\/w=1024,h=1024,fit=scale-down\" alt=\"\" class=\"wp-image-77934\" srcset=\"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2023\/02\/VPS-hosting-banner.png 1024w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2023\/02\/VPS-hosting-banner-300x88.png 300w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2023\/02\/VPS-hosting-banner-150x44.png 150w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2023\/02\/VPS-hosting-banner-768x225.png 768w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure><p>Once connected, follow these steps to create a new shell script file:<\/p><ol class=\"wp-block-list\">\n<li>Run the following command to create a blank <strong>.sh<\/strong> file. Replace <strong>script <\/strong>with your desired name:<\/li>\n<\/ol><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=\"\">sudo touch script.sh<\/pre><ol start=\"2\" class=\"wp-block-list\">\n<li>Open the new file with your preferred text editor. In this tutorial, we will use <a href=\"\/uk\/tutorials\/how-to-install-and-use-nano-text-editor\/\">Nano<\/a>:<\/li>\n<\/ol><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=\"\">sudo nano script.sh<\/pre><ol start=\"3\" class=\"wp-block-list\">\n<li>The text editor will open. Write the following <strong>shebang <\/strong>line at the top to indicate which shell interpreter your system should use to read the file&rsquo;s content. For example, we will make a bash script:<\/li>\n<\/ol><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><ol start=\"4\" class=\"wp-block-list\">\n<li>In the next line, add a few commands in chronological order. It might look like this:<\/li>\n<\/ol><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=\"\"># Update the repo\n\nsudo apt update\n\n# Install available updates\n\nsudo apt upgrade<\/pre><ol start=\"5\" class=\"wp-block-list\">\n<li>Once finished, hit<strong> Ctrl + X<\/strong>, <strong>Y<\/strong>, then<strong> Enter<\/strong> to save the file.&nbsp;<\/li>\n<\/ol><div class=\"wp-block-image\"><figure data-wp-context='{\"imageId\":\"6a588524d47c6\"}' data-wp-interactive=\"core\/image\" data-wp-key=\"6a588524d47c6\" class=\"aligncenter size-large wp-lightbox-container\"><img decoding=\"async\" data-wp-class--hide=\"state.isContentHidden\" data-wp-class--show=\"state.isContentVisible\" data-wp-init=\"callbacks.setButtonStyles\" data-wp-on--click=\"actions.showLightbox\" data-wp-on--load=\"callbacks.setButtonStyles\" data-wp-on-window--resize=\"callbacks.setButtonStyles\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2025\/05\/shell-script-content.png\/w=1024,h=1024,fit=scale-down\" alt=\"The content of a shell script on terminal\" class=\"wp-image-127690\"><button class=\"lightbox-trigger\" type=\"button\" aria-haspopup=\"dialog\" aria-label=\"Enlarge\" data-wp-init=\"callbacks.initTriggerButton\" data-wp-on--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><div class=\"announcement-block announcement-block--important\">\n            <span class=\"announcement-block__heading\">\n                <svg width=\"24\" height=\"24\" viewBox=\"0 0 24 24\" fill=\"none\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\">\n                    <path fill-rule=\"evenodd\" clip-rule=\"evenodd\"\n                          d=\"M12 22.5C17.799 22.5 22.5 17.799 22.5 12C22.5 6.20101 17.799 1.5 12 1.5C6.20101 1.5 1.5 6.20101 1.5 12C1.5 17.799 6.20101 22.5 12 22.5ZM13.637 7.65198C13.637 6.74791 12.9041 6.01501 12 6.01501C11.0959 6.01501 10.363 6.74791 10.363 7.65198C10.5335 9.53749 10.875 13.383 10.875 13.383C10.875 14.0043 11.3787 14.508 12 14.508C12.6213 14.508 13.125 14.0043 13.125 13.383V13.38L13.637 7.65198ZM11.9927 15.714C11.3714 15.714 10.8677 16.2177 10.8677 16.839C10.8677 17.4603 11.3714 17.964 11.9927 17.964H12.0073C12.6286 17.964 13.1323 17.4603 13.1323 16.839C13.1323 16.2177 12.6286 15.714 12.0073 15.714H11.9927Z\"\n                          fill=\"#FEA419\"\/>\n                <\/svg>\n                Important\n            <\/span>\n            <p class=\"announcement-block__content\">\n                <strong>Important!<\/strong> Your system shell interpreter will ignore lines that start with a hash mark (#), except the shebang. This is commonly used to add comments or notes, providing anyone reading or editing the script with more context.\n            <\/p><\/div>\n\n\n\n<\/p><p><strong>Creating a .sh file via GUI<\/strong><\/p><p>Here&rsquo;s how to create a .sh file via GUI on your Linux desktop. The general procedure should be the same regardless of your distribution, but the button location might differ slightly.<\/p><p>In this tutorial, we&rsquo;ll show you how to do it in <strong>Ubuntu<\/strong>:<\/p><ol class=\"wp-block-list\">\n<li>Click <strong>Show Applications<\/strong> from your desktop&rsquo;s launcher.<\/li>\n<\/ol><div class=\"wp-block-image\"><figure data-wp-context='{\"imageId\":\"6a588524d6505\"}' data-wp-interactive=\"core\/image\" data-wp-key=\"6a588524d6505\" class=\"aligncenter size-large wp-lightbox-container\"><img decoding=\"async\" width=\"1600\" height=\"900\" data-wp-class--hide=\"state.isContentHidden\" data-wp-class--show=\"state.isContentVisible\" data-wp-init=\"callbacks.setButtonStyles\" data-wp-on--click=\"actions.showLightbox\" data-wp-on--load=\"callbacks.setButtonStyles\" data-wp-on-window--resize=\"callbacks.setButtonStyles\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/51\/2025\/11\/Horizons_tutorials.png\/w=1024,h=1024,fit=scale-down\" alt=\"The Show Applications button on Ubuntu's launcher\" class=\"wp-image-127691\" srcset=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/51\/2025\/11\/Horizons_tutorials.png\/w=1600,fit=scale-down 1600w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/51\/2025\/11\/Horizons_tutorials.png\/w=300,fit=scale-down 300w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/51\/2025\/11\/Horizons_tutorials.png\/w=1024,fit=scale-down 1024w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/51\/2025\/11\/Horizons_tutorials.png\/w=150,fit=scale-down 150w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/51\/2025\/11\/Horizons_tutorials.png\/w=768,fit=scale-down 768w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/51\/2025\/11\/Horizons_tutorials.png\/w=1536,fit=scale-down 1536w\" sizes=\"(max-width: 1600px) 100vw, 1600px\" \/><button class=\"lightbox-trigger\" type=\"button\" aria-haspopup=\"dialog\" aria-label=\"Enlarge\" data-wp-init=\"callbacks.initTriggerButton\" data-wp-on--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><ol start=\"2\" class=\"wp-block-list\">\n<li>Select <strong>Text Editor<\/strong>.&nbsp;<\/li>\n\n\n\n<li>Write the shebang and your commands. Hit <strong>Save<\/strong>.&nbsp;<\/li>\n<\/ol><div class=\"wp-block-image\"><figure data-wp-context='{\"imageId\":\"6a588524d803d\"}' data-wp-interactive=\"core\/image\" data-wp-key=\"6a588524d803d\" class=\"aligncenter size-full wp-lightbox-container\"><img decoding=\"async\" data-wp-class--hide=\"state.isContentHidden\" data-wp-class--show=\"state.isContentVisible\" data-wp-init=\"callbacks.setButtonStyles\" data-wp-on--click=\"actions.showLightbox\" data-wp-on--load=\"callbacks.setButtonStyles\" data-wp-on-window--resize=\"callbacks.setButtonStyles\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2025\/05\/sh-file-content-in-gui-text-editor.png\/public\" alt=\"sh file content in GUI Text Editor\n\" class=\"wp-image-127692\"><button class=\"lightbox-trigger\" type=\"button\" aria-haspopup=\"dialog\" aria-label=\"Enlarge\" data-wp-init=\"callbacks.initTriggerButton\" data-wp-on--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><ol start=\"4\" class=\"wp-block-list\">\n<li>Select the location where you want to save the script. Click <strong>Save<\/strong> to confirm.<\/li>\n\n\n\n<li>Enter a name for your script. Make sure it ends with the <strong>.sh <\/strong>extension.&nbsp;<\/li>\n\n\n\n<li>Hit <strong>Save<\/strong>.&nbsp;<\/li>\n<\/ol><h2 class=\"wp-block-heading\" id=\"h-setting-permissions-to-run-a-sh-file\"><strong>Setting permissions to run a .sh file<\/strong><\/h2><p>For security reasons, you can&rsquo;t execute .sh files in Linux by default. You must set the appropriate permissions before running them.<\/p><p>To make the file executable, add the corresponding permission <a href=\"\/uk\/tutorials\/how-to-change-linux-permissions-and-owners\/\">using the chmod command<\/a> as below. Remember to replace the file name accordingly:<\/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=\"\">sudo chmod +x script.sh<\/pre><p>Note that this command will only grant executable permission to the file owner or creator. This means another user can&rsquo;t run the script as a program unless using <strong>sudo<\/strong>.<\/p><p><div class=\"announcement-block announcement-block--important\">\n            <span class=\"announcement-block__heading\">\n                <svg width=\"24\" height=\"24\" viewBox=\"0 0 24 24\" fill=\"none\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\">\n                    <path fill-rule=\"evenodd\" clip-rule=\"evenodd\"\n                          d=\"M12 22.5C17.799 22.5 22.5 17.799 22.5 12C22.5 6.20101 17.799 1.5 12 1.5C6.20101 1.5 1.5 6.20101 1.5 12C1.5 17.799 6.20101 22.5 12 22.5ZM13.637 7.65198C13.637 6.74791 12.9041 6.01501 12 6.01501C11.0959 6.01501 10.363 6.74791 10.363 7.65198C10.5335 9.53749 10.875 13.383 10.875 13.383C10.875 14.0043 11.3787 14.508 12 14.508C12.6213 14.508 13.125 14.0043 13.125 13.383V13.38L13.637 7.65198ZM11.9927 15.714C11.3714 15.714 10.8677 16.2177 10.8677 16.839C10.8677 17.4603 11.3714 17.964 11.9927 17.964H12.0073C12.6286 17.964 13.1323 17.4603 13.1323 16.839C13.1323 16.2177 12.6286 15.714 12.0073 15.714H11.9927Z\"\n                          fill=\"#FEA419\"\/>\n                <\/svg>\n                Important\n            <\/span>\n            <p class=\"announcement-block__content\">\n                <strong>Important!<\/strong> While you can add executable permission for all users, we don&prime;t recommend it for security reasons. The best practice is to only make a bash script executable for the owner.\n            <\/p><\/div>\n\n\n\n<\/p><p>You can also change your .sh file permission via the GUI. For instance, here&rsquo;s how to do it in Ubuntu:<\/p><ol class=\"wp-block-list\">\n<li>Right-click your .sh file and select <strong>Properties<\/strong>.&nbsp;&nbsp;<\/li>\n\n\n\n<li>Select the <strong>Permissions<\/strong> tab.<\/li>\n<\/ol><div class=\"wp-block-image\"><figure data-wp-context='{\"imageId\":\"6a588524d9ceb\"}' data-wp-interactive=\"core\/image\" data-wp-key=\"6a588524d9ceb\" class=\"aligncenter size-full wp-lightbox-container\"><img decoding=\"async\" data-wp-class--hide=\"state.isContentHidden\" data-wp-class--show=\"state.isContentVisible\" data-wp-init=\"callbacks.setButtonStyles\" data-wp-on--click=\"actions.showLightbox\" data-wp-on--load=\"callbacks.setButtonStyles\" data-wp-on-window--resize=\"callbacks.setButtonStyles\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2025\/05\/file-permission-configuration-menu-in-linux-gui.png\/public\" alt=\"The file permission configuration menu in Linux's graphical user interface\" class=\"wp-image-127696\"><button class=\"lightbox-trigger\" type=\"button\" aria-haspopup=\"dialog\" aria-label=\"Enlarge\" data-wp-init=\"callbacks.initTriggerButton\" data-wp-on--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><ol start=\"3\" class=\"wp-block-list\">\n<li>Check the <strong>Allow executing file as program<\/strong> box.<\/li>\n\n\n\n<li>Close the dialog window.&nbsp;<\/li>\n<\/ol><p>Although you can execute a .sh file, you might be unable to run all its content. When the script runs, a permission error might still arise.<\/p><p>This happens because the current user doesn&rsquo;t have enough permissions to run commands or modify an item specified in the .sh file.<\/p><h2 class=\"wp-block-heading\" id=\"h-running-a-sh-file\"><strong>Running a .sh file<\/strong><\/h2><p>There are various ways to run a .sh file for specific use cases. In this section, we&rsquo;ll cover four of the most common ones.<\/p><h3 class=\"wp-block-heading\" id=\"h-running-a-sh-file-normally\"><strong>Running a .sh file normally<\/strong><\/h3><p>The most basic way to run a .sh file is by entering its name in your command-line interface like so:<\/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=\"\">.\/script.sh<\/pre><p>This command only works if your<strong> .sh<\/strong> file is located in the current directory. If your script is located in another folder, specify the full path like so:<\/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=\"\">.\/path\/to\/folder\/script.sh<\/pre><p>If you use a GUI, you can easily run a .sh file by right-clicking it and selecting <strong>Run as a Program<\/strong>.<\/p><div class=\"wp-block-image\"><figure data-wp-context='{\"imageId\":\"6a588524db7ae\"}' data-wp-interactive=\"core\/image\" data-wp-key=\"6a588524db7ae\" class=\"aligncenter size-full wp-lightbox-container\"><img decoding=\"async\" data-wp-class--hide=\"state.isContentHidden\" data-wp-class--show=\"state.isContentVisible\" data-wp-init=\"callbacks.setButtonStyles\" data-wp-on--click=\"actions.showLightbox\" data-wp-on--load=\"callbacks.setButtonStyles\" data-wp-on-window--resize=\"callbacks.setButtonStyles\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2025\/05\/the-run-as-a-program-button-on-a-script.png\/public\" alt=\"The Run as a Program button on a script's left-click menu\" class=\"wp-image-127698\"><button class=\"lightbox-trigger\" type=\"button\" aria-haspopup=\"dialog\" aria-label=\"Enlarge\" data-wp-init=\"callbacks.initTriggerButton\" data-wp-on--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>While simple, this method&rsquo;s downside is that your script can only run using the shell interpreter specified in the shebang.<\/p><h3 class=\"wp-block-heading\" id=\"h-using-a-specific-interpreter\"><strong>Using a specific interpreter<\/strong><\/h3><p>In some cases, you may need to run your shell script using a specific interpreter. This is useful in several scenarios, like when you don&rsquo;t have permission to execute the file.<\/p><p>        <div class=\"protip\">\n            <div class=\"protip__heading\">\n                <svg width=\"24\" height=\"24\" viewBox=\"0 0 24 24\" fill=\"none\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\">\n                    <path d=\"M1.49234 23.5024C1.23229 23.5024 0.972242 23.4024 0.782206 23.2123C0.562165 22.9923 0.452144 22.6822 0.502153 22.3722C0.562165 21.9221 1.14227 17.9113 3.00262 16.351C3.63274 15.8209 4.43289 15.5509 5.26305 15.5609C6.09321 15.5909 6.87335 15.9109 7.47347 16.4911C8.6937 17.6913 8.76371 19.6717 7.6435 20.9919C6.0832 22.8523 2.08245 23.4324 1.63237 23.4924C1.59236 23.4924 1.54235 23.4924 1.50234 23.4924L1.49234 23.5024ZM5.16303 17.5613C4.84297 17.5613 4.53291 17.6713 4.29287 17.8813C3.60274 18.4614 3.07264 19.9317 2.75258 21.242C4.06282 20.9219 5.5331 20.3918 6.11321 19.7017C6.55329 19.1716 6.54329 18.3814 6.0832 17.9213C5.85316 17.7013 5.5431 17.5713 5.20304 17.5613C5.19304 17.5613 5.17303 17.5613 5.16303 17.5613ZM11.7243 21.8821C11.4942 21.8821 11.2642 21.8021 11.0841 21.652C10.8541 21.462 10.7241 21.1819 10.7241 20.8819V15.9109L8.08358 13.2705H3.11264C2.81259 13.2705 2.53254 13.1404 2.3425 12.9104C2.15246 12.6803 2.07245 12.3803 2.12246 12.0902C2.19247 11.7102 2.84259 8.36953 4.70294 7.12929C6.33325 6.04909 8.96375 6.49918 10.244 6.80923C11.5442 4.96889 13.2546 3.4286 15.2349 2.33839C17.4553 1.11816 19.9858 0.518051 22.4963 0.498047C23.0464 0.498047 23.4865 0.948132 23.4865 1.49824C23.4865 5.0389 22.3763 9.97983 17.1753 13.7605C17.4853 15.0408 17.9354 17.6613 16.8552 19.2816C15.615 21.1419 12.2744 21.7921 11.8943 21.8621C11.8343 21.8721 11.7743 21.8821 11.7143 21.8821H11.7243ZM12.7245 16.181V19.6016C13.7146 19.2916 14.7948 18.7915 15.2049 18.1814C15.675 17.4812 15.605 16.091 15.385 14.9008C14.5248 15.3808 13.6346 15.8109 12.7245 16.181ZM9.66388 12.0302L11.9643 14.3307C13.1845 13.8306 14.3648 13.2204 15.485 12.5103C19.9358 9.51974 21.2361 5.60901 21.4561 2.53843C19.6157 2.67846 17.8254 3.20856 16.2051 4.09872C14.2847 5.14892 12.6544 6.68921 11.4942 8.54956C10.7841 9.65977 10.174 10.82 9.66388 12.0302ZM4.39289 11.2701H7.81353C8.1936 10.3599 8.63368 9.46974 9.11377 8.60957C7.92355 8.38953 6.51329 8.31952 5.81315 8.78961C5.19304 9.19968 4.70294 10.3099 4.39289 11.2701Z\" fill=\"#673DE6\"\/>\n                <\/svg>\n                <p class=\"protip__title\">\n                    Running a non-executable .sh file                <\/p>\n            <\/div>\n            <p class=\"protip__content\">You can technically run a non-executable script by explicitly specifying the shell interpreter. However, this won&rsquo;t execute the file as a program &ndash; it will only read the file&rsquo;s content.<br>\n<br>\nThis doesn&rsquo;t present security risks since the user still needs to have permission to execute commands and modify files specified in the script.<\/p>\n                    <\/div>\n        \n\n\n\n<\/p><p>You can run your .sh file using a specific shell by specifying it in the beginning of your command. This will overwrite the interpreter specified in the shebang.<\/p><p>There are <a href=\"https:\/\/cyberpanel.net\/blog\/6-types-of-shells-in-linux\" target=\"_blank\" rel=\"noopener\">various other shell interpreters<\/a> with different behaviors. The two most common ones in Linux are <strong>sh<\/strong> and its modern version, <strong>bash<\/strong>. Here&rsquo;s what your commands will look like when running a script using either of those shells:<\/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 script.sh\n\nsh script.sh<\/pre><h3 class=\"wp-block-heading\" id=\"h-running-a-sh-file-as-another-user\"><strong>Running a .sh file as another user<\/strong><\/h3><p>System administrators run .sh files as a specific user or group for a few reasons. For example, dedicating a particular account to execute scripts for many similar operations helps simplify task management.<\/p><p>For security reasons, it&rsquo;s also advisable to run a .sh file as the owner instead of <strong>root<\/strong>. This procedure prevents you from granting excessive privileges during the operation, which can damage another part of your system.<\/p><p>For example: you run a script that cleans up a directory, but the variable specifying the path is missing. If you run this .sh file as root, you&rsquo;ll delete the main <strong>\/home <\/strong>directory. This won&rsquo;t happen with non-root users because their permissions are insufficient.<\/p><p>To run a .sh file as another user, use the following command syntax:<\/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=\"\">runuser -u [username] -- [\/path\/to\/script.sh]<\/pre><p>For example, if you want to run a monitoring script as the <strong>cron<\/strong> user, your command will look like this:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">runuser -u cron --\/program\/automation\/monitoring.sh<\/pre><p>If you wish to run a .sh file as a specific group, simply add the <strong>-g <\/strong>option to the above 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=\"\">runuser -u cron -g automation --\/program\/automation\/monitoring.sh<\/pre><p>In the example above, we are running the <strong>monitoring.sh<\/strong> script as the<strong> cron<\/strong> user within the <strong>automation <\/strong>group.<\/p><h3 class=\"wp-block-heading\" id=\"h-executing-a-script-in-the-background\"><strong>Executing a script in the background<\/strong><\/h3><p>By default, .sh files will run on your main terminal shell. This can be problematic if you&rsquo;re executing a script that needs to run constantly, like for monitoring or logging.<\/p><p>In this case, run your .sh file in the background so you can still use the main terminal shell while the script is running. The easiest way to do this is by appending an ampersand (<strong>&amp;<\/strong>) at the end of your 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=\"\">.\/script.sh &amp;<\/pre><p>Terminal will output the script&rsquo;s process ID, which you can use later to stop the process using the <a href=\"\/uk\/tutorials\/how-to-kill-a-process-in-linux\/\">kill command<\/a>.<\/p><div class=\"wp-block-image\"><figure data-wp-context='{\"imageId\":\"6a588524dd5d1\"}' data-wp-interactive=\"core\/image\" data-wp-key=\"6a588524dd5d1\" class=\"aligncenter size-full wp-lightbox-container\"><img decoding=\"async\" data-wp-class--hide=\"state.isContentHidden\" data-wp-class--show=\"state.isContentVisible\" data-wp-init=\"callbacks.setButtonStyles\" data-wp-on--click=\"actions.showLightbox\" data-wp-on--load=\"callbacks.setButtonStyles\" data-wp-on-window--resize=\"callbacks.setButtonStyles\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2025\/05\/shell-script-process-id-on-temrinal.png\/public\" alt=\"Shell script's process ID on terminal\" class=\"wp-image-127699\"><button class=\"lightbox-trigger\" type=\"button\" aria-haspopup=\"dialog\" aria-label=\"Enlarge\" data-wp-init=\"callbacks.initTriggerButton\" data-wp-on--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 wish to keep the script running in the background after closing terminal, you can use the <a href=\"\/uk\/tutorials\/nohup-command-in-linux\/\"><strong>nohup<\/strong> command<\/a>:<\/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=\"\">nohup .\/script.sh &amp;<\/pre><p>A more organized way to run a script in the background is using a terminal multiplexer like <a href=\"\/uk\/tutorials\/how-to-install-and-use-linux-screen\/\">GNU Screen<\/a> or <a href=\"\/uk\/tutorials\/how-to-use-tmux\/\">tmux<\/a>. This tool creates a virtual shell that can continue running even after you disconnect from the SSH session.<\/p><h2 class=\"wp-block-heading\" id=\"h-conclusion\"><strong>Conclusion<\/strong><\/h2><p>A .sh file is an executable script containing a series of commands. It is mainly used to streamline complex operations, run programs, and automate tasks in Linux.<\/p><p>To create a .sh file, simply use the<strong> touch<\/strong> command, then add commands to the file using a text editor like <strong>Nano<\/strong>. If you&rsquo;re using a GUI, just open a text editor to write and save your script to a chosen location.<\/p><p>Then, make the .sh file executable by adding the appropriate<strong> <\/strong>permission using the <strong>chmod <\/strong>command. You can do it in the GUI by going to the <strong>Permission <\/strong>tab in the file&rsquo;s <strong>Properties <\/strong>menu.<\/p><p>Run the .sh script by specifying its name in the Linux terminal. Alternatively, you can explicitly pass it to a shell interpreter like bash. For GUI users, right-click the file and click <strong>Run as a Program<\/strong>.<\/p><h2 class=\"wp-block-heading\" id=\"h-sh-file-faq\"><strong>.sh file FAQ<\/strong><\/h2><div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1746452242159\"><h3 class=\"schema-faq-question\"><strong>What is a .sh file?<\/strong><\/h3> <p class=\"schema-faq-answer\">A .sh file in Linux is an executable item containing a series of commands. Think of it as a script that defines a sequence of actions your system&rsquo;s command line should complete.&nbsp;<br>This file is useful for automating tasks, running programs, or streamlining command execution. In addition to saving time and effort, it helps reduce human error.<\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1746452249698\"><h3 class=\"schema-faq-question\"><strong>What permissions are needed for a .sh file?<\/strong><\/h3> <p class=\"schema-faq-answer\">A .sh file requires all permissions to run properly, which are <strong>read<\/strong>, <strong>write<\/strong>, and <strong>execute<\/strong>. These ensure you can view, edit, and run the script.&nbsp;<br>However, you typically only grant these privileges to the file owner for security reasons. For other users, they can run the file using <strong>sudo<\/strong>.&nbsp;&nbsp;<\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1746452264010\"><h3 class=\"schema-faq-question\"><strong>Can I edit a .sh file with any text editor?<\/strong><\/h3> <p class=\"schema-faq-answer\">Yes. A .sh bash script works like any other file extension, meaning you can edit it using different text editors. For example, you can open it using Nano, Vim, Gedit, and others.&nbsp;<br>However, make sure you have write permission enabled on the .sh file, because you won&rsquo;t be able to edit it otherwise.&nbsp;<\/p> <\/div> <\/div>\n","protected":false},"excerpt":{"rendered":"<p>A .sh file in Linux is an executable shell script containing a series of commands that will run sequentially. It&rsquo;s commonly used to streamline complex operations, automate tasks, and run programs. Understanding how to create and run a shell file is crucial when you&rsquo;re managing a Linux system. It helps make the administration process more [&#8230;]<\/p>\n<p><a class=\"btn btn-secondary understrap-read-more-link\" href=\"\/uk\/tutorials\/how-to-run-sh-file-in-linux\/\">Read More&#8230;<\/a><\/p>\n","protected":false},"author":337,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"rank_math_title":"How to create and run a .sh file in Linux","rank_math_description":"Here are the steps to run a .sh file in Linux: 1. Create the .sh file; 2. Add your commands; 3. Set the appropriate permissions + more.","rank_math_focus_keyword":".sh file","footnotes":""},"categories":[22640],"tags":[],"class_list":["post-128040","post","type-post","status-publish","format-standard","hentry","category-vps"],"hreflangs":[{"locale":"en-US","link":"https:\/\/www.hostinger.com\/tutorials\/how-to-run-sh-file-in-linux","default":0},{"locale":"en-IN","link":"https:\/\/www.hostinger.com\/in\/tutorials\/how-to-run-sh-file-in-linux","default":0},{"locale":"en-CA","link":"https:\/\/www.hostinger.com\/ca\/tutorials\/how-to-run-sh-file-in-linux","default":0},{"locale":"en-GB","link":"https:\/\/www.hostinger.com\/uk\/tutorials\/how-to-run-sh-file-in-linux","default":0},{"locale":"en-PH","link":"https:\/\/www.hostinger.com\/ph\/tutorials\/how-to-run-sh-file-in-linux","default":0},{"locale":"en-MY","link":"https:\/\/www.hostinger.com\/my\/tutorials\/how-to-run-sh-file-in-linux","default":0},{"locale":"en-AU","link":"https:\/\/www.hostinger.com\/au\/tutorials\/how-to-run-sh-file-in-linux","default":0},{"locale":"en-NG","link":"https:\/\/www.hostinger.com\/ng\/tutorials\/how-to-run-sh-file-in-linux","default":0}],"_links":{"self":[{"href":"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-json\/wp\/v2\/posts\/128040","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-json\/wp\/v2\/users\/337"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-json\/wp\/v2\/comments?post=128040"}],"version-history":[{"count":1,"href":"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-json\/wp\/v2\/posts\/128040\/revisions"}],"predecessor-version":[{"id":131974,"href":"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-json\/wp\/v2\/posts\/128040\/revisions\/131974"}],"wp:attachment":[{"href":"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-json\/wp\/v2\/media?parent=128040"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-json\/wp\/v2\/categories?post=128040"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-json\/wp\/v2\/tags?post=128040"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}