Ultimate shell scripting cheatsheet - part 1
Shell and Shell scripting is unarguably one of the best asset of UNIX systems. Yeah prima facie, it looks intimidating and old-school, this is all because it’s almost the same for almost five decades now.
Let’s begin!
#!/bin/sh
– Shebang
Almost all shell scripts out there have this weird looking line on the top the file. What does this line even mean? is it a comment? or a superstition?
Actually, this line has a very significant meaning and it tells the absolute
path of the interpreter which is supposed to execute the script. This path can be
/bin/bash
or /bin/sh
for shell and #!<path/to/python/bin>
for python scripts.
To find out your shell interpreter just $ echo $SHELL
(inside terminal)
and to find your python interpreter you can use $ which python
command.
The advantage is that — since *NIX systems don’t give special status to extension of a file (like Windows does) and it all depends upon the content of file and the program reading it. So this Shebang line declares the interpreter/program on your behalf and you can avoid explicitly defining the interpreter.
With shebang line on top, you can run your scripts directly like $ ./script.sh
(execute +x permission is required though) and it should work as expected.
Note: don’t hardcode /bin/bash
as your shell because you will lose portability
across systems as bash
is not the default shell in all systems,
OSX from Catalina has zsh
as default shell.
Variables
Shell apart from being an interface to interact with the Kernel, it is also a programming language and has the concept of variables. Let’s see them in action.
|
|
Conditionals
Without conditionals any scripting is limited, there are rare situations in real-world programs to be written without a single if-else statement (no switch case blocks too). Shell scripting is also no exception here but its conditionals are quite weird and might not be intuitive for beginners.
if-else statement
|
|
case statement
Apart from if-else statements for conditionals, shell also offers elegant case statement which you can somewhat relate to switch-case statements in General programming languages. Although, it has no concept of default and break keyword inside case
|
|
Loops – for and while
Sometimes you need to iterate over some sequence or need to run a block of code for certain number of times — What you do, you use loops. Like conditionals loops are also integral part of any programming language.
|
|
Phew!, thanks a lot for bearing with me till here, that’s all for today. Let’s conclude part 1 here and please practice as much as you can. No need to create real world programs as of now, just play with scripting, do some random stuff because learning with fun is never exhausting.
Conclusion
As we know, Shell is an interface for us to talk to kernel and scripting helps us to take this communication to next-level.
With programmable interface, you can automate your system administrative tasks like backing up your Database periodically, notify you when your Disk usage exceeds a certain threshold and much more.
If you want to be a true UNIX/Linux admin rather than a normal user you must learn shell scripting and believe me you will never look back again.
See you in Part 2
#TillThenHappyCoding