Find the answer to your Linux question:
Results 1 to 8 of 8
How to store the result of a command executed into a variable. Ok what exactly I want to do is to store the value of tty into a variable and ...
  1. #1
    Linux User vickey_20's Avatar
    Join Date
    Mar 2009
    Location
    Mumbai, India
    Posts
    493

    how so save the cmd results in variable

    How to store the result of a command executed into a variable.
    Ok what exactly I want to do is to store the value of tty into a variable and using substitute that variable in the PS1 so as to see every time in which terminal I m working. The code that I worked up is as below.
    Code:
    tty='tty'
    terminal=$(basename($tty))
    PS1=" TTY:$terminal #and other things
    Only if I could understand the man pages
    Registered Linux user #492640
    OS: RHEL4,5 ,RH 9,Ubuntu

  2. #2
    Linux Guru
    Join Date
    Nov 2007
    Location
    Córdoba (Spain)
    Posts
    1,513
    I guess you meant this:

    Code:
    foo=$(basename $(tty))
    PS1="${foo} <whatever else>"

  3. #3
    Linux User vickey_20's Avatar
    Join Date
    Mar 2009
    Location
    Mumbai, India
    Posts
    493
    no its not working that way
    Only if I could understand the man pages
    Registered Linux user #492640
    OS: RHEL4,5 ,RH 9,Ubuntu

  4. #4
    Linux Guru
    Join Date
    Nov 2007
    Location
    Córdoba (Spain)
    Posts
    1,513
    Are you using bash at all?

    If negative try backticks instead, cause I think that the $() notation is a bashism.

    Code:
    foo=`tty`
    foo=`basename $tty`
    PS1="$foo whatever else"
    If not, try to be more descriptive. A simple "doesn't work" tells me nothing about what the problem is.

  5. #5
    Linux User vickey_20's Avatar
    Join Date
    Mar 2009
    Location
    Mumbai, India
    Posts
    493
    Quote Originally Posted by i92guboj View Post
    I guess you meant this:

    Code:
    foo=$(basename $(tty))
    PS1="${foo} <whatever else>"
    dude I did some tweaks in this code and it got me working.
    What I did
    Code:
    foo=$(basename $(tty))
    PS1="$foo <whatever else>"
    But I m still confused where to use $() and where only $varname. Thanks for replying you helped me solve my problem.
    Only if I could understand the man pages
    Registered Linux user #492640
    OS: RHEL4,5 ,RH 9,Ubuntu

  6. #6
    Linux Guru
    Join Date
    Nov 2007
    Location
    Córdoba (Spain)
    Posts
    1,513
    $() or `...` are used when you want to expand the contents to the result of a command.

    For example, when you do var=$(ls) or var=`ls` var will hold the output of ls. Note that backticks are not the same than simple quotes.

    If you use var='ls', var will contain the string "ls", and not the output of the ls command.

  7. #7
    Linux User vickey_20's Avatar
    Join Date
    Mar 2009
    Location
    Mumbai, India
    Posts
    493
    and there is no such thing like $(var) but only $val right. Correct me if I m wrong. Thanks you cleared my concepts
    Only if I could understand the man pages
    Registered Linux user #492640
    OS: RHEL4,5 ,RH 9,Ubuntu

  8. #8
    Linux Guru
    Join Date
    Nov 2007
    Location
    Córdoba (Spain)
    Posts
    1,513
    Quote Originally Posted by vickey_20 View Post
    and there is no such thing like $(var) but only $val right. Correct me if I m wrong. Thanks you cleared my concepts
    Yep.

    To set a variable you do this:

    Code:
    var="whatever"
    To reference it you use this:

    Code:
    echo "$var"
    #or something similar depending on the concrete case,
    # for example if $var holds the name of a directory
    # you could do
    ls "$var"
    In cases where there's something adjacent to the variable you can do this to avoid confussion:

    Code:
    var="foo"
    echo "${foo}bar"
    Curly braces can be used that way to delimit the name of a variable from the surrounding text. For the rest, $var and ${var} are equivalent.

    Indeed, there's no $(var) on the sense that you mean it. It's rather var=$(code) or var=`code`

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...