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,980 points
2,552 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,890 points

Related Questions In Linux Administration

0 votes
0 answers

Linux Command for getting a dump of hex values bytes from Pcap File

I require a Linux command to display ...READ MORE

Apr 13, 2022 in Linux Administration by Rahul
• 9,680 points
974 views
0 votes
0 answers

Linux Command for getting a dump of hex values bytes from Pcap File

I require a linux command to display ...READ MORE

Apr 20, 2022 in Linux Administration by Edureka
• 13,690 points
636 views
0 votes
0 answers

How can I convert a series of images to a PDF from the command line on linux?

I have a scanning server I wrote ...READ MORE

Apr 21, 2022 in Linux Administration by Edureka
• 13,690 points
620 views
0 votes
1 answer

Windows system, after heroku login from git bash, there is no command line show

You can login via Windows command prompt ...READ MORE

answered May 27, 2022 in Linux Administration by Korak
• 5,820 points
1,605 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 Oct 20, 2020 in Linux Administration by MD
• 95,460 points
1,080 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 Oct 20, 2020 in Linux Administration by MD
• 95,460 points
1,096 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 Oct 20, 2020 in Linux Administration by MD
• 95,460 points
26,654 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,890 points
1,359 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,890 points
654 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP