You are not logged in.

#1 2008-03-15 04:07:13

tony5429
Member
Registered: 2006-03-28
Posts: 1,017

Very simple question about linux command line

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
hi

I apologise in advance for being a bit of a noob; I'm sure the issue is something simple.

Offline

#2 2008-03-15 04:13:42

skottish
Forum Fellow
From: Here
Registered: 2006-06-16
Posts: 7,942

Re: Very simple question about linux command line

Try this:

x="hi" && echo $x

You piped no output to echo in your first example.

Last edited by skottish (2008-03-15 04:14:10)

Offline

#3 2008-03-15 04:13:50

valnour
Member
From: Cleveland, TN, USA
Registered: 2008-02-17
Posts: 84
Website

Re: Very simple question about linux command line

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)

Offline

#4 2008-03-15 04:23:38

tam1138
Member
Registered: 2007-09-10
Posts: 238

Re: Very simple question about linux command line

This command

x="hi" | echo $x

causes 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 $x

causes 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

#5 2008-03-15 04:24:14

peart
Member
From: Kanuckistan
Registered: 2003-07-28
Posts: 510

Re: Very simple question about linux command line

You could also separate both commands with a ;.  Whichever works for you...

Offline

#6 2008-03-15 04:32:25

tony5429
Member
Registered: 2006-03-28
Posts: 1,017

Re: Very simple question about linux command line

Wow! Thanks for the help. I am using the && and it works for me now smile 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 $x

Does anyone know how to correctly do it?

Offline

#7 2008-03-15 05:03:46

Snowman
Developer/Forum Fellow
From: Montreal, Canada
Registered: 2004-08-20
Posts: 5,212

Re: Very simple question about linux command line

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

#8 2008-03-15 05:07:47

tony5429
Member
Registered: 2006-03-28
Posts: 1,017

Re: Very simple question about linux command line

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"?

Offline

#9 2008-03-15 06:18:31

peart
Member
From: Kanuckistan
Registered: 2003-07-28
Posts: 510

Re: Very simple question about linux command line

[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

#10 2008-03-15 06:28:27

tony5429
Member
Registered: 2006-03-28
Posts: 1,017

Re: Very simple question about linux command line

Thanks, Peart!!

Offline

Board footer

Powered by FluxBB