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:
- In the terminal, type ‘vi filename’ and press enter.
- Press ‘i’ to add contents to file.
- Add contents to file.
- 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
- We will use ‘vi’ editor to create a new file called ‘task.sh’ containing all the basic commands, each on a separate line.
- To make this file executable, we use command ‘chmod’ with an option ‘+x’
- 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.
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.
This really answered my drawback, thanks!
This really answered my drawback, thank you!
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.
This is a topic which is close to my heart… Cheers! Exactly where can I find the contact details for questions?
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.
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.
카지노사이트
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.
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.
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.
Pretty! This has been an extremely wonderful post. Thank you for providing this information.
Great post.
This is a topic that’s near to my heart… Best wishes! Where are your contact details though?
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.
Very nice blog post. I certainly appreciate this site. Stick with it!
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 .
wonderful post.Ne’er knew this, thanks for letting me know.
Very good post. I’m facing a few of these issues as well..
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.
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!
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!
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!
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.
This piece of writing presents clear idea designed for the new viewers of blogging, that actually how to do blogging.
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!
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!
These are truly fantastic ideas in concerning blogging. You
have touched some fastidious things here. Any way keep up wrinting.
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.
This is a topic that’s near to my heart…
Cheers! Exactly where are your contact details though?
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.
These are really wonderful ideas in about blogging.
You have touched some nice points here. Any way keep up wrinting.
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.
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.
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.
I love it when individuals get together and share ideas.
Great blog, stick with it!
May I have further information on the topic?
Thanks for your help and for posting this article. It’s been great.
Thank you for your excellent articles. May I ask for more information?
Thanks for posting such an excellent article. It helped me a lot and I love the subject matter.
Thank you for providing me with these article examples. May I ask you a question?
Thank you for writing about this topic. Your post really helped me and I hope it can help others too.
Thanks for writing this article. It helped me a lot and I love the subject.
Thank you for writing such an excellent article, it helped me out a lot and I love studying this topic.
You’ve been a big help to me. Thank you!
I must say you’ve been a big help to me. Thanks!
The articles you write help me a lot and I like the topic
You’ve been great to me. Thank you!
Thank you for being of assistance to me. I really loved this article.
Please tell me more about this
Thank you for posting such a wonderful article. It really helped me and I love the topic.
Thank you for writing about this topic. It helped me a lot and I hope it can help others too.
Thanks for writing this article. I enjoy the topic too.
Please answer my question. How can i contact you regards this article?
You’ve been great to me. Thank you!
May I have further information on the topic?
What are your articles about? Please tell me more.
You should write more articles like this, you really helped me and I love the subject.
I want to thank you for your assistance and this post. It’s been great.
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.
Thanks for your help and for writing this post. It’s been great.
Thank you for posting such a wonderful article. It really helped me and I love the topic.
You’ve been a big help to me. Thank you!
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.
What are your articles about? Please tell me more.
I have to thank you for this article
Thank you for writing this post. I like the subject too.
May I have information on the topic of your article?
How do I find out more?
Thanks for your help and for posting this. It’s been wonderful.
Please answer my question. How can i contact you regards this article?
Please provide me with more details on the topic
You’ve been a great help to me. Thank you!
Thank you for writing about this topic. It helped me a lot and I hope it can help others too.
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!
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.
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.
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?
Hurrah! Finally I got a webpage from where I know how to genuinely obtain helpful facts
regarding my study and knowledge.
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!
Great article 🙂