Reputation: 2023
I am reading bash scripting and trying a simple fork scenario where script will fork a process and then the child will perform exec statement
Script is as below
#!/bin/bash
echo "I am parent process"
echo "my pid is $$"
pid=fork()
if [ $pid eq 0 ]; then
echo "parent process resumed"
else
echo "child process pid is $pid"
exec ls -l
fi
It is not working. Does linux bash does not support fork
like this?
Updated: Editing the output
I am parent process
my pid is 61978
new_child_process: line 4: syntax error near unexpected token `('
new_child_process: line 4: `pid=fork()'
Upvotes: 1
Views: 9108
Reputation: 2463
fork
is not directly available in bash, but it can be effectively simulated with &
.
A couple of other things about this code:
To do a numeric comparison you should use -eq
not eq
(note the extra dash -
).
It is also considered a best practice to use [[ ]]
for tests instead of [ ]
.
Upvotes: 4