Bash Scripting: Execute command from within the script

Bash scripts are, essentially, just a series of Linux commands that have been chained together in order to accomplish something. Depending on your code, there are a few different ways to execute commands inside the script.

In this tutorial, we will go over a few ways to execute commands from within a Bash script on a Linux system.

In this tutorial you will learn:

  • How to execute command in Bash script
  • How to store output of executed command in variable
Two different ways to execute a command from within a Bash script
Two different ways to execute a command from within a Bash script
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software Bash shell (installed by default)
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Bash Scripting: Execute command from within the script examples




We will look at a few different scenarios below to see how to execute commands from within a Bash script. Check all of the examples below to learn how.

  1. Normally, we do not need to do anything special to execute a command inside of a Bash script. You just write the command the same way you would in your own terminal. Look at the following example where we execute three commands inside of our Bash script – echo, uptime, and who.
    #!/bin/bash
    
    echo "Here we are executing three commands."
    uptime
    who

    And here is what it looks like when we execute the script:

    $ ./test.sh
    Here we are executing three commands.
     23:39:36 up 1 min,  1 user,  load average: 3.36, 1.37, 0.51
    linuxconfig :0           2022-02-23 23:38 (:0)
    

    This is not any different than simply typing the commands in your terminal.

    $ echo "Here we are executing three commands."
    Here we are executing three commands.
    $ uptime
     23:40:05 up 2 min,  1 user,  load average: 2.68, 1.39, 0.54
    $ who
    linuxconfig :0           2022-02-23 23:38 (:0)
    
  2. Okay, that is simple enough. But now let’s look at another scenario. What if we need to store the results of a command inside of a variable in the Bash script? In that case, we will use a subshell with $( ) syntax, and store the result inside of a variable. Here is how to do it.
    #!/bin/bash
    
    var=$(date)
    
    echo $var

    And here is what it looks like when we execute the script:

    $ ./test.sh 
    Wed 23 Feb 2022 11:43:18 PM EST
    

    What is happening here is that we are executing the date command inside of the Bash script, but storing the result inside of the var variable, rather than immediately echoing the result. At the end of the script, we echo the var variable to see that the date has been stored inside of it.



    DID YOU KNOW?
    You can also use backticks ` ` instead of a subshell $( ) to execute a command. But the backticks method is older and does not support command nesting, so you should prefer to use the subshell in all your future Bash scripts.
  3. The subshell can also be used within the echo command. Here is an example of how it’s normally done.
    #!/bin/bash
    
    echo "The current date is $(date)"

    And here is what it looks like when we execute the script:

    $ ./test.sh 
    The current date is Wed 23 Feb 2022 11:48:06 PM EST
    

    There are simpler and better ways to get this result, but it is just an example so you can get an idea how the subshell works.

Closing Thoughts

In this tutorial, we saw how to execute a command from within a Bash script on Linux. Normally, executing commands will work the same way they do in terminal, but sometimes you will need to use a subshell in order to store the result of a command in a variable.



Comments and Discussions
Linux Forum