{"id":3287,"date":"2024-10-09T15:19:29","date_gmt":"2024-10-09T08:19:29","guid":{"rendered":"https:\/\/audithink.com\/?p=3287"},"modified":"2024-10-14T14:55:10","modified_gmt":"2024-10-14T07:55:10","slug":"bash-script","status":"publish","type":"post","link":"https:\/\/audithink.com\/en\/blog\/bash-script\/","title":{"rendered":"What is Bash Script? Meaning, Basic Commands &amp; 16 Examples"},"content":{"rendered":"<p>Linux and macOS users may already know the word bash. You could also say bash provides <em>syntax<\/em> and a wide variety of built-in commands to manage files, processes, and also other tasks in the system.<\/p>\n\n\n\n<p>Then for those who are not Linux or macOS users, what is bash? In order not to be too curious, let's Understand Together about what bash is.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The Bash Script<\/h2>\n\n\n\n<p>Bash <strong><a href=\"https:\/\/audithink.com\/en\/article\/script\/\" data-type=\"post\" data-id=\"3212\">script<\/a><\/strong> is a set of commands organized in a text file and executed using Bash (Bourne Again SHell), a command-line interpreter in Linux or Unix operating systems.\u00a0<\/p>\n\n\n\n<p>Used for automation of tasks that are often performed in the system, such as file Management, Process Management, networking, and so forth.<\/p>\n\n\n\n<p>A Shell is a program that provides a command-line interface for interacting with the operating system. Bash script allows the automation of various tasks that are usually performed manually through this interface, such as Directory navigation, file creation, or program execution.<\/p>\n\n\n\n<p>Every bash script starts with a shebang (#!\/bin \/ bash), which indicates the location of the Bash interpreter and tells the system to use Bash in executing that script. Inside the script file, a programmer can write down the same commands as those entered in the terminal.<\/p>\n\n\n\n<p>Variables in bash script are used to store data in the form of text or Numbers, without the need for a specific data type declaration. In addition, it also supports a function that allows grouping a number of commands into a single block that can be recalled as needed.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Benefits Of Bash Script<\/h2>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><img fetchpriority=\"high\" decoding=\"async\" width=\"2352\" height=\"1568\" src=\"https:\/\/audithink.com\/wp-content\/uploads\/2024\/10\/ezgif-1-a19e61f2f7_11zon.webp\" alt=\"picture of the person doing the task\" class=\"wp-image-3290\" style=\"width:602px;height:auto\" title=\"\" srcset=\"https:\/\/audithink.com\/wp-content\/uploads\/2024\/10\/ezgif-1-a19e61f2f7_11zon.webp 2352w, https:\/\/audithink.com\/wp-content\/uploads\/2024\/10\/ezgif-1-a19e61f2f7_11zon-300x200.webp 300w, https:\/\/audithink.com\/wp-content\/uploads\/2024\/10\/ezgif-1-a19e61f2f7_11zon-1024x683.webp 1024w, https:\/\/audithink.com\/wp-content\/uploads\/2024\/10\/ezgif-1-a19e61f2f7_11zon-768x512.webp 768w, https:\/\/audithink.com\/wp-content\/uploads\/2024\/10\/ezgif-1-a19e61f2f7_11zon-1536x1024.webp 1536w, https:\/\/audithink.com\/wp-content\/uploads\/2024\/10\/ezgif-1-a19e61f2f7_11zon-2048x1365.webp 2048w, https:\/\/audithink.com\/wp-content\/uploads\/2024\/10\/ezgif-1-a19e61f2f7_11zon-18x12.webp 18w\" sizes=\"(max-width: 2352px) 100vw, 2352px\" \/><figcaption class=\"wp-element-caption\">Bash Script (sumber: Pexels.com)<\/figcaption><\/figure>\n<\/div>\n\n\n<p>With the bash script certainly also provides many benefits for its users. Then what are the benefits? Here are some of the benefits of bash script are among others:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. Task Automation<\/h3>\n\n\n\n<p>Allows automation of repetitive tasks. This is especially beneficial in System Administration, where routine tasks such as data backup, system updating, or file management can be performed automatically, thus saving time and reducing the possibility of human error.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. Portability<\/h3>\n\n\n\n<p>Scripts written in bash can be run on a variety of platforms and operating systems that support Bash, such as Unix, Linux, macOS, and even Windows through emulators or virtual machines. Thus, the same script can be used in different environments without the need for significant modifications.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3. Flexibility and customization<\/h3>\n\n\n\n<p>The script is very flexible and can be customized to meet specific needs. These scripts can be easily modified and combined with other programming languages or utilities to create more powerful solutions according to specific needs.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4. Easy access and editing<\/h3>\n\n\n\n<p>No special tools or software are required to create bash scripts. This File can be edited using any text editor, and most operating systems come with a built-in shell interpreter, making it easily accessible to anyone.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">5. Integration with other tools and applications<\/h3>\n\n\n\n<p>It can be integrated with various tools and applications, such as databases, web servers, and cloud services. This enables more complex automation and more sophisticated system management.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">6. Easy to debug<\/h3>\n\n\n\n<p>It is relatively easy to analyze and repair when problems occur. Most shells provide built-in debugging and error reporting tools that help in quick identification and resolution of problems.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Basic Bash Script Commands<\/h2>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img decoding=\"async\" width=\"1024\" height=\"683\" src=\"https:\/\/audithink.com\/wp-content\/uploads\/2024\/10\/ezgif-1-1d5917c94c_11zon-1024x683.webp\" alt=\"sample script\" class=\"wp-image-3289\" style=\"width:652px;height:auto\" title=\"\" srcset=\"https:\/\/audithink.com\/wp-content\/uploads\/2024\/10\/ezgif-1-1d5917c94c_11zon-1024x683.webp 1024w, https:\/\/audithink.com\/wp-content\/uploads\/2024\/10\/ezgif-1-1d5917c94c_11zon-300x200.webp 300w, https:\/\/audithink.com\/wp-content\/uploads\/2024\/10\/ezgif-1-1d5917c94c_11zon-768x512.webp 768w, https:\/\/audithink.com\/wp-content\/uploads\/2024\/10\/ezgif-1-1d5917c94c_11zon-1536x1024.webp 1536w, https:\/\/audithink.com\/wp-content\/uploads\/2024\/10\/ezgif-1-1d5917c94c_11zon-2048x1365.webp 2048w, https:\/\/audithink.com\/wp-content\/uploads\/2024\/10\/ezgif-1-1d5917c94c_11zon-18x12.webp 18w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><figcaption class=\"wp-element-caption\">Bash Script (sumber: Pexels.com)<\/figcaption><\/figure>\n<\/div>\n\n\n<p>Here are some basic bash script commands you need to understand.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. echo<\/h3>\n\n\n\n<p>This first basic bash script command serves to display text on the terminal screen, usually used to show the output of the script.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. read<\/h3>\n\n\n\n<p>The second basic bash script command allows scripts to take input from users, this command is often used to enter data into variables.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3. if, else, elif<\/h3>\n\n\n\n<p>The third basic bash script command is a control structure used to make conditional decisions. This structure allows the script to execute different commands based on specific conditions.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4. for loop<\/h3>\n\n\n\n<p>The fourth basic bash script command is useful for repeating a series of commands a specified number of times, serving to iterate through a list of items or a range of numbers.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">5. while loop<\/h3>\n\n\n\n<p>The basic bash script command is a loop that lasts as long as the specified condition remains true, applied to a process that needs to be repeated until the specified condition is met.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">6. case<\/h3>\n\n\n\n<p>The basic command of bash script which is a control structure to perform selection based on a certain pattern, is often used to handle various cases in a single command block.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">7. function<\/h3>\n\n\n\n<p>These basic commands define functions (blocks of commands that can be called many times in a script), these commands help in the organization and management of complex code.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">8. chmod<\/h3>\n\n\n\n<p>This basic command changes the access permissions for a file or directory, essential to control who can read, write or run the file.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">9. \/ (pipe)<\/h3>\n\n\n\n<p>Basic commands stream the output from one command to another, allowing concatenation of multiple commands in a single line.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">10. &gt; and &gt;&gt;<\/h3>\n\n\n\n<p>The fourth basic bash script command is useful for repeating a series of commands a specified number of times, serving to iterate through a list of items or a range of numbers.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">11. grep<\/h3>\n\n\n\n<p>This basic command searches for patterns in text, useful for finding specific strings in files or command output.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">12. cut<\/h3>\n\n\n\n<p>As the name implies, this basic command cuts a part of each line either in the file or in the output, being used to extract a specific part of the text.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">13. cd<\/h3>\n\n\n\n<p>This basic command serves to change the current directory, this command to navigate in the system a file.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">14. mkdir &amp; rmdir<\/h3>\n\n\n\n<p>This basic command is used to create and delete directories. 'mkdir \u2018to create a new directory, while\u2019 rmdir' to delete an empty directory.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">15. rm<\/h3>\n\n\n\n<p>This basic command is useful for deleting files or directories, this command can be said to be important in file management.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">16. cp &amp; mv<\/h3>\n\n\n\n<p>Finally, this command is used to copy and move files or directories. 'cp \u2018is useful for copying, while\u2019 mv' is useful for moving or renaming files.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Bash Script Example<\/h2>\n\n\n\n<p>After knowing about understanding and some basic bash script commands, here are some examples of bash scripts, among others:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. Simple Bash Script Example<\/h3>\n\n\n\n<p>#!\/bin \/ bash&nbsp;<\/p>\n\n\n\n<p># Script to print messages&nbsp;<\/p>\n\n\n\n<p>echo \u201cHello, World!\u201d<\/p>\n\n\n\n<p>Explanation:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>#!\/bin \/ bash: Shebang, indicating that this script will be executed using Bash.<\/li>\n\n\n\n<li>echo \u201cHello, World!\": The echo command is used to print text to the screen.<\/li>\n<\/ul>\n\n\n\n<p>To run the script above, there are several steps you must do, here is the explanation.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Simpan script di file, misalnya hello.sh.<\/li>\n\n\n\n<li>Grant execution permission with the command:<\/li>\n<\/ul>\n\n\n\n<p>chmod +x hello.sh<\/p>\n\n\n\n<p>Run the script with the command:. \/hello.sh<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. Examples Of Complex Bash Scripts<\/h3>\n\n\n\n<p>#!\/bin \/ bash<\/p>\n\n\n\n<p># Script accepts name input<\/p>\n\n\n\n<p>echo \" enter your name.\u201d<\/p>\n\n\n\n<p>read the name<\/p>\n\n\n\n<p>echo \u201chello, $name. Welcome to Bash scripting!\u201d<\/p>\n\n\n\n<p>When running this script, the user will be prompted to enter a name, and then the script will print a customized message with that name.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>In this article, we take the conclusion that, bash script is a collection of commands executed through bash for automation of various tasks in Unix or Linux based operating systems.<\/p>\n\n\n\n<p>With features such as variables, functions, and flow control, Bash scripts allow users to automate routine tasks, such as file Management, Process Management, and System Administration.<\/p>\n\n\n\n<p>Its main benefits include automation, portability, flexibility, as well as integration with other tools and applications. A basic understanding of Bash commands such as echo, if-else, for, and grep, provides a solid foundation for creating simple to complex scripts.<\/p>\n\n\n\n<p>Lastly, if you are looking for the best audit app, trust Audithink. The features of the application that we offer are in the form of risk offerings, Planning, Monitoring, and much more. <\/p>\n\n\n\n<p><a href=\"https:\/\/audithink.com\/en\/contact\/\"><strong>Contact Contact Us<\/strong><\/a> to obtain other information or consult on the needs of your audit team.<\/p>","protected":false},"excerpt":{"rendered":"<p>Para pengguna Linux maupun macOS mungkin sudah sering mengetahui kata bash. Bisa dibilang juga bash menyediakan syntax yang kuat dan fleksibel serta berbagai macam perintah bawaan untuk mengelola file, proses, dan juga tugas lainnya dalam sistem. Lalu bagi mereka yang bukan pengguna Linux atau macOS, apakah itu bash? Supaya tidak terlalu penasaran, mari kita pahami [&hellip;]<\/p>\n","protected":false},"author":11,"featured_media":3288,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[15],"tags":[29],"class_list":["post-3287","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog","tag-teknologi-aplikasi-audit"],"acf":[],"_links":{"self":[{"href":"https:\/\/audithink.com\/en\/wp-json\/wp\/v2\/posts\/3287","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/audithink.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/audithink.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/audithink.com\/en\/wp-json\/wp\/v2\/users\/11"}],"replies":[{"embeddable":true,"href":"https:\/\/audithink.com\/en\/wp-json\/wp\/v2\/comments?post=3287"}],"version-history":[{"count":2,"href":"https:\/\/audithink.com\/en\/wp-json\/wp\/v2\/posts\/3287\/revisions"}],"predecessor-version":[{"id":3297,"href":"https:\/\/audithink.com\/en\/wp-json\/wp\/v2\/posts\/3287\/revisions\/3297"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/audithink.com\/en\/wp-json\/wp\/v2\/media\/3288"}],"wp:attachment":[{"href":"https:\/\/audithink.com\/en\/wp-json\/wp\/v2\/media?parent=3287"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/audithink.com\/en\/wp-json\/wp\/v2\/categories?post=3287"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/audithink.com\/en\/wp-json\/wp\/v2\/tags?post=3287"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}