Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I divide the output of a command by two, and store the result into a bash variable?

Tags:

bash

shell

unix

Say if i wanted to do this command:

(cat file | wc -l)/2

and store it in a variable such as middle, how would i do it?

I know its simply not the case of

$middle=$(cat file | wc -l)/2

so how would i do it?

like image 814
KP65 Avatar asked Feb 22 '10 21:02

KP65


People also ask

How do you output a command to a variable?

To store the output of a command in a variable, you can use the shell command substitution feature in the forms below: variable_name=$(command) variable_name=$(command [option ...] arg1 arg2 ...) OR variable_name='command' variable_name='command [option ...]


2 Answers

middle=$((`wc -l < file` / 2))
like image 146
blahdiblah Avatar answered Sep 27 '22 21:09

blahdiblah


middle=$((`wc -l file | awk '{print $1}'`/2))
like image 35
Steve Emmerson Avatar answered Sep 27 '22 20:09

Steve Emmerson