1

Perl is excellent language to store command output to variable As I know bash isn’t support that

for example

my $value = qx(ls -ltr);
print $value;

it will print: ( example )

 -r-xr-xr-x 1 root     root          761 May 30  2014 ASon.conf
 -r-xr-xr-x 1 root     root          699 May 30  2014 ascpl
 drwxr-xr-x 2 root     root         4096 Feb 24 13:22 da
 drwx------ 2 root     root         4096 Feb 25 08:08 vm-root
 drwxr-x--- 2 root     root         4096 Feb 25 08:10 hspeoot
 -rw-r--r-- 1 root     root         2451 Feb 25 16:12 gry.txt
 -rw-r--r-- 1 root     root         3112 Mar  1 09:49 new.xml
 -rw-r--r-- 1 root     root        44893 Mar  1 12:13 SMports.xml
 -rwxr-xr-x 1 root     root          101 Mar  2 09:56 test
 -rwxr-xr-x 1 root     root    461340404 Mar  2 10:20 audin.log

the problem is that I write my code with bash and I want to use the Perl add value in my bash script

so is it possible to combine the following line in my bash script?

  my $value = qx(ls -ltr);

in way that when I print the value variable it will print the ls -ltr output

4
  • echo $value total 451052 -r-xr-xr-x 1 root root 761 May 30 2014 ASCBMon.conf -r-xr-xr-x 1 root root 699 May 30 2014 ascbmon.pl drwxr-xr-x 2 roog ( this print one long line ) not as I want , and cat $value not illegale Commented Mar 2, 2015 at 9:33
  • 3
    You need to put double quotes around "$value" when you echo it, otherwise the white space will be lost. Commented Mar 2, 2015 at 9:40
  • WOW its work - write your answer - so I will can vote and select your answer, Commented Mar 2, 2015 at 9:43
  • I'd suggest not mixing and matching between two scripting languages unless there's a particularly compelling need. That's a road to confusion and maintenance headaches. Commented Mar 2, 2015 at 10:43

2 Answers 2

4

You don't need perl for this, your shell can handle it:

value=$(ls -ltr)
echo "$value"

The quotes when you echo are important.

Sign up to request clarification or add additional context in comments.

Comments

2

In Perl:

$var = `shell command`;
$var = qx( shell command );

In sh:

var=`shell command`
var=$( shell command )

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.