Bash Scripting: Part – 2

Functions, Conditional Statements and Loops in Bash

Let’s continue leaning about bash scripting. In this article we will deep dive in Functions, Conditional Statements and Loops in Bash.

You can checkout Basics of Bash before deep diving into scripting part.

Function in bash script

To organize and reuse code with more efficiency and readability of the script, we can use functions in bash script.

Here the function name is details, and function body consists of three echo commands. Every time a function call is made by using the function name, three echo commands are executed. Important point is that the function definition must precede function call, otherwise the script will return function not found error.

Output of above script is as follows:

Conditional Statements

To implement decision making based on certain conditions within a shell script, we can use conditional statements in bash script.

Two integer variables ‘a’ and ‘b’ are initialized with values 10 and 20 respectively. After that, we begin an if conditional block and compare both variables and if the comparison evaluation yields true, then echo command will print the result as the value within the variable $a is less than the value in variable ‘$b’. ‘fi’ keyword is used to close ‘if’ conditional block.

In the situation when the variable ‘$a’ greater than ‘$b’ , echo command will print the result as the value within the variable ‘$a’ is greater than the value in  variable ‘$b’

Output of above script is as follows:

Bash Loops

To iterate given tasks number of times, we can use loops in bash script. Let’s take a look at various loops.

For loop:

There are 4 shell reserved keywords: ‘for’, ‘in’, ‘do’ and ‘done’

The above code can be read as:

“FOR each item in list [1, 2, 3], assign each item temporarily into a variable ‘i’ after which DO echo ‘$i’ in order to print the item and keep printing until all items in the list are DONE.”

Output of above script is as follows:

While Loop:

The enclosed code within the while loop will get executed only while the ‘cnt’ variable is less than 3. During each loop iteration, the variable ‘cnt’ is incremented by one. Once the variable ‘cnt’ is equal to 3, the condition defined in while loop becomes false and while loop execution is terminated.

Output of above script is as follows:

Positional parameters

Positional parameters are nothing but command line arguments which can be accessible within a script as $1, $2 … $N variables.  During the script execution, any additional items supplied after the program name are considered arguments and are available during the script execution.

Here in above code snippet, $1 and $2 represents first and second command line argument. $* represents all the command line arguments. In below snippet, we are passing 3 command line arguments, namely ‘bash’, ‘scripting’, ‘rocks’.

$1 holds ‘bash’, $2 holds ‘scripting’ and $* holds all the three arguments.

98 thoughts on “Bash Scripting: Part – 2”

  1. Hello there! This article couldn’t be written any better! Going through this article reminds me of my previous roommate! He always kept talking about this. I most certainly will send this post to him. Fairly certain he’s going to have a good read. Thank you for sharing!

  2. Oh my goodness! Incredible article dude! Thank you, However I am having troubles with your RSS. I don’t know why I can’t subscribe to it. Is there anybody else having similar RSS problems? Anybody who knows the answer will you kindly respond? Thanx!

  3. Hello, every time i used too check web sitte posts herde inn tthe early hours in the morning,
    forr tthe reason tha i likje to gaiun knowldge off ore and more.

  4. Експедування

    Because the admin of this web site is working, no question very soon it will be well-known,
    due to its quality contents.

  5. Hello There. I found your blog the use of msn. This is a really smartly written article.
    I’ll be sure to bookmark it and come back to learn extra of your useful information.
    Thank you for the post. I will certainly return.

  6. of colurse likee your website buut you neeed tto test the spelling oon quit a few of your posts.

    A numbr of thuem arre rife wwith spelling probllems annd I inn
    finding iit verry bothersome to tell thhe truth then again I woll definitely come
    again again.

  7. Hayden Stanaland

    Thank you for your post. I really enjoyed reading it, especially because it addressed my issue. It helped me a lot and I hope it will also help others.

  8. Douglas Eggebrecht

    You helped me a lot with this post. I love the subject and I hope you continue to write excellent articles like this.

  9. Thank you for posting this post. I found it extremely helpful because it explained what I was trying to say. I hope it can help others as well.

  10. I have learned some important matters through your site post. One other subject I would like to say is that there are lots of games in the marketplace designed in particular for preschool age children. They contain pattern acceptance, colors, wildlife, and styles. These commonly focus on familiarization instead of memorization. This helps to keep children occupied without feeling like they are studying. Thanks

  11. I really enjoyed reading your post, especially because it addressed my issue. It helped me a lot and I hope it can help others too.

  12. You really make it seem really easy together with your presentation however I in finding this topic to be actually one thing which I feel I might by no means understand. It kind of feels too complex and very vast for me. I’m taking a look ahead for your subsequent submit, I?ll try to get the dangle of it!

  13. Antionette Cacatian

    Thanks for posting. I really enjoyed reading it, especially because it addressed my problem. It helped me a lot and I hope it will help others too.

  14. Mathilde Peckens

    Great goods from you, man. I’ve understand your stuff previous to and you are just extremely wonderful. I actually like what you have acquired here, really like what you are saying and the way in which you say it. You make it entertaining and you still care for to keep it smart. I can not wait to read much more from you. This is actually a wonderful web site.

  15. I am really impressed with your writing skills as well as with the layout on your blog. Is this a paid theme or did you customize it yourself? Either way keep up the excellent quality writing, it?s rare to see a nice blog like this one these days..

  16. I loved as much as you will receive carried out right here. The sketch is tasteful, your authored subject matter stylish. nonetheless, you command get got an nervousness over that you wish be delivering the following. unwell unquestionably come further formerly again as exactly the same nearly very often inside case you shield this hike.

  17. Pretty section of content. I just stumbled upon your site
    and in accession capital to assert that I get in fact enjoyed
    account your blog posts. Any way I will be subscribing to your feeds and even I achievement you access consistently rapidly.

  18. Good write-up, I’m normal visitor of one’s blog, maintain up the nice operate, and It is going to be a regular visitor for a long time.

  19. Howdy fantastic blog! Does running a blog similar to
    this require a great deal of work? I have absolutely no
    understanding of programming however I was
    hoping to start my own blog in the near future. Anyhow,
    should you have any suggestions or tips for new blog owners please share.
    I know this is off topic nevertheless I just had to ask.
    Kudos!

  20. Hi it’s me, I am also visiting this website on a regular basis,
    this web page is genuinely fastidious and the people are actually sharing good thoughts.

  21. Thanks for the auspicious writeup. It if truth be told was a leisure account it.
    Glance complicated to more added agreeable from you! By the
    way, how could we be in contact?

  22. I got this web site from my pal who shared with me on the topic of this web page and at the moment this time
    I am visiting this web site and reading very informative articles or reviews at this place.

  23. Good day! I could have sworn I’ve been to this website before but after going through some of the
    posts I realized it’s new to me. Anyways, I’m definitely happy I
    found it and I’ll be book-marking it and checking back often!

  24. We are a bunch of volunteers and opening a new scheme in our community.
    Your website offered us with useful information to work on. You’ve performed a formidable process
    and our whole community can be grateful to you.

  25. Hello i am kavin, its my first occasion to commenting anywhere, when i read
    this paragraph i thought i could also make comment due to this brilliant paragraph.

  26. Hey I am so excited I found your weblog, I really found you by error, while I
    was looking on Aol for something else, Regardless I am
    here now and would just like to say thank you for a remarkable post
    and a all round interesting blog (I also love the theme/design), I don’t have time to browse it all
    at the moment but I have book-marked it and also added your RSS feeds, so
    when I have time I will be back to read more, Please do keep up the fantastic work.

  27. What’s Taking place i am new to this, I stumbled upon this I’ve
    discovered It positively useful and it has helped me out loads.
    I hope to contribute & assist other customers like its helped me.
    Good job.

  28. Hi there, i read your blog from time to time and i own a similar one and i was just curious
    if you get a lot of spam remarks? If so how do you protect against it, any plugin or anything you can advise?

    I get so much lately it’s driving me crazy so any support is
    very much appreciated.

  29. I do not even know the way I stopped up right here, but I assumed this submit was great.

    I do not understand who you’re however certainly
    you’re going to a well-known blogger if you happen to aren’t already.
    Cheers!

  30. Hello There. I discovered your weblog the use of msn. That is a very neatly written article.

    I will be sure to bookmark it and come back to learn more of your useful info.
    Thanks for the post. I will certainly return.

  31. I love your blog.. very nice colors & theme. Did you design this website yourself
    or did you hire someone to do it for you?
    Plz reply as I’m looking to create my own blog and would like to find out where u got this from.
    appreciate it

  32. I was suggested this website by my cousin. I am
    not sure whether this post is written by him as no one else know such detailed about my difficulty.
    You’re wonderful! Thanks!

  33. It’s hard to come by experienced people on this topic,
    however, you seem like you know what you’re
    talking about! Thanks

  34. An outstanding share! I have just forwarded this onto a friend who had been doing a little
    homework on this. And he actually bought me lunch due to the fact that I stumbled upon it
    for him… lol. So allow me to reword this….

    Thanks for the meal!! But yeah, thanks for spending some time to talk
    about this topic here on your web site.

  35. graphic tees men

    Hі! I could ave sworn I’ve been to this blog before bᥙt after
    readіng trough some of the post I realized іt’s new to me.
    Nonetheless, I’m definitely gld I found it and I’ll be bookmarking
    and checking back ⲟften!

  36. Have you ever thought about publishing an e-book or guest authoring on other websites?

    I have a blog centered on the same topics you discuss and
    would really like to have you share some stories/information. I know my subscribers would value your work.
    If you’re even remotely interested, feel free to send me an e-mail.

  37. Hi, I think your site might be having browser compatibility issues.
    When I look at your blog in Safari, it looks
    fine but when opening in Internet Explorer,
    it has some overlapping. I just wanted to give you
    a quick heads up! Other then that, fantastic blog!

  38. We’re a group of volunteers and opening a new scheme in our
    community. Your website provided us with valuable information to work on. You have done a formidable job and our whole community
    will be grateful to you.

  39. Hi there to every one, the contents existing at this web page are truly awesome for people experience,
    well, keep up the good work fellows.

  40. Hey there, I think your website might be having browser compatibility issues.
    When I look at your blog in Opera, it looks fine
    but when opening in Internet Explorer, it has some overlapping.
    I just wanted to give you a quick heads
    up! Other then that, very good blog!

  41. I enjoy what you guys are usually up too. This type of clever work and coverage!
    Keep up the wonderful works guys I’ve added you guys to our
    blogroll.

  42. I’m not sure exactly why but this web site is loading incredibly slow
    for me. Is anyone else having this problem or is it a issue on my
    end? I’ll check back later on and see if the problem still exists.

Leave a Comment

Your email address will not be published. Required fields are marked *