setting a variable output from a bash command

0 votes

This is a script that I'm using:

#!/bin/bash

VAR1="$1"    
MOREF='sudo run command against $VAR1 | grep name | cut -c7-'

echo $MOREF

When I execute it and pass the required arguments, there is no output coming out. But if I execute it within the $MOREF variable, I do get the output. I just want to get the results of the command, save it to a variable and then print it out on the screen.

Feb 15, 2019 in Linux Administration by Damon Salvatore
• 5,550 points
739 views

1 answer to this question.

0 votes

You can use $(command), which in my opinion is easier to read than using backticks, and also allow nesting.

OUTPUT="$(ls -1)"
echo "${OUTPUT}"

MULTILINE=$(ls \
   -1)
echo "${MULTILINE}"

Quoting (") does matter to preserve multi-line values.

answered Feb 15, 2019 by DareDev
• 6,870 points

Related Questions In Linux Administration

0 votes
1 answer

How to take input from user in bash script?

You can use if-else branch to check ...READ MORE

answered Jan 31, 2019 in Linux Administration by Omkar
• 69,030 points
145 views
0 votes
2 answers

how to loop through the content of a file using bash

#!/bin/bash for i in  `cat peptides.txt` do echo $i done READ MORE

answered Sep 4 in Linux Administration by Prakash K. Aithal
779 views
0 votes
1 answer

Downloading file using SSH from a server without web access

Run this command in  your terminal: scp your_username@remotehost.edu:foobar.txt ...READ MORE

answered Mar 5, 2019 in Linux Administration by ajs3033
• 7,280 points
107 views
0 votes
1 answer

Redirecting output to a file and stdout

The command you want is named tee; foo ...READ MORE

answered May 31, 2019 in Linux Administration by Shubham
• 13,450 points
108 views
0 votes
1 answer
0 votes
1 answer

Select sections of text from each line of files in a Bash shell.

Hi@akhtar, There is no inbuild command available for ...READ MORE

answered 4 days ago in Linux Administration by MD
• 65,800 points
17 views
0 votes
1 answer

How to find the first field from a file in Bash Shell?

Hi@akhtar, You can extract text from a file. ...READ MORE

answered 4 days ago in Linux Administration by MD
• 65,800 points
12 views
0 votes
1 answer

How to sort text alphabetically in Linux system?

Hi@akhtar, In the Linux system, you will find ...READ MORE

answered 4 days ago in Linux Administration by MD
• 65,800 points
11 views
0 votes
1 answer

Making a program sleep for milliseconds?

There are no standard C API's that ...READ MORE

answered Mar 1, 2019 in Linux Administration by DareDev
• 6,870 points
110 views
0 votes
1 answer

Counting lines in a file/doc.

you can do this by using word ...READ MORE

answered Mar 5, 2019 in Linux Administration by DareDev
• 6,870 points
83 views