Bash Function -> Command not found

前端 未结 4 967
后悔当初
后悔当初 2021-01-17 11:11

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\"         


        
相关标签:
4条回答
  • 2021-01-17 11:26

    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)
    
    0 讨论(0)
  • 2021-01-17 11:31
    #!/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

    0 讨论(0)
  • 2021-01-17 11:39

    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
    
    0 讨论(0)
  • 2021-01-17 11:44

    You need to invoke the function by saying:

    system_info
    

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

    0 讨论(0)
提交回复
热议问题