storing 'du' result in a variable [duplicate] - bash

This question already has answers here:
How to return the output of program in a variable?
(4 answers)
Closed 7 years ago.
I am using Putty with bash-4.2. Therein, I am outputting file size with:
du -m myfile.csv
which returns:
1.25 myfile.csv
How do i store this line in a variable so I can later parse out the filesize?
Thanks in advance

Like so
FOO="$(du -m myfile.csv)"
echo "$FOO"
Output
1.25 myfile.csv

Related

Assign output of command to variable in BASH [duplicate]

This question already has an answer here:
Bash how do you capture stderr to a variable? [duplicate]
(1 answer)
Closed 3 years ago.
I would like to assign output of a specific command to variable (nginx -v). My trouble can be seen at printscreen bellow, it still prints output to stdout.
Thank you for your help
a=$(nginx -v 2>&1)
echo $a
Bash how do you capture stderr to a variable?

save value from wc -l command to variable [duplicate]

This question already has answers here:
How do I set a variable to the output of a command in Bash?
(15 answers)
How to assign the output of a Bash command to a variable? [duplicate]
(5 answers)
How do i store the output of a bash command in a variable? [duplicate]
(2 answers)
Closed 3 years ago.
I'm trying to get the number of lines that was printed from the ps command, and save to a variable.
COUNT_PS= ps -C $NAME | wc -l)
the line above prints me 2, but COUNT_PS unfortunately still equals 0 (at the start of the script I've assigned COUNT_PS as 0).
It actually prints the value - but doesn't save it to COUNT_PS.
What am I doing wrong?

How to name a file with current time? [duplicate]

This question already has answers here:
Redirect output to filename given by result of command
(3 answers)
Closed 4 years ago.
I'm trying to create a file using bash that has the current time as it's name. This is how I'm trying to do it:
echo 'hello' > date +"%T".txt
What am I missing?
Use command substitution to capture date's output as a string.
echo 'hello' > "$(date +%T)".txt

How to search for a pattern in a file and store it in a variable using shell script? Please give an example [duplicate]

This question already has answers here:
How do I set a variable to the output of a command in Bash?
(15 answers)
Bash script store command output into variable
(2 answers)
Save output in variable? [duplicate]
(2 answers)
Store output of sed into a variable [duplicate]
(4 answers)
Assigning the output of a command to a variable [duplicate]
(3 answers)
Closed 4 years ago.
suppose in a file if I have " Route : 8888" , I want to get the string after the pattern "Route :"
I have used sed command, but it is printing 8888 but I want to store it into a variable and use for further processing
So store sed's output in a variable:
ROUTE_VALUE=$(sed ... <file.txt)
Where '...' is your sed command.
$ cat rt.sh
ROUTE=`sed -n 's/.*Route = \(.*\),.*/\1/p' data.txt`
echo $ROUTE
$ bash rt.sh
8888
$ source rt.sh
8888

How to retrieve the first n characters of a variable in Bash? [duplicate]

This question already has answers here:
Extract substring in Bash
(26 answers)
Closed 6 years ago.
How to retrieve the first 10 characters of a variable with Bash?
FOO="qwertzuiopasdfghjklyxcvbnm"
I need to get qwertzuiop.
If the variable is: FOO="qwertzuiopasdfghjklyxcvbnm"
then
echo ${FOO:0:10}
will give the first 10 characters.
Use the head command.
echo $FOO | head -c 10
=> qwertzuiop

Resources