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 ...
- 05-24-2009 #1
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
- 05-24-2009 #2Linux 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>"
- 05-24-2009 #3
no its not working that way
Only if I could understand the man pages
Registered Linux user #492640
OS: RHEL4,5 ,RH 9,Ubuntu
- 05-24-2009 #4Linux 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.
If not, try to be more descriptive. A simple "doesn't work" tells me nothing about what the problem is.Code:foo=`tty` foo=`basename $tty` PS1="$foo whatever else"
- 05-24-2009 #5Only if I could understand the man pages

Registered Linux user #492640
OS: RHEL4,5 ,RH 9,Ubuntu
- 05-24-2009 #6Linux 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.
- 05-24-2009 #7
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
- 05-24-2009 #8Linux Guru
- Join Date
- Nov 2007
- Location
- Córdoba (Spain)
- Posts
- 1,513
Yep.
To set a variable you do this:
To reference it you use this:Code:var="whatever"
In cases where there's something adjacent to the variable you can do this to avoid confussion: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"
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.Code:var="foo" echo "${foo}bar"
Indeed, there's no $(var) on the sense that you mean it. It's rather var=$(code) or var=`code`


Reply With Quote
