{"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-03-10T10:08:53","modified_gmt":"2026-03-10T10:08:53","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":"<?xml encoding=\"utf-8\" ?><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-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><?xml encoding=\"utf-8\" ?><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:\/\/www.hostinger.com\/tutorials\/wp-content\/uploads\/sites\/2\/2023\/02\/VPS-hosting-banner-1024x300.png\" alt=\"\" class=\"wp-image-77934\" srcset=\"https:\/\/www.hostinger.com\/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\":\"69da5ebc1d39f\"}' data-wp-interactive=\"core\/image\" 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-async--click=\"actions.showLightbox\" data-wp-on-async--load=\"callbacks.setButtonStyles\" data-wp-on-async-window--resize=\"callbacks.setButtonStyles\" src=\"https:\/\/www.hostinger.com\/tutorials\/wp-content\/uploads\/sites\/2\/2025\/05\/shell-script-content-1024x513.png\" 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-async--click=\"actions.showLightbox\" data-wp-style--right=\"state.imageButtonRight\" data-wp-style--top=\"state.imageButtonTop\">\n\t\t\t<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"12\" height=\"12\" fill=\"none\" viewbox=\"0 0 12 12\">\n\t\t\t\t<path fill=\"#fff\" d=\"M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z\"><\/path>\n\t\t\t<\/svg>\n\t\t<\/button><\/figure><\/div><p><div><p class=\"important\"><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.<\/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\":\"69da5ebc1f7f9\"}' data-wp-interactive=\"core\/image\" 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-async--click=\"actions.showLightbox\" data-wp-on-async--load=\"callbacks.setButtonStyles\" data-wp-on-async-window--resize=\"callbacks.setButtonStyles\" src=\"https:\/\/www.hostinger.com\/tutorials\/wp-content\/uploads\/sites\/2\/2025\/05\/the-show-applications-button-on-ubuntu-launcher-1024x641.png\" alt=\"The Show Applications button on Ubuntu's launcher\" class=\"wp-image-127691\"><button class=\"lightbox-trigger\" type=\"button\" aria-haspopup=\"dialog\" aria-label=\"Enlarge\" data-wp-init=\"callbacks.initTriggerButton\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-style--right=\"state.imageButtonRight\" data-wp-style--top=\"state.imageButtonTop\">\n\t\t\t<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"12\" height=\"12\" fill=\"none\" viewbox=\"0 0 12 12\">\n\t\t\t\t<path fill=\"#fff\" d=\"M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z\"><\/path>\n\t\t\t<\/svg>\n\t\t<\/button><\/figure><\/div><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><figure data-wp-context='{\"imageId\":\"69da5ebc21cc6\"}' data-wp-interactive=\"core\/image\" class=\"wp-block-image 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-async--click=\"actions.showLightbox\" data-wp-on-async--load=\"callbacks.setButtonStyles\" data-wp-on-async-window--resize=\"callbacks.setButtonStyles\" src=\"https:\/\/www.hostinger.com\/tutorials\/wp-content\/uploads\/sites\/2\/2025\/05\/sh-file-content-in-gui-text-editor.png\" 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-async--click=\"actions.showLightbox\" data-wp-style--right=\"state.imageButtonRight\" data-wp-style--top=\"state.imageButtonTop\">\n\t\t\t<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"12\" height=\"12\" fill=\"none\" viewbox=\"0 0 12 12\">\n\t\t\t\t<path fill=\"#fff\" d=\"M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z\"><\/path>\n\t\t\t<\/svg>\n\t\t<\/button><\/figure><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><p class=\"important\"><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.<\/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\":\"69da5ebc244f8\"}' data-wp-interactive=\"core\/image\" 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-async--click=\"actions.showLightbox\" data-wp-on-async--load=\"callbacks.setButtonStyles\" data-wp-on-async-window--resize=\"callbacks.setButtonStyles\" src=\"https:\/\/www.hostinger.com\/tutorials\/wp-content\/uploads\/sites\/2\/2025\/05\/file-permission-configuration-menu-in-linux-gui.png\" 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-async--click=\"actions.showLightbox\" data-wp-style--right=\"state.imageButtonRight\" data-wp-style--top=\"state.imageButtonTop\">\n\t\t\t<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"12\" height=\"12\" fill=\"none\" viewbox=\"0 0 12 12\">\n\t\t\t\t<path fill=\"#fff\" d=\"M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z\"><\/path>\n\t\t\t<\/svg>\n\t\t<\/button><\/figure><\/div><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\":\"69da5ebc26919\"}' data-wp-interactive=\"core\/image\" 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-async--click=\"actions.showLightbox\" data-wp-on-async--load=\"callbacks.setButtonStyles\" data-wp-on-async-window--resize=\"callbacks.setButtonStyles\" src=\"https:\/\/www.hostinger.com\/tutorials\/wp-content\/uploads\/sites\/2\/2025\/05\/the-run-as-a-program-button-on-a-script.png\" 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-async--click=\"actions.showLightbox\" data-wp-style--right=\"state.imageButtonRight\" data-wp-style--top=\"state.imageButtonTop\">\n\t\t\t<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"12\" height=\"12\" fill=\"none\" viewbox=\"0 0 12 12\">\n\t\t\t\t<path fill=\"#fff\" d=\"M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z\"><\/path>\n\t\t\t<\/svg>\n\t\t<\/button><\/figure><\/div><p>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                    <h4 class=\"title\">Running a non-executable .sh file<\/h4>\n                    <p>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<\/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\":\"69da5ebc28cf8\"}' data-wp-interactive=\"core\/image\" 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-async--click=\"actions.showLightbox\" data-wp-on-async--load=\"callbacks.setButtonStyles\" data-wp-on-async-window--resize=\"callbacks.setButtonStyles\" src=\"https:\/\/www.hostinger.com\/tutorials\/wp-content\/uploads\/sites\/2\/2025\/05\/shell-script-process-id-on-temrinal.png\" 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-async--click=\"actions.showLightbox\" data-wp-style--right=\"state.imageButtonRight\" data-wp-style--top=\"state.imageButtonTop\">\n\t\t\t<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"12\" height=\"12\" fill=\"none\" viewbox=\"0 0 12 12\">\n\t\t\t\t<path fill=\"#fff\" d=\"M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z\"><\/path>\n\t\t\t<\/svg>\n\t\t<\/button><\/figure><\/div><p>If you 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":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"rank_math_title":"","rank_math_description":"","rank_math_focus_keyword":"","footnotes":""},"categories":[22644,22640],"tags":[],"class_list":["post-128040","post","type-post","status-publish","format-standard","hentry","category-managing-monitoring-and-security","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-UK","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":0,"href":"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-json\/wp\/v2\/posts\/128040\/revisions"}],"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}]}}