Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store value returned by nproc in linux shell?

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

like image 382
Kyanite Avatar asked Feb 12 '14 16:02

Kyanite


People also ask

What is nproc Linux?

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.

What does $$ do in Shell?

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.

What does the variable $? Hold?

$? 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.


1 Answers

Just use nested call to nproc:

$ NPROC=$(nproc)
echo $NPROC
1
like image 87
Малъ Скрылевъ Avatar answered Nov 09 '22 21:11

Малъ Скрылевъ