UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

#1 2007-02-21 06:27 AM

usharegadi
Guest

Re: redirect output of a command to a variable in a shell script

can we redirect output of a command to a variable in a shell script

   the following are the commands used

   set var1 = " "
   basename  test.tif.gz .gz  >   var1


  or the other way

   set the variable from the file
   basename test.tif.gz  .gz > outputfile
    set var1 = < outputfile

#2 2007-02-21 10:01 AM

biologz
Administrator
From: Puking on the pavement
Registered: 2005-11-02
Posts: 393

Re: redirect output of a command to a variable in a shell script

hi,

In your shell just do this (i'm using bash):

[ascotto@localhost ~]$ test=`uname`
[ascotto@localhost ~]$ echo $test
Linux

explanations: "test" is the variable and using the `` quotes (sorry i don't remember the name) just do exactly what you want, it executes the command.`` replace the command with the result of the command.

Sorry my english sucks for such explanations... i feel like a 5 years old boy trying to explain something...

With bash you have other way to do that, search for "command substitution" in the manpages.
But you can do this:

test=$(command)

Each shell has its own features so just look into specific manpages. Anyway the first method i showed you may work almost everywhere (except for csh based shells that i don't use).

Remember one thing, like RobSeace said: "CSH-based shells are the spawn of satan" :-)


gethostbyintuition() is still a dream of mine

                                                 -- quoted from bash

Offline

#3 2007-02-21 02:06 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,765
Website

Re: redirect output of a command to a variable in a shell script

Yes, that they are... ;-)

Offline

#4 2010-10-26 03:05 PM

Dave
Guest

Re: redirect output of a command to a variable in a shell script

backtick

Board footer

Powered by FluxBB