Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash Script store cat output in variable and then echo it [duplicate]

Tags:

bash

unix

I am trying to store a cat output into a variable and then trying to echo it. and then I would like to kill the process.

#!/bin/bash

var = $(cat tmp/pids/unicorn.pid)

echo $var
sudo kill -QUIT $var

Please if anyone can tell where I am going wrong

like image 448
Sumeet Masih Avatar asked Oct 22 '16 13:10

Sumeet Masih


People also ask

How do you store output of a shell script in 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 ...] arg1 arg2 ...'

How does cat << EOF work in bash?

The EOF operator is used in many programming languages. This operator stands for the end of the file. This means that wherever a compiler or an interpreter encounters this operator, it will receive an indication that the file it was reading has ended.

What is cat << in Linux?

Cat is short for concatenate. This command displays the contents of one or more files without having to open the file for editing. In this article, learn how to use the cat command in Linux.


1 Answers

Variable assignments in bash should not have any spaces before or after the equal sign. It should be like this:

#!/bin/bash
var=$(cat tmp/pids/unicorn.pid)
echo "$var"

Which can be written more idiomatically as

#!/bin/bash
var=$(< tmp/pids/unicorn.pid)
echo "$var"
like image 175
user000001 Avatar answered Sep 28 '22 12:09

user000001