You are not logged in.

#1 2022-01-16 16:21:06

kitalia
Member
Registered: 2022-01-16
Posts: 13

echo multiple variables on same line [SOLVED]

Hello there

in shellscript I have 2 variables let's say $fruit and $color

echo $fruits

outputs:

banana
apple
lampoon

and

echo $color

outputs

yellow
red
purple

I need to output

banana yellow
apple red
lampoon purple

instead of

banana
apple
lampoon
yellow
red
purple

Thanks

Last edited by kitalia (2022-01-16 17:33:59)

Offline

#2 2022-01-16 16:36:37

Scimmia
Fellow
Registered: 2012-09-01
Posts: 13,729

Re: echo multiple variables on same line [SOLVED]

shellscript as in POSIX sh only? If you're using bash, I would put them into an array and address them individually. Otherwise, you can make it happen with sed.

Offline

#3 2022-01-16 16:38:01

kitalia
Member
Registered: 2022-01-16
Posts: 13

Re: echo multiple variables on same line [SOLVED]

yep posix, how can I use sed for that?

Offline

#4 2022-01-16 16:46:25

Scimmia
Fellow
Registered: 2012-09-01
Posts: 13,729

Re: echo multiple variables on same line [SOLVED]

Something like `echo "$fruits" | sed -n 1p` will print the first line of the given variable. Not hard to build things from there.

Last edited by Scimmia (2022-01-16 16:46:52)

Offline

#5 2022-01-16 16:51:22

kitalia
Member
Registered: 2022-01-16
Posts: 13

Re: echo multiple variables on same line [SOLVED]

that outputs only the first line....if I knew how to proceed from there I wouldn't ask for help....I could have just googled 'how to print only first line shellscript" ....

Last edited by kitalia (2022-01-16 16:52:57)

Offline

#6 2022-01-16 16:52:23

Scimmia
Fellow
Registered: 2012-09-01
Posts: 13,729

Re: echo multiple variables on same line [SOLVED]

You can't build it from there?

What are you actually doing, anyway?

Offline

#7 2022-01-16 16:53:35

kitalia
Member
Registered: 2022-01-16
Posts: 13

Re: echo multiple variables on same line [SOLVED]

I thought it was clear what my end goal is

Offline

#8 2022-01-16 16:54:28

Scimmia
Fellow
Registered: 2012-09-01
Posts: 13,729

Re: echo multiple variables on same line [SOLVED]

Not really. If you can't build it from there, it sounds like you have no idea about shell scripting at all, making me think this is homework.

Offline

#9 2022-01-16 16:56:40

kitalia
Member
Registered: 2022-01-16
Posts: 13

Re: echo multiple variables on same line [SOLVED]

I need to output

banana yellow
apple red
lampoon purple


and I'm 33, 100% self thaught and asking for in the NEWBIE section, this explains why I do not know how to make very simple things that people with regular education in IT might think as stupid easy but even if it was homework what's the point? I need any specific skill level or certificate to ask a newbie question?

you need any more personal info? Maybe my ssn

obviously this is mock up data, i do not need to sort fruits/colors :-) this is part of a ~300 lines shellscript

Last edited by kitalia (2022-01-16 17:10:11)

Offline

#10 2022-01-16 17:11:23

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,492
Website

Re: echo multiple variables on same line [SOLVED]

I'd like to know where the content of these "variables" are coming from, because this is clearly an X-Y problem.  I say this is clear, as the commands and output you provided are not real: you've provided them as a fictional example as they could not generate that output as written.  Shell expansion of the variable passed to echo will result in a collapsing of whitespace (including newlines); so the only way you'd even get the output you are currently getting is by making up the examples and fictional output rather than it being something you actually ran.

This is not nitpicking as it is most likely relevant.  If these "variables" are actually the results of some other process, you'd probably want to use the command `paste`.  So what are you really trying to do?

kitalia wrote:

... even if it was homework what's the point?

The point is, we will not do people's homework for them.  It is a violation of our rules as it undermines the goals of educational programs.

kitalia wrote:

obviously this is mock up data, i do not need to sort fruits/colors

Ah, yes, as I already noted above: obviously it is.  But is it so "mocked up" as to be meaningless.  Tell us what you really need to do.

Last edited by Trilby (2022-01-16 17:15:47)


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#11 2022-01-16 17:13:20

kitalia
Member
Registered: 2022-01-16
Posts: 13

Re: echo multiple variables on same line [SOLVED]

yep this is mock up data and it's the result of other processes

how can I do it with paste? never used it


I'm using curl and sed until I get the desired data, queries are determined by user input read

Last edited by kitalia (2022-01-16 17:15:43)

Offline

#12 2022-01-16 17:14:31

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,492
Website

Re: echo multiple variables on same line [SOLVED]

RTFM.

Saying you're using curl and sed doesn't mean much.  Is this top secret, or some proprietary 300 line shell script?  We really don't care what flavor of porn your script is downloading - but if you will not share actual code, it's not likely that you will be able to get helpful answers.

Last edited by Trilby (2022-01-16 17:19:48)


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#13 2022-01-16 17:29:05

seth
Member
From: Won't reply 2 private help req
Registered: 2012-09-03
Posts: 77,096

Re: echo multiple variables on same line [SOLVED]

Also (because I don't trust you t know the difference between posix and bash and it becomes ABYSMALLY simpler w/ the latter):

declare -p fruits

Offline

#14 2022-01-16 17:31:04

kitalia
Member
Registered: 2022-01-16
Posts: 13

Re: echo multiple variables on same line [SOLVED]

to whomever will stumble upon the same issue, here's the super secret answer the elitist arch community won't provide to newbies:

paste <(echo "$fruit") <(echo "$color")

omg do you understand that a working example will be indexed and people with the same question will find it?

Trilby wrote:

RTFM.

Saying you're using curl and sed doesn't mean much.  Is this top secret, or some proprietary 300 line shell script?  We really don't care what flavor of porn your script is downloading - but if you will not share actual code, it's not likely that you will be able to get helpful answers.


I might have a 1000 different reasons not to share why or how the script works (which is open source FYI), you have 0 reasons to be condescendent about it

also the manual didn't say much, found the answer on stack overflow.

And this ladies and gents is why people say the Arch community is toxic.

Last edited by kitalia (2022-01-16 17:47:25)

Offline

#15 2022-01-16 17:32:32

kitalia
Member
Registered: 2022-01-16
Posts: 13

Re: echo multiple variables on same line [SOLVED]

seth wrote:

Also (because I don't trust you t know the difference between posix and bash and it becomes ABYSMALLY simpler w/ the latter):

declare -p fruits

Thank you, I thaught myself every single thing I know about IT, I don't know shit, thank you.

Offline

#16 2022-01-16 17:48:10

Scimmia
Fellow
Registered: 2012-09-01
Posts: 13,729

Re: echo multiple variables on same line [SOLVED]

kitalia wrote:

to whomever will stumble upon the same issue, here's the super secret answer the elitist arch community won't provide to newbies:

paste <(echo "$fruit") <(echo "$color")

That's process substitution, and is not defined by POSIX, so it doesn't meet your requirements.

Offline

#17 2022-01-16 17:52:02

kitalia
Member
Registered: 2022-01-16
Posts: 13

Re: echo multiple variables on same line [SOLVED]

Scimmia wrote:
kitalia wrote:

to whomever will stumble upon the same issue, here's the super secret answer the elitist arch community won't provide to newbies:

paste <(echo "$fruit") <(echo "$color")

That's process substitution, and is not defined by POSIX, so it doesn't meet your requirements.

If you have a better solution that accomplishes the same result please feel free to share, thank you

Last edited by kitalia (2022-01-16 17:53:02)

Offline

#18 2022-01-16 17:57:00

seth
Member
From: Won't reply 2 private help req
Registered: 2012-09-03
Posts: 77,096

Re: echo multiple variables on same line [SOLVED]

Posix heredocs, but that's a complete pita and also why the very first thing you were asked was whether this is bash or posix, which you falsely answered to be posix.

Offline

#19 2022-01-16 17:59:02

kitalia
Member
Registered: 2022-01-16
Posts: 13

Re: echo multiple variables on same line [SOLVED]

seth wrote:

Posix heredocs, but that's a complete pita and also why the very first thing you were asked was whether this is bash or posix, which you falsely answered to be posix.

thank you but this doesn't really solve the issue, if you too have a solution please feel free to share!

Offline

#20 2022-01-16 18:00:19

Scimmia
Fellow
Registered: 2012-09-01
Posts: 13,729

Re: echo multiple variables on same line [SOLVED]

Yes, please only post if you can give them something they can copy and paste, as it seems like that's the only thing they're capable of here.

Offline

#21 2022-01-16 18:01:09

seth
Member
From: Won't reply 2 private help req
Registered: 2012-09-03
Posts: 77,096

Re: echo multiple variables on same line [SOLVED]

The issue is that we're still unclear about your requirements and that "we" includes "you"

Last edited by seth (2022-01-16 18:01:36)

Offline

#22 2022-01-16 18:07:00

kitalia
Member
Registered: 2022-01-16
Posts: 13

Re: echo multiple variables on same line [SOLVED]

Scimmia wrote:

Yes, please only post if you can give them something they can copy and paste, as it seems like that's the only thing they're capable of here.

Yes thank you, you are super special and knowing the inner workings of everything puts you on top of me as human being!

Offline

#23 2022-01-16 18:08:28

kitalia
Member
Registered: 2022-01-16
Posts: 13

Re: echo multiple variables on same line [SOLVED]

seth wrote:

The issue is that we're still unclear about your requirements and that "we" includes "you"

I'd like to achieve the same result as

paste <(echo "$fruit") <(echo "$color")

in a posix compliant way do you know how to?

Offline

#24 2022-01-16 18:10:39

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,492
Website

Re: echo multiple variables on same line [SOLVED]

kitalia wrote:

to whomever will stumble upon the same issue, here's the super secret answer the elitist arch community won't provide to newbies:

It's so fucking secret that it's precisely what I told you you'd probably want to use in my first reply.

You're selfish, rude, ungrateful, and abusive to the people who you ask for help.  Think whatever the hell you want of this community, as long as you leave it: you are not welcome here.


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#25 2022-01-16 18:10:57

seth
Member
From: Won't reply 2 private help req
Registered: 2012-09-03
Posts: 77,096

Re: echo multiple variables on same line [SOLVED]

Echo the variables into temporary files?
Again, posix heredoc is going to be a complete pita

Offline

Board footer

Powered by FluxBB