Bash Function -> Command not found

2020-04-02 06:07发布

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.

Any help much appreciated

标签: bash
4条回答
不美不萌又怎样
2楼-- · 2020-04-02 06:46

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
查看更多
家丑人穷心不美
3楼-- · 2020-04-02 06:53
#!/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

查看更多
家丑人穷心不美
4楼-- · 2020-04-02 06:55

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)
查看更多
\"骚年 ilove
5楼-- · 2020-04-02 06:56

You need to invoke the function by saying:

system_info

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

查看更多
登录 后发表回答