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

Comments & Discussion

Facing issues? Have questions? Post them here! We're happy to help!