I'm trying to store the number of CPUs in a variable for a linux bash shell. I know nproc returns the number of CPUs however I can't seem to store the value returned in a variable.
I'm sure it's a simple solution so any help would be appreciated.
Thanks
nproc is a simple Unix command which is used to print the number of processing units available in the system or to the current process. This command could be used in system diagnostics and related purposes. It is part of GNU Core utils, so it comes pre-installed with all modern Linux operating systems.
The $$ variable is the PID (Process IDentifier) of the currently running shell. This can be useful for creating temporary files, such as /tmp/my-script. $$ which is useful if many instances of the script could be run at the same time, and they all need their own temporary files.
$? is a built-in variable that stores the exit status of a command, function, or the script itself. $? reads the exit status of the last command executed.
Just use nested call to nproc:
$ NPROC=$(nproc)
echo $NPROC
1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With