Shell Script Basics
原文地址 shell-script-basics
Echoing, Comments, Variables and Conditionals.
The best thing about GNU/Linux is that it’s packed with utilities that improve productivity immensely. One such utility is a shell that can help accomplish complex tasks with just a few sequences of commands. Most of the time, users operate shell in an interactive way.
However, it becomes really productive when we leverage the scripting capabilities of the shell.
In this article, we are going to explore the basics of shell scripting, how simple echo
command can be used in a shell script file, how to write comments, working with variables and conditionals.
Keeping it short and simple, let’s get started!
Shell Script Printing
When we are working with shell scripts printing on the output screen is called echoing which can be done using echo
.
1 |
|
Let’s do this inside a shell script file. Follow the below steps:
- Create a file, name it whatever you want with the
.sh
extension. Let’s sayecho_script.sh
. - Add following inside the file -
echo "Hello Shell"
, hitSave
and open a terminal on the file location. - On the terminal, write -
$ bash echo_script.sh
and hit enter, you should be able to see -Hello Shell
.
Comments are used to improve the readability of the script. Shell uses the pound #
symbol for comments. A line beginning with the pound #
symbol is ignored by the shell interpreter during execution.
The script given below shows the usage of the comment:
1 |
|
Go to terminal and type - $ bash filename.sh
(replace filename
with whatever name you have given) and hit enter. You should be able to see - Hello Shell
without printing anything given after the #
symbol.
Shell Script Variables
We can define variables to store information, which can be accessed within the shell script. There are certain rules we need to follow while defining variables:
- The variable name can contain any combination of letters from A-Z or a-z, digits from 0-9 or an underscore (_) character.
- The variable name should start with a letter or an underscore character.
- Variables are case sensitive.
By convention, shell variables are defined in the upper case. Given below is a simple script, which shows the usage of a variable within it:
1 |
|
cat
command is used to display the content of a file given after that. Try your hands on it.
So, we have a file hello.sh
which contains a variable NAME
which we used in the echo
statement as $NAME
that is after the $
symbol. Here we saw variable declaration and how inside double quotes a variable can be used.
If you hit - $ bash hello.sh
and hit enter, you should be able to see - Hello CodeKaro
on the terminal.
Shell Script Conditionals
Like other programming languages, shell supports conditional expressions like – if, if-else and case. Let us understand this with simple examples:
-
if
expression: In case we want to verify whether a file exists or not, we can use the if expression, as follows:
1 |
|
The script given above generates the following output when it is executed:
1 |
|
-
if-else
expression: Let us modify the above script to generate output when the file does not exist:
1 |
|
Type following and hit enter:
1 |
|
-
case
statement: Shell provides a switch statement-like functionality using the case statement. Let us understand this with the example given below:
1 |
|
The script given above generates the following output when it is executed:
1 |
|
1 |
|
1 |
|
In the case
statement, an asterisk (*) denotes a default case.
Well, that’s it from me.
We covered printing, variables, comments and conditionals, not too much, right?
- Just starting your Open Source Journey? Don’t forget to check Hello Open Source
- Want to showcase your Python project or just out there to explore? Check Awesome Python Repos
- Want to make a simple and awesome game from scratch? Check out PongPong
- Want to
++
your GitHub Profile README? Check out Quote - README
Till next time!
Namaste 🙏
Image Credits - pch.vector
Interested in reading more such articles from ?
Support the author by donating an amount of your choice.