Can we assign a variable for a commad

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vasu1308
    New Member
    • Feb 2007
    • 31

    Can we assign a variable for a commad

    Hi,

    Can i assign a variable..Here is my sample code:
    $a = who grep $LOGNAME
    $b = pwd
    Is this right or wrong.
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    Originally posted by vasu1308
    Hi,

    Can i assign a variable..Here is my sample code:
    $a = who grep $LOGNAME
    $b = pwd
    Is this right or wrong.
    try
    Code:
    a=ls
    b=pwd
    $a
    $b

    Comment

    • ghostdog74
      Recognized Expert Contributor
      • Apr 2006
      • 511

      #3
      Originally posted by vasu1308
      Hi,

      Can i assign a variable..Here is my sample code:
      $a = who grep $LOGNAME
      $b = pwd
      Is this right or wrong.
      not quite. use backticks
      Code:
      $a = `who |grep $LOGNAME`
      $b = `pwd`

      Comment

      • horace1
        Recognized Expert Top Contributor
        • Nov 2006
        • 1510

        #4
        Originally posted by ghostdog74
        not quite. use backticks
        Code:
        $a = `who |grep $LOGNAME`
        $b = `pwd`
        which shell are you using?

        mine
        Code:
        a=ls
        b=pwd
        $a
        $b
        works using bash but not csh

        Comment

        • ghostdog74
          Recognized Expert Contributor
          • Apr 2006
          • 511

          #5
          Originally posted by horace1
          which shell are you using?

          mine
          Code:
          a=ls
          b=pwd
          $a
          $b
          works using bash but not csh
          bash. and should be like this instead
          Code:
          a=`who |grep $LOGNAME`
          b=`pwd`

          Comment

          • horace1
            Recognized Expert Top Contributor
            • Nov 2006
            • 1510

            #6
            on my linux PC
            Code:
            a=ls
            b=pwd
            $a
            $b
            works with bash and
            Code:
            set a=ls
            set b=pwd
            $a
            $b
            works with csh

            Comment

            • ghostdog74
              Recognized Expert Contributor
              • Apr 2006
              • 511

              #7
              Originally posted by horace1
              on my linux PC
              Code:
              a=ls
              b=pwd
              $a
              $b
              I had a different interpretation of what the OP needs.
              I assume he wants to store the results of the who|grep and 'pwd' command to variables 'a' and 'b'. so if that's the case, the above is not correct.
              But if OP just wants to store the word "ls" in 'a' and the word "pwd" in b, then its correct.

              Comment

              • horace1
                Recognized Expert Top Contributor
                • Nov 2006
                • 1510

                #8
                Originally posted by ghostdog74
                I had a different interpretation of what the OP needs.
                I assume he wants to store the results of the who|grep and 'pwd' command to variables 'a' and 'b'. so if that's the case, the above is not correct.
                But if OP just wants to store the word "ls" in 'a' and the word "pwd" in b, then its correct.
                I think that vasu1308 needs to tell us exactly what is required?

                Comment

                Working...