Skip to main content

Command Palette

Search for a command to run...

Shell scripting in Linux

Published
•2 min read•View as Markdown
Shell scripting in Linux
A

I'm a DevOps & Cloud Engineer and a passionate technical blogger. I share insights and best practices on DevOps, cloud architecture, CI/CD, automation, and more. Let's explore the world of efficient software delivery and scalable cloud infrastructure together.

Your One and Only Linux Shell Scripting Tutorial

  1. Shebang: Specifies the interpreter to be used for executing the script.

     shellCopy code#!/bin/bash
    
  2. Comments: Used for adding comments in the script to provide information or explanations.

     shellCopy code# This is a comment
    
  3. Variables: Used to store values.

     shellCopy codevariable_name=value
    
  4. Command substitution: Allows the output of a command to replace the command itself.

     shellCopy codevariable_name=$(command)
    
  5. Conditionals: Used to perform actions based on certain conditions.

     shellCopy codeif condition
     then
         # commands
     elif condition
     then
         # commands
     else
         # commands
     fi
    
  6. Loops: Used to iterate over a sequence of values or perform repetitive tasks.

    • For loop:

        shellCopy codefor variable in list
        do
            # commands
        done
      
    • While loop:

        shellCopy codewhile condition
        do
            # commands
        done
      
    • Until loop:

        shellCopy codeuntil condition
        do
            # commands
        done
      
  7. Functions: Used to group a set of commands into a reusable block.

     shellCopy codefunction_name() {
         # commands
     }
    
  8. Input/output redirection:

    • Redirecting standard output (stdout) to a file:

        shellCopy codecommand > file
      
    • Redirecting standard error (stderr) to a file:

        shellCopy codecommand 2> file
      
    • Appending stdout to a file:

        shellCopy codecommand >> file
      
    • Redirecting stderr and stdout to different files:

        shellCopy codecommand > stdout_file 2> stderr_file
      
  9. Command-line arguments: Allowing passing arguments to a script.

     shellCopy codescript_name arg1 arg2 arg3
    
  10. Conditional execution:

    • Execute a command only if the previous command succeeds (return code 0):

        shellCopy codecommand1 && command2
      
    • Execute a command only if the previous command fails (return code non-zero):

        shellCopy codecommand1 || command2
      
  11. Exit status: Checking the exit status of a command.

    shellCopy code$?
    
  12. String manipulation:

    • Concatenating strings:

        shellCopy codenew_string="$string1$string2"
      
    • Extracting substrings:

        shellCopy codesub_string="${string: start: length}"
      
    • String length:

        shellCopy codelength=${#string}
      

These are just a few essential commands and concepts in shell scripting. The possibilities and complexities of shell scripting are vast, and there are many more commands and features available.

More from this blog

Anurag's Blog

18 posts

I'm Anurag, a DevOps engineer and technical blogger at HasNode, merging Cloud Technology and DevOps for seamless software delivery.