0
0
Bash Scriptingscripting~20 mins

Running commands in background (&) in Bash Scripting - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Background Command Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
1:30remaining
What is the output of this background command?
Consider the following bash command run in a terminal:

sleep 2 & echo "Done"

What will be printed immediately after running this command?
Bash Scripting
sleep 2 & echo "Done"
ADone
BNo output until 2 seconds pass
CDone\nsleep 2
Dsleep: command not found
Attempts:
2 left
💡 Hint
The ampersand (&) runs the first command in the background, so the next command runs immediately.
💻 Command Output
intermediate
1:30remaining
What happens if you run multiple background commands?
What will be the output of this script?

echo Start
sleep 1 &
sleep 2 &
echo End


Choose the correct output order.
Bash Scripting
echo Start
sleep 1 &
sleep 2 &
echo End
AStart\nEnd\n(sleep 1 and sleep 2 outputs)
BStart\nEnd
CStart\nsleep 1\nsleep 2\nEnd
DEnd\nStart
Attempts:
2 left
💡 Hint
Background commands do not produce output unless they have their own output commands.
🔧 Debug
advanced
2:00remaining
Why does this background command fail to run?
Look at this command:

my_script.sh &

It gives an error: 'my_script.sh: command not found'. Why?
Bash Scripting
my_script.sh &
ABackground commands cannot run scripts
BThe ampersand (&) is misplaced and causes syntax error
CThe script is not executable or not in PATH
DThe shell does not support background commands
Attempts:
2 left
💡 Hint
Check if the script file has execute permission and is in the current directory or PATH.
🚀 Application
advanced
2:00remaining
How to run a command in background and save its output to a file?
You want to run long_task.sh in background and save its output to output.log. Which command does this correctly?
Along_task.sh & > output.log
Blong_task.sh & output.log >
Clong_task.sh &> output.log &
Dlong_task.sh > output.log &
Attempts:
2 left
💡 Hint
Redirect output before the ampersand to capture it.
🧠 Conceptual
expert
2:30remaining
What is the effect of running a pipeline with background (&)?
Consider this command:

cat file.txt | grep 'hello' &

What happens when you run it?
AThe whole pipeline runs in background, output goes to terminal
BOnly 'grep' runs in background, 'cat' runs in foreground
CSyntax error because & cannot be used with pipelines
DThe pipeline runs in background, output is discarded
Attempts:
2 left
💡 Hint
The ampersand puts the entire pipeline in background.