23

Hi gusy I am trying to learn Bash and cannot seem to get this basic script to work.

#!/bin/bash

function system_info
{    
    echo "function system_info"
}

$(system_info)

I get a function: command not found issue.

4 Answers 4

25

Bash is trying to evaluate the string that is outputted by the system_info function. You'll want to try the following, which will just simply run the function:

system_info

or to store the outputted value to a variable:

value=$(system_info)
Sign up to request clarification or add additional context in comments.

2 Comments

+1 for explaining what is wrong and what happens as a result.
my function is with two inputs parameter and echoes the result that seems not to work. it gives an error of command not found
5

You need to invoke the function by saying:

system_info

$(...) is used for command substitution.

Comments

2

Invoke the function inside the script with just the function name and execute the script from the shell

#!/bin/bash
function system_info {
echo "function system_info"
}
system_info

Comments

1
#!/bin/bash

function system_info
{    
    echo "function system_info"
}

echo $(system_info)

Kind of redundant but it works without the command not found error.

Or This:

#!/bin/bash

function system_info
{    
  echo "function\n system_info"
}

printf "$(system_info)"

If you want to use newline character.

You can try this code in: https://www.tutorialspoint.com/execute_bash_online.php

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.