{"id":22210,"date":"2023-10-01T16:51:18","date_gmt":"2023-10-01T16:51:18","guid":{"rendered":"https:\/\/linuxways.net\/?p=22210"},"modified":"2023-10-01T16:51:18","modified_gmt":"2023-10-01T16:51:18","slug":"how-to-run-a-bash-script-in-ubuntu-22-04","status":"publish","type":"post","link":"https:\/\/linuxways.net\/de\/centos\/how-to-run-a-bash-script-in-ubuntu-22-04\/","title":{"rendered":"How to Run a Bash Script in Ubuntu 22.04?"},"content":{"rendered":"<p>A <strong>Bash (Bourne Again Shell) Script <\/strong>is a text file possessing a series of commands delivering instructions to a bash shell. Bash scripts are designed to run on the <strong>Command Line. <\/strong>Bash scripts are widely used to automate tasks, for example, update\/upgrade a system, backup work, create log files., etc.<\/p>\n<p>A bash script can be executed in different methods. This article will demonstrate different ways of running a bash script in Ubuntu 22.04 LTS. This article will discuss:<\/p>\n<ul>\n<li>How to Create a Bash Script in Ubuntu 22.04.<\/li>\n<li>How to Run\/Execute a Bash Script via Bash in Ubuntu 22.04?<\/li>\n<li>How to Run\/Execute a Bash Script via sh in Ubuntu 22.04?<\/li>\n<li>How to Run\/Execute Bash Script via Source in Ubuntu 22.04?<\/li>\n<li>How to Run\/Execute a Bash Script by Specifying the Absolute Path in Ubuntu 22.04?<\/li>\n<li>How to Run\/Execute Bash Script from Anywhere in Ubuntu 22.04?<\/li>\n<\/ul>\n<h2><a id=\"post-22210-_n7pixqwd5w5n\"><\/a>How to Create a Bash Script in Ubuntu 22.04?<\/h2>\n<p>To create a bash script, first launch the terminal by pressing a combination of keys [<strong>Ctrl + Alt + T<\/strong>]. Next, a user can use a text editor, for example, Nano text editor to type the contents of the bash script. The following command is executed to create a bash script using Nano text editor<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"co4\">$ <\/span><span class=\"kw2\">nano<\/span> hello.sh<\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"2048\" height=\"126\" class=\"wp-image-22243\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22210-1.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22210-1.png 2048w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22210-1-300x18.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22210-1-1024x63.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22210-1-768x47.png 768w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22210-1-1536x95.png 1536w\" sizes=\"auto, (max-width: 2048px) 100vw, 2048px\" \/><\/p>\n<p>After the file is launched via Nano text editor, the contents of the file are typed in the file:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"co0\">#!\/bin\/bash<\/span><br \/>\n<br \/>\n<span class=\"kw3\">echo<\/span> <span class=\"st0\">&quot;Hello World&quot;<\/span><br \/>\n<br \/>\n<span class=\"kw3\">echo<\/span> <span class=\"st0\">&quot;This is a Test File&quot;<\/span><\/div><\/div>\n<p>Where,<\/p>\n<ul>\n<li>#!\/bin\/bash: shell interpreter that will be used, i.e., bash. \u201c<strong>#!<\/strong>\u201d indicates the start of the script and is called shebang.<\/li>\n<li>echo &#8220;Hello World&#8221; and echo &#8220;This is a Test File&#8221;: <strong>echo <\/strong>is used to print on the screen.<\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"2048\" height=\"1122\" class=\"wp-image-22251\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22210-2.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22210-2.png 2048w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22210-2-300x164.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22210-2-1024x561.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22210-2-768x421.png 768w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22210-2-1536x842.png 1536w\" sizes=\"auto, (max-width: 2048px) 100vw, 2048px\" \/><br \/>\nThe file is then saved and closed by pressing Ctrl+O and Ctrl+X respectively. \u201c<strong>ls<\/strong>\u201d can be used to verify if the file is successfully created:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"2048\" height=\"233\" class=\"wp-image-22255\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22210-3.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22210-3.png 2048w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22210-3-300x34.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22210-3-1024x117.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22210-3-768x87.png 768w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22210-3-1536x175.png 1536w\" sizes=\"auto, (max-width: 2048px) 100vw, 2048px\" \/><\/p>\n<p>The above image shows that the script \u201ctestfile.sh\u201d is successfully created. In the following sections, different ways of running a bash script will be discussed.<\/p>\n<h2><a id=\"post-22210-_ylcliizbzrok\"><\/a>How to Run\/Execute a Bash Script via Bash in Ubuntu 22.04?<\/h2>\n<p><strong>Bash (Bourne Again Shell) <\/strong>is a shell interpreter. It is a replacement for <strong>sh (Bourne Shell) <\/strong>and includes features from different shells in Linux, for example, C shell and Kom shell<strong>. <\/strong>Bash allows you to recall earlier used commands by arrow keys. The bash interpreter resides in the <strong>\/bin\/bash<\/strong>. A bash script can be executed using<strong> bash<\/strong> by<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"co4\">$ <\/span><span class=\"kw2\">bash<\/span> testfile.sh<\/div><\/div>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"2048\" height=\"269\" class=\"wp-image-22262\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22210-4.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22210-4.png 2048w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22210-4-300x39.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22210-4-1024x135.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22210-4-768x101.png 768w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22210-4-1536x202.png 1536w\" sizes=\"auto, (max-width: 2048px) 100vw, 2048px\" \/><\/strong><\/p>\n<p>From the above image, it can be observed that the bash script is successfully executed as it displays the script contents.<\/p>\n<h2><a id=\"post-22210-_xr3sxi5khbu\"><\/a>How to Run\/Execute a Bash Script via sh in Ubuntu 22.04?<\/h2>\n<p><strong>\u201csh (Bourne Shell)\u201d <\/strong>is the first UNIX shell. The sh shell resides in <strong>\/bin\/sh <\/strong>and <strong>\/sbin\/sh. <\/strong>A bash script can be executed using<strong> sh<\/strong> by<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"co4\">$ <\/span><span class=\"kw2\">sh<\/span> testfile.sh<\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"2048\" height=\"247\" class=\"wp-image-22264\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22210-5.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22210-5.png 2048w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22210-5-300x36.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22210-5-1024x124.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22210-5-768x93.png 768w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22210-5-1536x185.png 1536w\" sizes=\"auto, (max-width: 2048px) 100vw, 2048px\" \/><\/p>\n<p>From the above image, it can be observed that the bash script is successfully executed as it displays the script content.<\/p>\n<h1><a id=\"post-22210-_96vy8gj30xdw\"><\/a>How to Run\/Execute Bash Script via Source in Ubuntu 22.04?<\/h1>\n<p><strong>\u201cSource\u201d <\/strong>is a built-in shell command in Linux that executes the specified file content in the current shell. A bash script can either be executed by <strong>source <\/strong>command by<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"co4\">$ <\/span><span class=\"kw3\">source<\/span> testfile.sh<\/div><\/div>\n<p>Or<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"co4\">$ <\/span>. testfile.sh<\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"2048\" height=\"485\" class=\"wp-image-22274\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22210-6.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22210-6.png 2048w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22210-6-300x71.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22210-6-1024x243.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22210-6-768x182.png 768w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22210-6-1536x364.png 1536w\" sizes=\"auto, (max-width: 2048px) 100vw, 2048px\" \/><\/p>\n<p>From the above image, it can be observed that the bash script is successfully executed as it displays the script content.<\/p>\n<h2><a id=\"post-22210-_nw8l2uusnwz0\"><\/a>How to Run\/Execute Bash Script by Specifying the AbsolutePath in Ubuntu 22.04?<\/h2>\n<p>In order to run a bash script by specifying the <strong>absolute path<\/strong>, first, the script\u2019s permission is changed to be executable by utilizing the <strong>chmod <\/strong>command:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"co4\">$ <\/span><span class=\"kw2\">chmod<\/span> +x testfile.sh<\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"2048\" height=\"624\" class=\"wp-image-22281\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22210-7.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22210-7.png 2048w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22210-7-300x91.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22210-7-1024x312.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22210-7-768x234.png 768w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22210-7-1536x468.png 1536w\" sizes=\"auto, (max-width: 2048px) 100vw, 2048px\" \/><\/p>\n<p>As seen from the above image, initially the script file permission was <strong>-rw-rw-r\u2013<\/strong>, i.e., the file was not executable for the user(u), groups(g), and others(o) groups. After execution of <strong>chmod <\/strong>command, the script permissions are updated to <strong>-rwxrwxr-x<\/strong>, i.e., it is not executable for all three groups.<\/p>\n<p>More information about permissions and ownership in Ubuntu can be checked from the articles: <a href=\"https:\/\/linuxways.net\/de\/how-to-change-file-permissions-and-ownership-in-linux-ubuntu\/\">How to Change File Permissions and Ownership in Linux\/Ubuntu?<\/a> and <a href=\"https:\/\/linuxways.net\/de\/how-does-chmod-777-work-in-linux-ubuntu\/\">How does Chmod 777 Work in Linux\/Ubuntu?<\/a> The executable script can be executed by:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"co4\">$ <\/span><span class=\"sy0\">\/<\/span>home<span class=\"sy0\">\/<\/span>linuxuser<span class=\"sy0\">\/<\/span>Downloads<span class=\"sy0\">\/<\/span>testfile.sh<\/div><\/div>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"2048\" height=\"386\" class=\"wp-image-22286\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22210-8.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22210-8.png 2048w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22210-8-300x57.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22210-8-1024x193.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22210-8-768x145.png 768w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22210-8-1536x290.png 1536w\" sizes=\"auto, (max-width: 2048px) 100vw, 2048px\" \/><\/strong><\/p>\n<p>From the above image, it can be observed that the bash script is successfully executed as it displays the script content. Additionally, the shortcut for home is represented by a tilde(<strong>~<\/strong>) character<strong>, <\/strong>therefore, the above command can also be typed as<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"co4\">$ <\/span>~<span class=\"sy0\">\/<\/span>Downloads<span class=\"sy0\">\/<\/span>testfile.sh<\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"2048\" height=\"231\" class=\"wp-image-22289\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22210-9.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22210-9.png 2048w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22210-9-300x34.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22210-9-1024x116.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22210-9-768x87.png 768w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22210-9-1536x173.png 1536w\" sizes=\"auto, (max-width: 2048px) 100vw, 2048px\" \/><\/p>\n<h2><a id=\"post-22210-_vzj6w1171b1u\"><\/a>How to Run\/Execute Bash Script from Anywhere in Ubuntu 22.04?<\/h2>\n<p>To execute a bash script from anywhere, the directory that contains the script is added to a <strong>$PATH<\/strong> by executing the following command<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"co4\">$ <\/span><span class=\"kw3\">export<\/span> <span class=\"re2\">PATH<\/span>=<span class=\"st0\">&quot;\/home\/linuxuser\/Downloads:<span class=\"es2\">$PATH<\/span>&quot;<\/span><\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"2048\" height=\"113\" class=\"wp-image-22298\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22210-10.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22210-10.png 2048w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22210-10-300x17.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22210-10-1024x57.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22210-10-768x42.png 768w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22210-10-1536x85.png 1536w\" sizes=\"auto, (max-width: 2048px) 100vw, 2048px\" \/><\/p>\n<p>After the directory is added to the $PATH, the script can be executed without specifying the path as follows:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"co4\">$ <\/span>testfile.sh<\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"2048\" height=\"643\" class=\"wp-image-22308\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22210-11.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22210-11.png 2048w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22210-11-300x94.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22210-11-1024x322.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22210-11-768x241.png 768w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22210-11-1536x482.png 1536w\" sizes=\"auto, (max-width: 2048px) 100vw, 2048px\" \/><\/p>\n<p>From the above image, it can be observed that the bash script is successfully executed as it displays the script content.<\/p>\n<p>These $PATH changes are temporary and will no longer exist after the terminal is restarted. In order to add a directory to a $PATH permanently, either of the three files: <strong>.bashrc file<\/strong>, <strong>.profile file, and <\/strong> <strong>\/etc\/environment file<\/strong> can be modified. The details of permanently adding a directory to a $PATH can be seen in the article: <a href=\"https:\/\/linuxways.net\/de\/pathlink\/\">How to Add a Directory to a $PATH in Linux\/Ubuntu?<\/a><\/p>\n<h2><a id=\"post-22210-_k4r3ojha6hv3\"><\/a>Conclusion<\/h2>\n<p>Bash Scripts are executed in Ubuntu 22.04 by <strong>bash<\/strong>, <strong>sh<\/strong>, <strong>source<\/strong> by <strong>bash &lt;script&gt;<\/strong>, <strong>sh &lt;script&gt;,<\/strong> and, <strong>source &lt;script&gt; <\/strong>commands respectively. Additionally, a script can be executed from anywhere either by specifying the <strong>absolute path <\/strong>or by adding the directory that contains the script to a <strong>$PATH.<\/strong> This article demonstrated different ways of running a bash script in Ubuntu 22.04 LTS.<\/p>","protected":false},"excerpt":{"rendered":"<p>Bash Scripts are executed in Ubuntu 22.04 by bash, sh, source by bash <script>, sh <script>, and, source <script> commands respectively.<\/p>","protected":false},"author":110,"featured_media":22368,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-22210","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-centos"],"_links":{"self":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/22210","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/users\/110"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/comments?post=22210"}],"version-history":[{"count":0,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/22210\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media\/22368"}],"wp:attachment":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media?parent=22210"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/categories?post=22210"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/tags?post=22210"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}