{"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-02-06T07:56:08","modified_gmt":"2026-02-06T06:56:08","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":"<br \/>\n.elementor-heading-title{padding:0;margin:0;line-height:1}.elementor-widget-heading .elementor-heading-title[class*=elementor-size-]&gt;a{color:inherit;font-size:inherit;line-height:inherit}.elementor-widget-heading .elementor-heading-title.elementor-size-small{font-size:15px}.elementor-widget-heading .elementor-heading-title.elementor-size-medium{font-size:19px}.elementor-widget-heading .elementor-heading-title.elementor-size-large{font-size:29px}.elementor-widget-heading .elementor-heading-title.elementor-size-xl{font-size:39px}.elementor-widget-heading .elementor-heading-title.elementor-size-xxl{font-size:59px}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.\n\nIt 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 <b>Brian Fox<\/b> in 1989 for the <b>GNU<\/b> project, it was developed to replace the original <b>Bourne Shell<\/b>, bringing significant improvements in terms of functionality and compatibility.\n\n<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.\n<h3>The Basics of Bash Scripting<\/h3>\nBash scripting is an essential skill for anyone working with Unix or Linux systems.\n<h4>Bash and Bang: Introduction to the Shebang<\/h4>\nThe first key element of any Bash script is the <b>shebang<\/b> line. It tells the system which interpreter to use to execute the script.\n<pre data-line=\"\">\t\t\t\t<code>\n\t\t\t\t\t#!\/bin\/bash\n\t\t\t\t<\/code>\n<\/pre>\nThe <strong>#!<\/strong> is known as a shebang, and <b>\/bin\/bash<\/b> 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.\n<h4>First Step: Hello World!<\/h4>\nWith your favorite text editor, create a file called <b>hello.sh<\/b>, and add the following content, then save it:\n<pre data-line=\"\">\t\t\t\t<code>\n\t\t\t\t\t#!\/bin\/bash\necho \"Hello World!\"\n\t\t\t\t<\/code>\n<\/pre>\n<h4>Making a Script Executable<\/h4>\nBy 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:\n<pre data-line=\"\">\t\t\t\t<code>\n\t\t\t\t\tchmod +x hello.sh\n\t\t\t\t<\/code>\n<\/pre>\nYou can then check the permissions with the following command:\n<pre data-line=\"\">\t\t\t\t<code>\n\t\t\t\t\tls -l hello.sh\n\t\t\t\t<\/code>\n<\/pre>\n\n<div class=\"wp-block-buttons is-layout-flex wp-block-buttons-is-layout-flex is-content-justification-center\"><div class=\"wp-block-button \"><a class=\"wp-block-button__link wp-element-button \" href=\"\/en\/courses\/data-ai\/\">Understanding Bash Scripting<\/a><\/div><\/div>\n\n<h4>Running a Script<\/h4>\nTo run the script, we have several options:\n<ul>\n \t<li style=\"font-weight: 400\">Relative Path:<\/li>\n<\/ul>\n<pre data-line=\"\">\t\t\t\t<code>\n\t\t\t\t\t.\/hello.sh\n\t\t\t\t<\/code>\n<\/pre>\n<ul>\n \t<li>Using the Bash shell:<\/li>\n<\/ul>\n<pre data-line=\"\">\t\t\t\t<code>\n\t\t\t\t\tbash hello.sh\n\t\t\t\t<\/code>\n<\/pre>\n<ul>\n \t<li>Via the sh shell, but this may cause different behaviors if the script uses specific Bash features:<\/li>\n<\/ul>\n<pre data-line=\"\">\t\t\t\t<code>\n\t\t\t\t\tsh hello.sh\n\t\t\t\t<\/code>\n<\/pre>\n<h3>Basic Commands<\/h3>\nBash 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:\n<ul>\n \t<li style=\"font-weight: 400\">List files and folders in the current directory<\/li>\n<\/ul>\n<pre data-line=\"\">\t\t\t\t<code>\n\t\t\t\t\tls\n\t\t\t\t<\/code>\n<\/pre>\n<ul>\n \t<li>Change directory<\/li>\n<\/ul>\n<pre data-line=\"\">\t\t\t\t<code>\n\t\t\t\t\tcd \/path\/to\/directory\n\t\t\t\t<\/code>\n<\/pre>\n<ul>\n \t<li>Display the current directory<\/li>\n<\/ul>\n<pre data-line=\"\">\t\t\t\t<code>\n\t\t\t\t\tpwd\n\t\t\t\t<\/code>\n<\/pre>\n<ul>\n \t<li>Create an empty file or update the timestamp of an existing file<\/li>\n<\/ul>\n<pre data-line=\"\">\t\t\t\t<code>\n\t\t\t\t\ttouch newfile.txt\n\t\t\t\t<\/code>\n<\/pre>\n<ul>\n \t<li>Delete files or folders<\/li>\n<\/ul>\n<pre data-line=\"\">\t\t\t\t<code>\n\t\t\t\t\trm file.txt\n\t\t\t\t<\/code>\n<\/pre>\n\n<div class=\"wp-block-buttons is-layout-flex wp-block-buttons-is-layout-flex is-content-justification-center\"><div class=\"wp-block-button \"><a class=\"wp-block-button__link wp-element-button \" href=\"\/en\/courses\/data-ai\/\">Training in Bash Scripting<\/a><\/div><\/div>\n\n<h3>Combining Commands<\/h3>\nBash scripts become powerful when you combine commands to perform more complex tasks. Here are some examples:\n<ul>\n \t<li>Use the <strong>&#8220;<\/strong><b>;&#8221;<\/b> to execute multiple commands in a sequence<\/li>\n<\/ul>\n<pre data-line=\"\">\t\t\t\t<code>\n\t\t\t\t\tcd \/path\/to\/directory; ls; pwd\n\t\t\t\t<\/code>\n<\/pre>\n<ul>\n \t<li>Redirect output: Use <b>&gt;<\/b> to redirect the output of a command to a file, or <b>&gt;&gt;<\/b> to append to an existing file<\/li>\n<\/ul>\n<pre data-line=\"\">\t\t\t\t<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>\n<\/pre>\n<ul>\n \t<li>Use the Pipe ( <strong>|<\/strong> ) to send the output of one command as input to another command<\/li>\n<\/ul>\n<pre data-line=\"\">\t\t\t\t<code>\n\t\t\t\t\tls -l | grep \".txt\"\n\t\t\t\t<\/code>\n<\/pre>\n<h3>Variables and Substitution<\/h3>\n<ul>\n \t<li style=\"font-weight: 400\">Declare a variable and use it with <b>$<\/b><\/li>\n<\/ul>\n<pre data-line=\"\">\t\t\t\t<code>\n\t\t\t\t\tNAME=\u201dAlice\u201d\necho \u201cHello, $NAME!\u201d\n\t\t\t\t<\/code>\n<\/pre>\n<ul>\n \t<li>Use substitution to take the output of a command and use it as a variable<\/li>\n<\/ul>\n<pre data-line=\"\">\t\t\t\t<code>\n\t\t\t\t\tDATE=$(date)\necho \"Today's date is $DATE\"\n\t\t\t\t<\/code>\n<\/pre>\n<em>Note that this example is often used in log creation<\/em>\n<h3>Managing Paths<\/h3>\nUnderstanding and managing file paths is essential for efficiently navigating the filesystem and writing robust scripts.\n<h4>Absolute Path vs Relative Path<\/h4>\n\n.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 \/>\n\n<table style=\"undefined;width: 400px\">\n<colgroup>\n<col style=\"width: 175px\">\n<col style=\"width: 175px\">\n<col style=\"width: 175px\">\n<col style=\"width: 175px\">\n<\/colgroup>\n<thead>\n<tr>\n<th><img decoding=\"async\" src=\"https:\/\/liora.io\/app\/uploads\/2024\/07\/image1-2.png\" alt=\"Image\" width=\"50\" height=\"50\"><\/th>\n<th>Absolute<\/th>\n<th><img decoding=\"async\" src=\"https:\/\/liora.io\/app\/uploads\/2024\/07\/image2-3.png\" alt=\"Image\" width=\"50\" height=\"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>\n\n<div class=\"wp-block-buttons is-layout-flex wp-block-buttons-is-layout-flex is-content-justification-center\"><div class=\"wp-block-button \"><a class=\"wp-block-button__link wp-element-button \" href=\"\/en\/courses\/data-ai\/\">Become an expert in  Bash Scripting<\/a><\/div><\/div>\n\n<h4>Associated Commands<\/h4>\nTo locate files and commands, several tools are available in Bash:\n<ul>\n \t<li style=\"font-weight: 400\">Find the location of an executable<\/li>\n<\/ul>\n<pre data-line=\"\">\t\t\t\t<code>\n\t\t\t\t\twhich bash\n\t\t\t\t<\/code>\n<\/pre>\n<ul>\n \t<li>Location of binaries, sources, and documentation associated with a command<\/li>\n<\/ul>\n<pre data-line=\"\">\t\t\t\t<code>\n\t\t\t\t\twhereis bash\n\t\t\t\t<\/code>\n<\/pre>\n<ul>\n \t<li>Search for files and folders<\/li>\n<\/ul>\n<pre data-line=\"\">\t\t\t\t<code>\n\t\t\t\t\tfind \/home\/user -name \"report.txt\"\n\t\t\t\t<\/code>\n<\/pre>\n<h4>Environment Variables<\/h4>\nEnvironment variables are key-value pairs that affect the behavior of system processes. Here are the most common ones:\n<ul>\n \t<li><b>$PATH<\/b>: Contains a list of directories where the system looks for executables. To modify it, use the following command<\/li>\n<\/ul>\n<pre data-line=\"\">\t\t\t\t<code>\n\t\t\t\t\texport PATH=$PATH:\/new\/directory\/path\n\t\t\t\t<\/code>\n<\/pre>\n<ul>\n \t<li><b>$HOME<\/b>: Represents the user&#8217;s home directory<\/li>\n<\/ul>\n<pre data-line=\"\">\t\t\t\t<code>\n\t\t\t\t\techo $HOME\ncd $HOME\n\t\t\t\t<\/code>\n<\/pre>\n<ul>\n \t<li><b>$PWD<\/b>: Indicates the current working directory<\/li>\n<\/ul>\n<pre data-line=\"\">\t\t\t\t<code>\n\t\t\t\t\techo $PWD\n\t\t\t\t<\/code>\n<\/pre>\n<ul>\n \t<li><b>$USER<\/b>: Contains the name of the current user<\/li>\n<\/ul>\n<pre data-line=\"\">\t\t\t\t<code>\n\t\t\t\t\techo $USER\n\t\t\t\t<\/code>\n<\/pre>\n<h3>Flow Control and Logic<\/h3>\nFlow control and logic are essential elements of Bash scripting, allowing for the creation of dynamic and adaptable scripts.\n\n<a href=\"\/en\/courses\/data-ai\/\">\nAll about Bash Scripting\n<\/a>\n<h4>Conditional Statements<\/h4>\nThey allow for code execution based on certain conditions. Here is an example illustrating the use of <b>if\u2026elif\u2026else<\/b>.\n<pre data-line=\"\">\t\t\t\t<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>\n<\/pre>\n<h4>Loops<\/h4>\nLoops are used to repeat commands multiple times.\n<ul>\n \t<li style=\"font-weight: 400\"><b>for<\/b><\/li>\n<\/ul>\n<pre data-line=\"\">\t\t\t\t<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>\n<\/pre>\n<ul>\n \t<li><b>while<\/b><\/li>\n<\/ul>\n<pre data-line=\"\">\t\t\t\t<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>\n<\/pre>\n<ul>\n \t<li><b>until<\/b><\/li>\n<\/ul>\n<pre data-line=\"\">\t\t\t\t<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>\n<\/pre>\n\n<div class=\"wp-block-buttons is-layout-flex wp-block-buttons-is-layout-flex is-content-justification-center\"><div class=\"wp-block-button \"><a class=\"wp-block-button__link wp-element-button \" href=\"\/en\/courses\/data-ai\/\">Find a course for you<\/a><\/div><\/div>\n\n<h3>Some Useful Scripts and Best Practices<\/h3>\n<h4>Sample Scripts<\/h4>\n<ul>\n \t<li><b>Backup Script<\/b>: Copies files from a source to a destination<\/li>\n<\/ul>\n<pre data-line=\"\">\t\t\t\t<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>\n<\/pre>\n<ul>\n \t<li><b>Cleanup<\/b>: Deletes items from a temporary folder<\/li>\n<\/ul>\n<pre data-line=\"\">\t\t\t\t<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>\n<\/pre>\n\n<div class=\"wp-block-buttons is-layout-flex wp-block-buttons-is-layout-flex is-content-justification-center\"><div class=\"wp-block-button \"><a class=\"wp-block-button__link wp-element-button \" href=\"\/en\/courses\/data-ai\/\">Mastering Bash Scripting<\/a><\/div><\/div>\n\n<h3>Some Best Practices<\/h3>\n<p style=\"padding-left: 40px\"><strong><b>1. Comment<\/b>: This makes scripts more readable and understandable<\/strong><\/p>\n<p style=\"padding-left: 40px\"><b>2. Name<\/b> your variables descriptively. Avoid variable names like $var1, $var2, or $tmpvar.<\/p>\n<p style=\"padding-left: 40px\"><b>3. Handle errors<\/b>: Use conditions to check for success and capture errors appropriately<\/p>\n<p style=\"padding-left: 40px\"><b>4. Modularize your code<\/b> using functions, especially if the script performs multiple operations, for example:<\/p>\n\n<pre data-line=\"\">\t\t\t\t<code>\n\t\t\t\t\tbackup_files() {\n    # Code to perform the backup\n}\n\t\t\t\t<\/code>\n<\/pre>\n<p style=\"padding-left: 40px\"><b>5. Debug to identify errors<\/b>. Here are useful commands for this:<\/p>\n\n<pre data-line=\"\">\t\t\t\t<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>\n<\/pre>\n<h3>To Conclude<\/h3>\nBash scripting is a powerful tool <b>to automate and simplify tasks on Unix and Linux systems<\/b>. By mastering the basics, basic commands, path management, flow control, and best practices, you can <b>write robust and effective scripts<\/b> to improve your daily productivity.\n\n<a href=\"\/en\/courses\/data-ai\/\">\nStart a Bash Scripting training course\n<\/a>","protected":false},"excerpt":{"rendered":"<p>.elementor-heading-title{padding:0;margin:0;line-height:1}.elementor-widget-heading .elementor-heading-title[class*=elementor-size-]&gt;a{color:inherit;font-size:inherit;line-height:inherit}.elementor-widget-heading .elementor-heading-title.elementor-size-small{font-size:15px}.elementor-widget-heading .elementor-heading-title.elementor-size-medium{font-size:19px}.elementor-widget-heading .elementor-heading-title.elementor-size-large{font-size:29px}.elementor-widget-heading .elementor-heading-title.elementor-size-xl{font-size:39px}.elementor-widget-heading .elementor-heading-title.elementor-size-xxl{font-size:59px}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 [&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":1,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/posts\/187780\/revisions"}],"predecessor-version":[{"id":205677,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/posts\/187780\/revisions\/205677"}],"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}]}}