This question already has answers here:
How to get process ID of background process?
(9 answers)
Closed 3 years ago.
I'm trying to capture the pid of a background process run in bash.
I tried
somecommand & > somecommand.pid
It outputs the pid to the screen but it's not part of stdout of the command.
[1] 1778
The special variable $! contains the PID of the most recent job placed in the background:
$ sleep 4 &
$ sleep_pid=$!
$ echo $sleep_pid
4456
This is documented in Special Parameters of man bash
Related
This question already has answers here:
Does $! mean something in shell scripting
(3 answers)
Closed 4 years ago.
I've been working on Shell and I've seen something like :
pid_A2=$!
wait $pid_A2
pid_A2=$?
Would you please explain the difference between the two syntax "$!" and "$?"...
Actually, i know that $? is the exit status of the previous command but I've never seen the previous one.
$?: the status of the last process execution
$!: the pid of the last command in background
$! is the last job of the background process. For example:
$ sleep 1000 &
[1] 6646 ---> process id
echo "$!" will print the process id of the last command (here, 6646).
$? returns the exit value of the command which is recently executed. $? is used when we want to handle the return value of a command or function. For example:
if [ **$?** -eq 1 ];
then
# do something
fi
This question already has answers here:
What is indirect expansion? What does ${!var*} mean?
(6 answers)
How to use a variable's value as another variable's name in bash [duplicate]
(6 answers)
Closed 5 years ago.
In the UNIX terminal, when I write the following commands:
$ a=b
$ c=a
$ echo $$c
I expected the output to be b, since the value of c is a and the value of a is b.
But , instead the output I received was: 2861c.
Can someone tell me the reason behind this output?
echo $$c prints your terminal PID and letter 'c' after it. You can verify it by 'ps aux | grep bash'.
From http://www.tldp.org/LDP/abs/html/internalvariables.html
$$ gives PID of the current running shell instance.
bash4$ echo $$ 11015
bash4$ echo $BASHPID 11015
The first $ sign captures the next character and prints the value.
For your case, a double substitution is the best option.
echo ${!c}
or you may go for
eval echo \$$c
$$ is special variable for BASH and used to print the process id of execution of current script. So $$c prints process id followed by c letter
If you still want to archive Indirect reference to variable,
a=b
c=a
echo ${!c} #will print "b" on console
This question already has answers here:
$$ in a script vs $$ in a subshell
(4 answers)
Closed 7 years ago.
In my shell scripts,I wrote those:
#! /bin/bash
(ls;echo$$)
echo$$
then I run the shell,the two output is the same
2592
2592
why subshell didn't have a new process id?
$$ is the process ID of the main shell. To get the process ID of a subshell, use BASHPID:
$ echo $$ $BASHPID; ( echo $$ $BASHPID; )
19610 19610
19610 21937
The subshell has PID 21937.
Documentation
From man bash:
BASHPID
Expands to the process ID of the current bash process. This differs from $$ under certain circumstances, such as sub‐
shells that do not require bash to be re-initialized.
By contrast, $$ is documented as follows:
$$
Expands to the process ID of the shell. In a () subshell, it expands
to the process ID of the current shell, not the sub‐ shell.
This question already has answers here:
How do you catch error codes in a shell pipe?
(5 answers)
Closed 9 years ago.
Here's an example of what I'm trying to achieve:
#!/bin/bash
set -e # abort if error
...
command1 2>&1 | command2
...
And I notice that sometimes command1 fails but command2 does not and the shell script happily continues...
if I did not have to use the pipe here, the set -e would have been sufficient but now it does not work with the pipe there...
Any thoughts?
Thanks
Since you are using bash, other than set -e you can also add set -o pipefail to get the results you want...
This question already has answers here:
Bash script - store stderr in a variable [duplicate]
(4 answers)
Closed 6 years ago.
Bash how do you capture stderr to a variable?
I would like to do something like this inside of my bash script
sh -c path/myExcecutable-bin 2>&1 =MYVARIABLE
How do you send stderror output to a variable ?
To save both stdout and stderr to a variable:
MYVARIABLE="$(path/myExcecutable-bin 2>&1)"
Note that this interleaves stdout and stderr into the same variable.
To save just stderr to a variable:
MYVARIABLE="$(path/myExcecutable-bin 2>&1 > /dev/null)"