anotherone
anotherone

Reputation: 795

Bash script echo error

What is wrong in this code? I am just trying to write something from a function.

$ cat system_info.sh 

#!/bin/bash

drive_space ()
{
    echo "drive space"
}

$(drive_space)

Error message

$ ./system_info.sh 
./system_info.sh: line 8: drive: command not found

Upvotes: 0

Views: 750

Answers (2)

choroba
choroba

Reputation: 241768

$(command) is command substitution. Bash expands it to the output of the command (similarly to `command`. In this case, the command's output is drive space, so bash tries to run it, but it can't find the drive command.

Command substitution is typically used when you need to capture the output in a variable:

output=$(drive_space)  # $output now contains "drive space".

To call a function, just use its name:

drive_space

Upvotes: 4

Kokogino
Kokogino

Reputation: 1056

$(drive_space) executes drive space which isn't a command.

#!/bin/bash

drive_space ()
{
    echo "drive space"
}

drive_space

Upvotes: 2

Related Questions