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.

72 thoughts on “Bash Scripting: Part – 1”

  1. 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.

  2. 베스트토토사이트

    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.

  3. 津田沼 整体 姿勢

    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.

  4. 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 .

  5. 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.

  6. 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!

  7. 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!

  8. 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!

  9. 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.

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

  11. 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!

  12. 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!

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

  14. 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.

  15. 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.

  16. 스핀카지노

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

  17. 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.

  18. 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.

  19. 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.

  20. 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.

  21. 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.

  22. 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!

  23. 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.

  24. 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.

  25. 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?

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

  27. 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 *