You are not logged in.
Can anyone explain this? Why is the variable only saved if it is done on two different lines? And how can I save the variable and echo it in the same line??
[karam@vladimir ~]$ x="hi" | echo $x
[karam@vladimir ~]$ x="hi"
[karam@vladimir ~]$ echo $x
hiI apologise in advance for being a bit of a noob; I'm sure the issue is something simple.
Yellowcot: A free, lightweight, open-source, cross-platform, Qt-based multimedia flash card simulator.
Offline
Try this:
x="hi" && echo $xYou piped no output to echo in your first example.
Last edited by skottish (2008-03-15 04:14:10)
Offline
It works fine for me with zsh, I'm assuming you are using bash. Your problem could be related to your use of a pipe. You probably want two ampersands (&&).
Last edited by valnour (2008-03-15 04:14:21)
Happy Hacking!
Twitter: http://twitter.com/bobbyrburden/
Website: http://codebutcher.com/
Offline
This command
x="hi" | echo $xcauses the shell to start two different processes and then hook them together via a pipe. They are otherwise unrelated, so changes to the environment of the first (which is what x="hi" is doing) does not affect the environment of the second. In contrast, this command:
x="hi" && echo $xcauses the shell to run the first command and then run the second if and only if the first succeeded, but in the same process, and therefore the same environment.
Offline
You could also separate both commands with a ;. Whichever works for you...
Offline
Wow! Thanks for the help. I am using the && and it works for me now
One more question, I actually need an if-then statement on one line... but this is not working...
x = "hi" && if [ ! -z "${x:16}" ] then echo "${x::16}..." else echo $xDoes anyone know how to correctly do it?
Yellowcot: A free, lightweight, open-source, cross-platform, Qt-based multimedia flash card simulator.
Offline
you're missing a semi-colon after the test and forgot to end the if statement with fi
x = "hi" && if [ ! -z "${x:16}" ] ;then echo "${x::16}..." else echo $x ;fi
Offline
Hrm... I must be missing something else too...
[karam@vladimir ~]$ x="hi" && if [ ! -z "${x:16}" ] ;then echo "${x::16}..." else echo $x ;fi
[karam@vladimir ~]$Shouldn't it output "hi"?
Yellowcot: A free, lightweight, open-source, cross-platform, Qt-based multimedia flash card simulator.
Offline
[marc@~] x="hi" && if [ ! -z "${x:16}" ]; then echo "${x::16}"; else echo "$x"; fi
hi
[marc@~] x="supercalafragilistic" && if [ ! -z "${x:16}" ]; then echo "${x::16}"; else echo "$x"; fi
supercalafragili
[marc@~]Offline