Skip to Content
Learning Linux Shell Scripting - Second Edition
book

Learning Linux Shell Scripting - Second Edition

by Ganesh Sanjiv Naik
May 2018
Beginner
332 pages
7h 28m
English
Packt Publishing
Content preview from Learning Linux Shell Scripting - Second Edition

Running functions in the background

We have already seen in previous chapters that to run any command in the background, we have to terminate the command using &:

    $ command &
  

Similarly, we can make the function run in the background by appending & after the function call. This will make the function run in the background so that the Terminal will be free:

#!/bin/bash 
dobackup() 
{ 
    echo "Started backup" 
    tar -zcvf /dev/st0 /home >/dev/null 2>& 1 
    echo "Completed backup" 
} 
dobackup & 
echo -n "Task...done." 
echo 

Test the script as follows:

    $ chmod +x function_17.sh
    $ ./function_17.sh
  

This should produce the following output:

    Task...done.
    Started backup
    Completed backup
  
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Learning Linux Shell Scripting

Learning Linux Shell Scripting

Ganesh Sanjiv Naik
Linux Shell Scripting Cookbook - Third Edition

Linux Shell Scripting Cookbook - Third Edition

Clif Flynt, Sarath Lakshman, Shantanu Tushar

Publisher Resources

ISBN: 9781788993197Supplemental Content