Facebook Twitter Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    You are at:Home»Programming»Bash Shell»How to Debug a Shell Script?

    How to Debug a Shell Script?

    By RahulJune 28, 20212 Mins Read

    Debugging is the process of finding and resolving bugs within a computer program. It provides a huge output at the runtime to analyze each part of the execution. Which helps to identify the root cause of any error in the script. In this tutorial, you will learn how to Debug a shell script on the Linux command line.

    Advertisement

    We can debug a shell script in two ways. Either add the debugging instruction in the shell script by using “set -xv” or using -xv on command line while executing script.

    Adding Debug Instructions in Shell Script

    Let’s create a small script to test the debugging process. Create a new file and edit in text editor:

    nano checkdebug.sh 
    

    Add the following content to file:

    1
    2
    3
    4
    5
    6
    7
    #!/bin/bash
     
    set -xv  # This will enable debug
    cd /var/log/
    for i in "*.log"; do
    du -sh $i
    done

    Save changes and close the file.

    Next set the execute permissions on file and then execute it.

    chmod +x checkdebug.sh 
    ./checkdebug.sh 
    

    [Sample Output]

    cd /var/log/
    + cd /var/log/
    for i in "*.log"; do
     du -sh $i
    done
    + for i in '"*.log"'
    + du -sh boot.log mysqld.log post111.log post1121.log yum.log
    0       boot.log
    32K     mysqld.log
    0       post111.log
    0       post1121.log
    4.0K    yum.log
    

    Provide Debug Instructions from Command Line

    Using this option we don’t need to add “set -xv” in shell script. Just create a shell script like below

    nano checkdebug2.sh 
    

    1
    2
    3
    4
    5
    6
    #!/bin/bash
     
    cd /var/log/
    for i in "*.log"; do
    du -sh $i
    done

    and Execute like below

    sh -xv checkdebug2.sh 
    

    [Sample Output]

    #!/bin/bash
    
    cd /var/log/
    + cd /var/log/
    for i in "*.log"; do
     du -sh $i
    done
    + for i in '"*.log"'
    + du -sh boot.log mysqld.log post111.log post1121.log yum.log
    0       boot.log
    32K     mysqld.log
    0       post111.log
    0       post1121.log
    4.0K    yum.log
    

    bash debug shell programming shell scripts
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    10 Most Popular Linux Shells

    10 Most Popular Open Source Linux Shells

    How to Create Bash Aliases with Arguments

    How to Create Bash Aliases with Parameters

    Checking If a Command Succeeded in Bash Using the `$?` Special Variable

    View 1 Comment

    1 Comment

    1. Anwar Zahid on June 28, 2021 7:16 am

      Nice post. Thanks Rahul.

      Reply

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • How to Install PHP 8.2-7.4 on RHEL & CentOS Stream 9
    • How to Install MySQL 8.0 on RHEL & CentOS Stream 9
    • How to Split Large Archives in Linux using the Command Line
    • System.out.println() Method in Java: A Beginner’s Guide
    • Split Command in Linux With Examples (Split Large Files)
    Facebook Twitter Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.