# Shell Script - Basic [Part - 1]

### <mark>First Script:</mark>

* To find the path of bash on the Linux Terminal  
    `which bash`
    
* Run the below cmd to run/execute your shell script  
    `bash yourshellscript.sh`  
    `Or`  
    `./yourshellscript.sh`  
    `Or`  
    `sh yourshellscript.sh`
    
* Make sure the script has executable permissions and  
    `chmod 777 <script_name.sh> Or chmod 755 <script_name.sh>`
    
* When you want to Run the script as Command, make sure your script path is defined in the $PATH variable so the shell searches your script/cmd and executes it.  
    `echo $PATH $ export PATH=$PATH:/home/<Userdirectory>`
    
* **<mark>Variables</mark>**<mark>:</mark>  
    Variable names must be in **lower-case** with **underscores** to separate words”  
    Variables are **case-sensitive**.  
    Only **ALPHANUMERIC** OR **UNDERSCORES** should be used for variables when using **<mark>-</mark>** (hyphen) will cause errors for the Variables  
    i.e (Bad Variable: product-name)
    
      
    Variables are represented via **<mark>$ </mark>** (Dollar sign)
    
    i.e  
    System variable:  
    `echo $BASH`
    
    * Manually define variable  
        `name="Devops Team"`
        
    * Call the variables in the script  
        `name="Devops Team" echo "hello ${name}"`
        
    * To comment or to skip from execution you can use **<mark>#</mark>**(hash) at the start of the cmd
        
* User Input and arguments using "**<mark>read</mark>**"
    
    * Option: 1  
        `echo "Please enter your age: "`  
        `read age`  
        `echo "My age is ${age}"`
        
    * Option: 2
        
        `read -p "Please enter your age:" age`  
        `echo "Your age is $age"`
        
* Halting using sleep  
    `sleep 2`
    
* Take arguments from the user when running the script using **<mark>$1 </mark>** (**First argument**)
    
    * Run the shell script using  
        `bash`[`your-shell-script.sh`](http://yourshellscript.sh) `<argument>`
        
    * The file  
        `echo "Sub: Hello, I am $1"`
        
* <mark>If else statement</mark>  
    `if [ "$1" = "like" ]`  
    `then`  
    `echo "Hello, Please $1 this video"`  
    `else`  
    `echo "Okay, then please Subscribe:)"`  
    `fi`
    
* **<mark>Shell Script that prints</mark>** `I will complete #90DaysOofDevOps challenge`
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1681556815590/8de6c0ff-e866-4377-9383-7a18cd1d695f.png align="center")
    
    * **<mark>Shell Script with coloring effects</mark>** `I will complete #90DaysOofDevOps challenge`
        
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1681557459263/8aea9e5c-242c-4890-b86d-6fcd1f1eebf9.png align="center")
    

Code:-  
`for code in {0..255}; do echo -e "\e[38;05;${code}m $code: Colour"; done`

**Reference Url:-**  
[`https://stackoverflow.com/questions/5947742/how-to-change-the-output-color-of-echo-in-linux`](https://stackoverflow.com/questions/5947742/how-to-change-the-output-color-of-echo-in-linux)  
[`https://devopscube.com/linux-shell-scripting-for-devops/`](https://devopscube.com/linux-shell-scripting-for-devops/)  
[`https://www.geeksforgeeks.org/bash-script-read-user-input/`](https://www.geeksforgeeks.org/bash-script-read-user-input/)

* **<mark>Write a Shell Script to take user input, input from arguments, and print the variables.</mark>**
    

`Code:-`

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1681570405322/4d9f43ac-2a52-4bdc-b388-b06de055a740.png align="center")

`Script Output:-`

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1681570447792/e094dbf1-1995-470b-9988-e3ac0924462e.png align="center")

**<mark>SHELL</mark>** is a program which provides the interface between the user and an operating system. When the user logs in OS starts a shell for user. **Kernel** controls all essential computer operations, and provides the restriction to hardware access, coordinates all executing utilities, and manages Resources between process. Using kernel only user can access utilities provided by operating system.  
  
**Popular Types of Shell:**

1. Bourne Shell (sh)
    
2. C Shell (csh)
    
3. KornShell (ksh)
    
4. Bourne Again Shell (bash)
    
5. Z Shell (zsh)  
      
    This `#!` is called <mark>shebang </mark> or <mark>hashbang</mark>. The shebang plays an important role in shell scripting, specially while dealing with different types of shell.  
      
    The shebang is the combination of the <mark># </mark> (pound key) and <mark>!</mark> (exclamation mark). This character combination has a special meaning when it is used in the very first line of the script. It is used to specify the interpreter with which the given script will be run by default.
    
    So, if the first line of a script is:
    
    ```plaintext
    #!/bin/bash
    ```
    
    It means the interpreter should be bash shell.  
      
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1682934468327/37d06ef8-5967-4789-9e0c-000d7ad09080.png align="center")
