How to run a Command in Bash Script


If you want to run a bash command from within a bash script, you can simply run them as follows,

Example 1: Simple command with no argument

    #!/bin/bash
    
    whoami
    Output:

    c2cdev

Example 2: Simple command with arguments

    #!/bin/bash
    
    whoami
    Output:

    c2cdev

    Execute Command in Bash Script Example

Example 3: Storing Command Output to Variable

    #!/bin/bash
    
    date_today=$(date +"%Y-%m-%d %H:%M:%S")
    
    echo $date_today
    Output:

    2023-08-10 04:32:35

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org

Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap