Bash Scripting: Part – 1

What is bash?

Bash is the shell or command language interpreter for GNU/ Linux operating systems that processes shell commands. In other words, it’s a shell interpreter that takes commands from user and performs actions with the help of underlying operating system services. Bash currently runs on nearly every version of Unix.

What is bash scripting?

On Linux/ Unix operating systems, we execute commands in terminal and interacts with the underlying operating system. (This terminal is nothing but an interface to the shell interpreter). Any command you run in your terminal can be run in a Bash script. A bash script is a set of commands executed in a sequence. Bash scripting is a great way to automate repetitive tasks and it saves a ton of time.

Let’s take a look at the terminal and execute few basic commands –

Need for bash scripting?

Bash scripting is intensively used in areas such as system administration, application deployment, DevOps (CICD pipeline), task automation, automated backups and many more.

Default interpreter / Script execution?

bash is a default interpreter on many GNU/Linux systems, thus most of the times we use it even without realizing. This is why, shell scripts often works even without defining bash as an interpreter. To see what is your default interpreter, execute command echo $SHELL:

To explicitly specify your script’s interpreter as Bash, first  we need to locate a full path to its executable binary using which command, and then prefix it with a  #! and insert it as the first line of your script.

To create a file using ‘vi’ editor:

  1. In the terminal, type ‘vi filename’ and press enter.
  2. Press ‘i’ to add contents to file.
  3. Add contents to file.
  4. To save file, press ‘esc’ and then ‘shift+colon’ and ‘wq’ and ‘enter’

First bash shell script:

Let’s create a bash script with basic commands

  1. We will use ‘vi’ editor to create a new file called ‘task.sh’ containing all the basic commands, each on a separate line.
  2. To make this file executable, we use command ‘chmod’ with an option ‘+x’
  3. Execute this file by prefixing its name with ‘./’

Variables in bash script:

Just like variables in any other programming languages, we can define and use variables in bash script, to store data, alter and reuse them throughout the script.

Let’s look at the script more closely. We have declared a variable user which contains a value of user name running a shell session. This is called command substitution. It means output of the ‘whoami’ command will be directly assigned to the user variable. The same logic applies to next variable day whose value is today’s day produced by ‘date +%A’ command.

‘echo’ command is to print a message while substituting variable names now prefixed by ‘$’ sign with their relevant values. ‘$BASH_VERSION’ and ‘$HOSTNAME’ are internal variables.

Output of above script is as follows:

Let’s talk about Functions, Conditional Statements and Loops in bash scripting in next article.

81 thoughts on “Bash Scripting: Part – 1”

  1. I would like to thnkx for the efforts you have put in writing this site. I am hoping the same high-grade blog post from you in the upcoming as well. In fact your creative writing abilities has encouraged me to get my own website now. Actually the blogging is spreading its wings quickly. Your write up is a good example of it.

  2. Thanks for your post on the vacation industry. I’d personally also like to include that if you are one senior taking into consideration traveling, it can be absolutely vital that you buy travel insurance for senior citizens. When traveling, golden-agers are at high risk of getting a health emergency. Obtaining the right insurance cover package in your age group can protect your health and provide peace of mind.

  3. This is a topic which is close to my heart… Cheers! Exactly where can I find the contact details for questions?

  4. ラブックス

    The next time I read a blog, Hopefully it doesn’t disappoint me just as much as this one. After all, Yes, it was my choice to read through, but I actually thought you would have something useful to say. All I hear is a bunch of complaining about something that you could possibly fix if you were not too busy seeking attention.

  5. You have made some really good points there. I checked on the net for additional information about the issue and found most individuals will go along with your views on this web site.

  6. 바카라추천

    Hey there, You have done an excellent job. I?ll certainly digg it and in my view suggest to my friends. I am sure they will be benefited from this web site.

  7. Heya i am for the first time here. I came across this board and I find It truly useful & it helped me out much. I hope to give something back and aid others like you helped me.

  8. 베스트토토사이트

    An interesting discussion is definitely worth comment. I do think that you need to publish more about this issue, it may not be a taboo matter but typically folks don’t talk about these topics. To the next! Many thanks.

  9. 津田沼 整体 姿勢

    This is a great tip particularly to those fresh to the blogosphere. Simple but very precise info… Thank you for sharing this one. A must read post.

  10. Hello my family member! I wish to say that this article is awesome,
    nice written and come with almost all important infos.
    I’d like to peer extra posts like this .

  11. What’s up to every one, it’s in fact a nice for me to pay a
    quick visit this web site, it includes valuable Information.

  12. This is a good tip particularly to those new to the blogosphere.
    Short but very accurate info… Many thanks for sharing this one.
    A must read article!

  13. I do not even know the way I finished up here, but
    I assumed this post was once good. I don’t recognise who you’re
    however certainly you’re going to a well-known blogger in case
    you aren’t already. Cheers!

  14. Hello would you mind letting me know which hosting company you’re working with?
    I’ve loaded your blog in 3 completely different browsers and I must say this blog loads a lot faster then most.
    Can you recommend a good hosting provider
    at a honest price? Many thanks, I appreciate it!

  15. Nice post. I learn something totally new and challenging on websites I stumbleupon everyday.
    It’s always interesting to read content from other writers
    and practice something from their websites.

  16. This piece of writing presents clear idea designed for the new viewers of blogging, that actually how to do blogging.

  17. It’s perfect time to make some plans for the future and it’s time to
    be happy. I have read this post and if I could I desire to suggest
    you some interesting things or tips. Perhaps you can write next
    articles referring to this article. I wish to read even more things about it!

  18. Do you mind if I quote a few of your articles as long as I provide credit and sources back to your website?
    My blog site is in the very same niche as yours and my visitors would definitely benefit from a lot of the information you
    present here. Please let me know if this alright with you.

    Regards!

  19. These are truly fantastic ideas in concerning blogging. You
    have touched some fastidious things here. Any way keep up wrinting.

  20. Gadzety elektroniczne

    You’ve made some decent points there. I checked on the internet to learn more about the issue and
    found most individuals will go along with your views
    on this site.

  21. Your style is really unique in comparison to other folks I’ve read stuff from.
    Many thanks for posting when you have the opportunity, Guess I will just book mark this
    page.

  22. 스핀카지노

    These are really wonderful ideas in about blogging.
    You have touched some nice points here. Any way keep up wrinting.

  23. Baskili tisort

    What’s Taking place i am new to this, I stumbled upon this I’ve discovered
    It positively useful and it has aided me out loads. I’m
    hoping to give a contribution & aid different customers like its
    helped me. Good job.

  24. Hi there, I wish for to subscribe for this blog to get most up-to-date
    updates, thus where can i do it please help.

  25. Generally I don’t learn article on blogs, but I would like to say that this write-up very compelled me
    to take a look at and do it! Your writing style has been amazed me.
    Thanks, very great post.

  26. Thank you for your post. I liked reading it because it addressed my issue. It helped me a lot and I hope it will help others too.

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

  28. Great site. Lots of useful info here. I?¦m sending it to several pals ans also sharing in delicious. And of course, thank you in your sweat!

  29. Hey There. I found your weblog using msn. This is a really neatly written article.
    I will make sure to bookmark it and come back to read more of your useful
    info. Thanks for the post. I’ll certainly comeback.

  30. Hello! Do you use Twitter? I’d like to follow you if that would be okay.
    I’m definitely enjoying your blog and look forward to new posts.

  31. Hello! Do you know if they make any plugins to protect against hackers?
    I’m kinda paranoid about losing everything I’ve worked hard on. Any suggestions?

  32. Hurrah! Finally I got a webpage from where I know how to genuinely obtain helpful facts
    regarding my study and knowledge.

  33. It’s a pity you don’t have a donate button! I’d without
    a doubt donate to this brilliant blog! I suppose for now i’ll settle for bookmarking
    and adding your RSS feed to my Google account.
    I look forward to brand new updates and will share this blog
    with my Facebook group. Chat soon!

Leave a Comment

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