How do i use functions in bash?



  • You can use functions in one of two ways in bash. I know it is a function by using the parentheses "( )" so prefer the second method, anything to get away with less typing!

    function functname
                   {
                   shell commands
                   }
    

    or

     functname( )
    {
        shell commands
                   }
    

    e.g:
    Create a file bash.function, add the function called printmsg to the file, run the script.

    $ cat bash.function
    
    printmsg()
    { 
     echo "Hello World"
    }
    
    printmsg
    
    $ chmod +x bash.function.sh 
    $ ./bash.function.sh 
    Hello World
    

Log in to reply
 

© Lightnetics 2024