{"id":187780,"date":"2024-08-14T06:30:00","date_gmt":"2024-08-14T05:30:00","guid":{"rendered":"https:\/\/liora.io\/en\/?p=187780"},"modified":"2026-08-07T10:19:51","modified_gmt":"2026-08-07T09:19:51","slug":"all-about-bash-scripting","status":"publish","type":"post","link":"https:\/\/liora.io\/en\/all-about-bash-scripting","title":{"rendered":"What is Bash scripting?"},"content":{"rendered":"\n<p><strong>Scripting offers users the ability to simplify and streamline their daily operations. Bash scripts (short for Bourne Again Shell) are very powerful and useful components for development.<\/strong><\/p>\n\n\n<p>It is a command-line interpreter for Unix and <a href=\"https:\/\/liora.io\/en\/linux-the-preferred-os-for-developers\">Linux<\/a> systems. Designed by <strong>Brian Fox<\/strong> in 1989 for the <strong>GNU<\/strong> project, it was developed to replace the original <strong>Bourne Shell<\/strong>, bringing significant improvements in terms of functionality and compatibility.<\/p>\n\n\n<p><a href=\"https:\/\/liora.io\/en\/bash-bourne-again-shell-principle-benefits-training\">The importance of Bash in system administration and software development<\/a> cannot be underestimated. It allows for the automation of repetitive tasks, management of large-scale systems, and the facilitation of complex script development for various applications.<\/p>\n\n\n<h2 class=\"wp-block-heading\" id=\"the-basics-of-bash-scripting\">The Basics of Bash Scripting<\/h2>\n\n\n<p>Bash scripting is an essential skill for anyone working with Unix or Linux systems.<\/p>\n\n\n<h3 class=\"wp-block-heading\" id=\"bash-and-bang-introduction-to-the-shebang\">Bash and Bang: Introduction to the Shebang<\/h3>\n\n\n<p>The first key element of any Bash script is the <strong>shebang<\/strong> line. It tells the system which interpreter to use to execute the script.<\/p>\n\n\n<pre class=\"wp-block-code\"><code>\n\t\t\t\t\t#!\/bin\/bash\n\t\t\t\t<\/code><\/pre>\n\n\n<p>The <strong>#!<\/strong> is known as a shebang, and <strong>\/bin\/bash<\/strong> specifies the path to the Bash interpreter. This line is crucial because it ensures that the script will be interpreted by Bash, even if other shells are present on the system.<\/p>\n\n\n<h3 class=\"wp-block-heading\" id=\"first-step-hello-world\">First Step: Hello World!<\/h3>\n\n\n<p>With your favorite text editor, create a file called <strong>hello.sh<\/strong>, and add the following content, then save it:<\/p>\n\n\n<pre class=\"wp-block-code\"><code>\n\t\t\t\t\t#!\/bin\/bash\necho \"Hello World!\"\n\t\t\t\t<\/code><\/pre>\n\n\n<h3 class=\"wp-block-heading\" id=\"making-a-script-executable\">Making a Script Executable<\/h3>\n\n\n<p>By default, a text file does not have the necessary permissions to be executed like a program. To make your script executable, you need to use the following command to change the permissions:<\/p>\n\n\n<pre class=\"wp-block-code\"><code>\n\t\t\t\t\tchmod +x hello.sh\n\t\t\t\t<\/code><\/pre>\n\n\n<p>You can then check the permissions with the following command:<\/p>\n\n\n<pre class=\"wp-block-code\"><code>\n\t\t\t\t\tls -l hello.sh\n\t\t\t\t<\/code><\/pre>\n\n\n<div class=\"wp-block-buttons is-layout-flex wp-block-buttons-is-layout-flex is-content-justification-center wp-container-core-buttons-is-layout-5ee10de4\" style=\"margin-top:32px;margin-bottom:32px\"><div class=\"wp-block-button\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/liora.io\/en\/courses\/data-ai\/data-analyst\">Understanding Bash Scripting<\/a><\/div><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"running-a-script\">Running a Script<\/h3>\n\n\n<p>To run the script, we have several options:<\/p>\n\n\n<ul class=\"wp-block-list\">\n<li>Relative Path:<\/li>\n<\/ul>\n\n\n<pre class=\"wp-block-code\"><code>\n\t\t\t\t\t.\/hello.sh\n\t\t\t\t<\/code><\/pre>\n\n\n<ul class=\"wp-block-list\">\n<li>Using the Bash shell:<\/li>\n<\/ul>\n\n\n<pre class=\"wp-block-code\"><code>\n\t\t\t\t\tbash hello.sh\n\t\t\t\t<\/code><\/pre>\n\n\n<ul class=\"wp-block-list\">\n<li>Via the sh shell, but this may cause different behaviors if the script uses specific Bash features:<\/li>\n<\/ul>\n\n\n<pre class=\"wp-block-code\"><code>\n\t\t\t\t\tsh hello.sh\n\t\t\t\t<\/code><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id=\"basic-commands\">Basic Commands<\/h2>\n\n\n<p>Bash scripting relies on the effective use of a variety of commands to perform simple and complex tasks. Here are some commonly used commands for navigating and manipulating the filesystem:<\/p>\n\n\n<ul class=\"wp-block-list\">\n<li>List files and folders in the current directory<\/li>\n<\/ul>\n\n\n<pre class=\"wp-block-code\"><code>\n\t\t\t\t\tls\n\t\t\t\t<\/code><\/pre>\n\n\n<ul class=\"wp-block-list\">\n<li>Change directory<\/li>\n<\/ul>\n\n\n<pre class=\"wp-block-code\"><code>\n\t\t\t\t\tcd \/path\/to\/directory\n\t\t\t\t<\/code><\/pre>\n\n\n<ul class=\"wp-block-list\">\n<li>Display the current directory<\/li>\n<\/ul>\n\n\n<pre class=\"wp-block-code\"><code>\n\t\t\t\t\tpwd\n\t\t\t\t<\/code><\/pre>\n\n\n<ul class=\"wp-block-list\">\n<li>Create an empty file or update the timestamp of an existing file<\/li>\n<\/ul>\n\n\n<pre class=\"wp-block-code\"><code>\n\t\t\t\t\ttouch newfile.txt\n\t\t\t\t<\/code><\/pre>\n\n\n<ul class=\"wp-block-list\">\n<li>Delete files or folders<\/li>\n<\/ul>\n\n\n<pre class=\"wp-block-code\"><code>\n\t\t\t\t\trm file.txt\n\t\t\t\t<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"combining-commands\">Combining Commands<\/h2>\n\n\n<p>Bash scripts become powerful when you combine commands to perform more complex tasks. Here are some examples:<\/p>\n\n\n<ul class=\"wp-block-list\">\n<li>Use the <strong>&#8220;<\/strong><strong>;&#8221;<\/strong> to execute multiple commands in a sequence<\/li>\n<\/ul>\n\n\n<pre class=\"wp-block-code\"><code>\n\t\t\t\t\tcd \/path\/to\/directory; ls; pwd\n\t\t\t\t<\/code><\/pre>\n\n\n<ul class=\"wp-block-list\">\n<li>Redirect output: Use <strong>&gt;<\/strong> to redirect the output of a command to a file, or <strong>&gt;&gt;<\/strong> to append to an existing file<\/li>\n<\/ul>\n\n\n<pre class=\"wp-block-code\"><code>\n\t\t\t\t\techo \"Hello, World!\" &gt; hello.txt\necho \"Hello again!\" &gt;&gt; hello.txt\n\t\t\t\t<\/code><\/pre>\n\n\n<ul class=\"wp-block-list\">\n<li>Use the Pipe ( <strong>|<\/strong> ) to send the output of one command as input to another command<\/li>\n<\/ul>\n\n\n<pre class=\"wp-block-code\"><code>\n\t\t\t\t\tls -l | grep \".txt\"\n\t\t\t\t<\/code><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id=\"variables-and-substitution\">Variables and Substitution<\/h2>\n\n\n<ul class=\"wp-block-list\">\n<li>Declare a variable and use it with <strong>$<\/strong><\/li>\n<\/ul>\n\n\n<pre class=\"wp-block-code\"><code>\n\t\t\t\t\tNAME=\u201dAlice\u201d\necho \u201cHello, $NAME!\u201d\n\t\t\t\t<\/code><\/pre>\n\n\n<ul class=\"wp-block-list\">\n<li>Use substitution to take the output of a command and use it as a variable<\/li>\n<\/ul>\n\n\n<pre class=\"wp-block-code\"><code>\n\t\t\t\t\tDATE=$(date)\necho \"Today's date is $DATE\"\n\t\t\t\t<\/code><\/pre>\n\n\n<p><em>Note that this example is often used in log creation<\/em><\/p>\n\n\n<h2 class=\"wp-block-heading\" id=\"managing-paths\">Managing Paths<\/h2>\n\n\n<p>Understanding and managing file paths is essential for efficiently navigating the filesystem and writing robust scripts.<\/p>\n\n\n<h3 class=\"wp-block-heading\" id=\"absolute-path-vs-relative-path\">Absolute Path vs Relative Path<\/h3>\n\n\n<p>.tg  {border-collapse:collapse;border-spacing:0;}<br\/>\n.tg td{border-color:black;border-style:solid;border-width:1px;font-family:Rubik, sans-serif;font-size:16px;<br\/>\n  overflow:hidden;padding:10px 5px;word-break:normal;}<br\/>\n.tg th{border-color:black;border-style:solid;border-width:1px;font-family:Rubik, sans-serif;font-size:16px;<br\/>\n  font-weight:normal;overflow:hidden;padding:10px 5px;word-break:normal;}<br\/>\n.tg .tg-fgfr{background-color:#9b9b9b;border-color:#9b9b9b;font-family:Rubik, Helvetica, sans-serif !important;font-size:16px;<br\/>\n  text-align:right;vertical-align:middle}<br\/>\n.tg .tg-n3w7{background-color:#efefef;border-color:inherit;font-family:Rubik, Helvetica, sans-serif !important;font-size:16px;<br\/>\n  text-align:center;vertical-align:middle}<br\/>\n.tg .tg-hauo{background-color:#9b9b9b;border-color:#9b9b9b;font-family:Rubik, Helvetica, sans-serif !important;font-size:22px;<br\/>\n  text-align:left;vertical-align:middle}<br\/><\/p>\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table>\n<colgroup>\n<col\/>\n<col\/>\n<col\/>\n<col\/>\n<\/colgroup>\n<thead>\n<tr>\n<th><img alt=\"Illustration for Absolute Path vs Relative Path\" decoding=\"async\" height=\"50\" loading=\"lazy\" src=\"https:\/\/liora.io\/app\/uploads\/2024\/07\/image1-2.png\" width=\"50\"\/><\/th>\n<th>Absolute<\/th>\n<th><img alt=\"Illustration for Absolute Path vs Relative Path\" decoding=\"async\" height=\"50\" loading=\"lazy\" src=\"https:\/\/liora.io\/app\/uploads\/2024\/07\/image2-3.png\" width=\"50\"\/><\/th>\n<th>Relative<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td colspan=\"2\">Specifies the complete location of a file or directory from the root of the filesystem.<\/td>\n<td colspan=\"2\">Indicates the location of a file or directory relative to the current directory.<\/td>\n<\/tr>\n<tr>\n<td colspan=\"2\">Example : \/home\/user\/documents\/report.txt<\/td>\n<td colspan=\"2\">Example : documents\/report.txt<\/td>\n<\/tr>\n<\/tbody>\n<\/table><\/figure>\n\n\n<div class=\"wp-block-buttons is-layout-flex wp-block-buttons-is-layout-flex is-content-justification-center wp-container-core-buttons-is-layout-5ee10de4\" style=\"margin-top:32px;margin-bottom:32px\"><div class=\"wp-block-button\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/liora.io\/en\/courses\/data-ai\/data-analyst\">Become an expert in  Bash Scripting<\/a><\/div><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"associated-commands\">Associated Commands<\/h3>\n\n\n<p>To locate files and commands, several tools are available in Bash:<\/p>\n\n\n<ul class=\"wp-block-list\">\n<li>Find the location of an executable<\/li>\n<\/ul>\n\n\n<pre class=\"wp-block-code\"><code>\n\t\t\t\t\twhich bash\n\t\t\t\t<\/code><\/pre>\n\n\n<ul class=\"wp-block-list\">\n<li>Location of binaries, sources, and documentation associated with a command<\/li>\n<\/ul>\n\n\n<pre class=\"wp-block-code\"><code>\n\t\t\t\t\twhereis bash\n\t\t\t\t<\/code><\/pre>\n\n\n<ul class=\"wp-block-list\">\n<li>Search for files and folders<\/li>\n<\/ul>\n\n\n<pre class=\"wp-block-code\"><code>\n\t\t\t\t\tfind \/home\/user -name \"report.txt\"\n\t\t\t\t<\/code><\/pre>\n\n\n<h3 class=\"wp-block-heading\" id=\"environment-variables\">Environment Variables<\/h3>\n\n\n<p>Environment variables are key-value pairs that affect the behavior of system processes. Here are the most common ones:<\/p>\n\n\n<ul class=\"wp-block-list\">\n<li><strong>$PATH<\/strong>: Contains a list of directories where the system looks for executables. To modify it, use the following command<\/li>\n<\/ul>\n\n\n<pre class=\"wp-block-code\"><code>\n\t\t\t\t\texport PATH=$PATH:\/new\/directory\/path\n\t\t\t\t<\/code><\/pre>\n\n\n<ul class=\"wp-block-list\">\n<li><strong>$HOME<\/strong>: Represents the user&#8217;s home directory<\/li>\n<\/ul>\n\n\n<pre class=\"wp-block-code\"><code>\n\t\t\t\t\techo $HOME\ncd $HOME\n\t\t\t\t<\/code><\/pre>\n\n\n<ul class=\"wp-block-list\">\n<li><strong>$PWD<\/strong>: Indicates the current working directory<\/li>\n<\/ul>\n\n\n<pre class=\"wp-block-code\"><code>\n\t\t\t\t\techo $PWD\n\t\t\t\t<\/code><\/pre>\n\n\n<ul class=\"wp-block-list\">\n<li><strong>$USER<\/strong>: Contains the name of the current user<\/li>\n<\/ul>\n\n\n<pre class=\"wp-block-code\"><code>\n\t\t\t\t\techo $USER\n\t\t\t\t<\/code><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id=\"flow-control-and-logic\">Flow Control and Logic<\/h2>\n\n\n<p>Flow control and logic are essential elements of Bash scripting, allowing for the creation of dynamic and adaptable scripts.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"conditional-statements\">Conditional Statements<\/h3>\n\n\n<p>They allow for code execution based on certain conditions. Here is an example illustrating the use of <strong>if\u2026elif\u2026else<\/strong>.<\/p>\n\n\n<pre class=\"wp-block-code\"><code>\n\t\t\t\t\t#!\/bin\/bash\necho \"Enter a number: \"\nread number\nif [ $number -gt 10 ]; then\n    echo \"The number is greater than 10.\"\nelif [ $number -eq 10 ]; then\n    echo \"The number is equal to 10.\"\nelse\n    echo \"The number is less than 10.\"\nfi\n\t\t\t\t<\/code><\/pre>\n\n\n<h3 class=\"wp-block-heading\" id=\"loops\">Loops<\/h3>\n\n\n<p>Loops are used to repeat commands multiple times.<\/p>\n\n\n<ul class=\"wp-block-list\">\n<li><strong>for<\/strong><\/li>\n<\/ul>\n\n\n<pre class=\"wp-block-code\"><code>\n\t\t\t\t\tfor i in 1 2 3 4 5; do\n    echo \"Counter : $i\"\ndone\n\t\t\t\t<\/code><\/pre>\n\n\n<ul class=\"wp-block-list\">\n<li><strong>while<\/strong><\/li>\n<\/ul>\n\n\n<pre class=\"wp-block-code\"><code>\n\t\t\t\t\tcount=1\nwhile [ $count -le 5 ]; do\n    echo \"Counter: $count\"\n    ((count++))\ndone\n\n\t\t\t\t<\/code><\/pre>\n\n\n<ul class=\"wp-block-list\">\n<li><strong>until<\/strong><\/li>\n<\/ul>\n\n\n<pre class=\"wp-block-code\"><code>\n\t\t\t\t\tcount=1\nuntil [ $count -gt 5 ]; do\n    echo \"Counter : $count\"\n    ((count++))\ndone\n\t\t\t\t<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"some-useful-scripts-and-best-practices\">Some Useful Scripts and Best Practices<\/h2>\n\n\n<h3 class=\"wp-block-heading\" id=\"sample-scripts\">Sample Scripts<\/h3>\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Backup Script<\/strong>: Copies files from a source to a destination<\/li>\n<\/ul>\n\n\n<pre class=\"wp-block-code\"><code>\n\t\t\t\t\t#!\/bin\/bash\n# This script performs a backup of files\nsrc=\"\/home\/user\/documents\"\ndest=\"\/backup\/documents\"\nif [ ! -d $dest ]; then\n    mkdir -p $dest\nfi\nfor file in $src\/*; do\n    if [ -f $file ]; then\n        cp $file $dest\n        echo \"Copi\u00e9 $file vers $dest\"\n    fi\ndone\n\t\t\t\t<\/code><\/pre>\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Cleanup<\/strong>: Deletes items from a temporary folder<\/li>\n<\/ul>\n\n\n<pre class=\"wp-block-code\"><code>\n\t\t\t\t\t#!\/bin\/bash\ndir=\"\/home\/user\/temp\"\necho \"Cleaning up the directory $dir\"\nfor file in $dir\/*; do\n    if [ -f $file ]; then\n        rm $file\n        echo \"Deleted $file\"\n    fi\ndone\n\t\t\t\t<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"some-best-practices\">Some Best Practices<\/h2>\n\n\n<p><strong><strong>1. Comment<\/strong>: This makes scripts more readable and understandable<\/strong><\/p>\n\n\n<p><strong>2. Name<\/strong> your variables descriptively. Avoid variable names like $var1, $var2, or $tmpvar.<\/p>\n\n\n<p><strong>3. Handle errors<\/strong>: Use conditions to check for success and capture errors appropriately<\/p>\n\n\n<p><strong>4. Modularize your code<\/strong> using functions, especially if the script performs multiple operations, for example:<\/p>\n\n\n<pre class=\"wp-block-code\"><code>\n\t\t\t\t\tbackup_files() {\n    # Code to perform the backup\n}\n\t\t\t\t<\/code><\/pre>\n\n\n<p><strong>5. Debug to identify errors<\/strong>. Here are useful commands for this:<\/p>\n\n\n<pre class=\"wp-block-code\"><code>\n\t\t\t\t\t#!\/bin\/bash\nset -x # Enables trace mode, displaying each command and its result\n# Script code\n#!\/bin\/bash\nset -e # Stops the script on errors\n# Script code\necho \"Starting the script\" # Use echo at strategic points in the script to check outputs and follow the execution flow\n\t\t\t\t<\/code><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id=\"to-conclude\">To Conclude<\/h2>\n\n\n<p>Bash scripting is a powerful tool <strong>to automate and simplify tasks on Unix and Linux systems<\/strong>. By mastering the basics, basic commands, path management, flow control, and best practices, you can <strong>write robust and effective scripts<\/strong> to improve your daily productivity.<\/p>\n\n\n<div class=\"wp-block-buttons is-layout-flex wp-block-buttons-is-layout-flex is-content-justification-center wp-container-core-buttons-is-layout-5ee10de4\" style=\"margin-top:32px;margin-bottom:32px\"><div class=\"wp-block-button\"><a class=\"wp-block-button__link wp-element-button\" href=\"\/en\/courses\/cloud-dev\/devops-engineer\">Start a Bash Scripting training course<\/a><\/div><\/div>\n\n","protected":false},"excerpt":{"rendered":"<p>Scripting offers users the ability to simplify and streamline their daily operations. Bash scripts (short for Bourne Again Shell) are very powerful and useful components for development. It is a command-line interpreter for Unix and Linux systems. Designed by Brian Fox in 1989 for the GNU project, it was developed to replace the original Bourne [&hellip;]<\/p>\n","protected":false},"author":74,"featured_media":187782,"comment_status":"open","ping_status":"open","sticky":false,"template":"elementor_theme","format":"standard","meta":{"_acf_changed":false,"editor_notices":[],"footnotes":""},"categories":[2434],"class_list":["post-187780","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cloud-dev"],"acf":[],"_links":{"self":[{"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/posts\/187780","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/users\/74"}],"replies":[{"embeddable":true,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/comments?post=187780"}],"version-history":[{"count":3,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/posts\/187780\/revisions"}],"predecessor-version":[{"id":209693,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/posts\/187780\/revisions\/209693"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/media\/187782"}],"wp:attachment":[{"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/media?parent=187780"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/categories?post=187780"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}