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 in Linux Administration by Damon Salvatore
• 5,430 points
60 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
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 by DareDev
• 6,670 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 in Linux Administration by Omkar
• 66,880 points
20 views
0 votes
1 answer

how to loop through the content of a file using bash

One of the ways to do it ...READ MORE

answered Feb 15 in Linux Administration by ajs3033
• 7,080 points
48 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 in Linux Administration by ajs3033
• 7,080 points
24 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 in Linux Administration by Shubham
• 12,790 points
14 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

How do I set variable if a specific package version is installed in CFEngine?

Here is what you can do.Just use packagesmatching to ...READ MORE

answered Jul 12, 2018 in Other DevOps Questions by Atul
• 10,220 points
58 views
+5 votes
17 answers

What are job opportunities as a Linux System Administrator?

I just started working professionally as a ...READ MORE

answered Jul 18, 2018 in Career Counselling by eatcodesleeprepeat
• 4,670 points
616 views
0 votes
1 answer

Making a program sleep for milliseconds?

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

answered Mar 1 in Linux Administration by DareDev
• 6,670 points
22 views
0 votes
1 answer

Counting lines in a file/doc.

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

answered Mar 5 in Linux Administration by DareDev
• 6,670 points
23 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.