To run a command in the background in a bash script, you simply append an ampersand (&) at the end of the command. Here’s a simple example:
- Create a bash script, for example
background_task.sh:
#!/bin/bash
# A simple script that runs a command in the background
echo "Starting background task..."
sleep 10 & # This command will run in the background
echo "You can continue using the terminal while the task runs."
- Make the script executable:
chmod +x background_task.sh
- Run the script:
./background_task.sh
In this example, the sleep 10 command will run in the background, allowing the script to continue executing and the terminal to remain available for other commands.
