Most of the programmers use programming languages such as C,C++, Python, Javascript. They use shell for many tasks . But most of them don’t know that they can program with shell for automating stuff that we find ourselves doing over and over again. Though shell is limited in number of tokens, its power lies within the hands of the developer. He/She can write a program and install it and can use it shell just like any other command. How awesome is that ? So next time you need something like this, just make a coffee and start coding…. Dive In In this article , I will share some basic things about shell that is necessary to program. Before That, Lets do a quick Review of most used commands List the files and directories in the Current directory or in the given directory ls — Print the current working directory pwd — Make a directory with a name given in the current directory mkdir — Remove specified files and folders rm — Produce a sequence from start to stop with increment steps seq — Change the current directory cd — Prints the contents of the file cat — Prints the string passed to it in the terminal echo — There are plenty of more commands and above are enough for enough to follow. If you need to learn more or study above commands in detail, Feel free to do so. Lets Get Started Let’s Answer this question What are things needed in a programming Language for basic stuff ? Answer is … Variables and Data types Control Statements [ if, while, for ..] Arithmetic Expressions and Boolean Expressions Well, above are the things needed to do basic automation But Shell has also got Arrays Regular Expressions and related tools I/O Functions for interacting with the user and the environment Functions Others Start Note shell programs are saved with .sh extension. They can be run with sh pro1.sh #shellname program.sh #Example There are many shells such as Z shell, C shell , bash, an lot of others. Most common is bash. Install any if you don’t have any. Ask your administrator for permission if you don’t have sufficient rights for executing programs. This article assumes that you are using bash and you have programming experience. Variables and Data types Variables in shell can be set by simply typing VARIABLE=VALUE Examples are numberVariable=3; realVariable=3.14; stringVariable_1=pi; stringVariable_2= ; "pi" Control Statements [ if, while, for ..] If statement [testCondition] fiORif[testCondition] if then #codeblock then #code block else #codeblock fi Examples [ -eq ] if $a $b # -eq tests whether left operand is equal to the right operand then echo "a is equal to b" else echo "a and b are not equal" fi While Statement Like other programming Languages, syntax of while is shell is similar [ testCondition ] while do #code block done Examples i=0 [ -lt 10 ] ; i=$((i+1)); while $i do echo $i #increment the varaible i done For statement for statement syntax is variable sequence for in do #codeblock done variable is a usual variable whereas sequnce is a list of values separated by spaces. Example i 1 2 3 4 5 6 ; for in do echo $i done Arithmetic Expressions Usual Arithmetic expressions are possible in shell which includes operators +, -, *, / and % and usual hard-cored values and variables (prefixed with $). You should enclose the arithmetic expressions in $((exp)) or any other standard formats. See the code snippet below… i=5; $((1+2)); $(( -3)); $((7/2)); $((7%5)); echo echo $i echo echo Boolean Expressions This is the area where shell differs from most of the languages. Boolean expression is of of the form operand operand -relop operand can be hard-core values or variables or themselves boolean expressions and relop can be one of the following: for less than operator ( “<” in other languages ) -lt for less than or equal to operator ( “≤” in other languages ) -le for greater than operator ( “>” in other languages ) -gt for greater than or equal to operator ( “≥” in other languages ) -ge for equal to operator ( “=” in other languages ) -eq for equal to operator ( “!=” in other languages ) -ne for logical and operator ( “&&” in other languages ) -a for logical or operator ( “||” in other languages ) -o for not operator ( “! expression “ in other languages) ! expression— Look at the example below [ 3 -eq 3 -a 3 -ge 2 ] ; [ ! 3 -ge 4 ] ; if then echo "work begins" fi if then echo "! 3>4" fi That’s it for now. We can just take a moment and try to use it in a scenario. Next Part is my favourite. I always use that snippet for organizing Downloads Directory. Sample Program Lets us consider a simple scenario. Consider that you have all videos of every season of your favourite TV series in a single Folder and you want to organize them into seasons. Thanks to appropriate Naming (S0xE0x ) of episodes. Following snippet will do the job for you. i `seq 5` mkdir S ; mv *S * S ; for in #consider that we have 5 seasons do $i # creating directory $i $i #moving the files done f this 4 line script can save that much work, Just think about what else you can do with it. End Note So if you find yourselves typing some commands or tasks repeatedly, try to make it as a shell program. Congratulations! You made it to the end. I hope you found this article useful. Check out my profile to find more articles. Thank You. Happy Coding with Shell ………