• We did not send an email asking for donations - please read this post.

Is there a way to auto-assign variables with bash?

CrazedNerd

Well-Known Member
Joined
Mar 31, 2021
Messages
774
Reaction score
305
Credits
6,606
I've had a lot of different ideas for making bash scripts, but i keep running into this problem where it seems like i have to assign variables manually in order to insure that there isn't a problem with the output. Would there be a way to take arguments generated with $(<command-output>) and then auto-assign variables with brace expansion or something?
 


JasKinasis

Well-Known Member
Joined
Apr 25, 2017
Messages
1,563
Reaction score
2,224
Credits
11,524
I've had a lot of different ideas for making bash scripts, but i keep running into this problem where it seems like i have to assign variables manually in order to insure that there isn't a problem with the output. Would there be a way to take arguments generated with $(<command-output>) and then auto-assign variables with brace expansion or something?
I don’t understand what you mean by "auto-assign variables with brace expansion". Can you elaborate further? Perhaps with an example?!
 
OP
CrazedNerd

CrazedNerd

Well-Known Member
Joined
Mar 31, 2021
Messages
774
Reaction score
305
Credits
6,606
I don’t understand what you mean by "auto-assign variables with brace expansion". Can you elaborate further? Perhaps with an example?!
I guess what i was asking is confusing b/c you don't really assign variables in bash like with other languages...

So take something like this:
Code:
Var1=<some_value>

So normally i type that manually, but what if i want command substitution to generate variables using its output.

Code:
echo $(find type -f) <fallowed-by-some-mechanism-to-generate-each-result-as-a-new-numbered-variable)

Find there is just an example, the conept is kinda confusing, i think its maybe just a silly idea
 

dos2unix

Well-Known Member
Joined
May 3, 2019
Messages
1,559
Reaction score
1,177
Credits
9,920
myvar=$( output of some command )

#!/bin/bash
myvar=$(ps -ef | grep tty | awk {'print $1'})
echo $myvar

Not sure why you need the variables to be numbered, but that's possible too.
Take a look at enum. Or you you could increment the variable name.
 
OP
CrazedNerd

CrazedNerd

Well-Known Member
Joined
Mar 31, 2021
Messages
774
Reaction score
305
Credits
6,606
myvar=$( output of some command )

#!/bin/bash
myvar=$(ps -ef | grep tty | awk {'print $1'})
echo $myvar

Not sure why you need the variables to be numbered, but that's possible too.
Take a look at enum. Or you you could increment the variable name.
Its my understanding of that mechanism that lead to my question, i was wondering if there was a way to assign the output from the command automatically to a variable, like lets say i wanted to use $var1 to $var1000...i can't make what im talking about more clear than this, i dont think its possible to do this
 
Last edited:

dos2unix

Well-Known Member
Joined
May 3, 2019
Messages
1,559
Reaction score
1,177
Credits
9,920
You could also do something like this.
This page has 2 examples (right way, wrong way)
 
OP
CrazedNerd

CrazedNerd

Well-Known Member
Joined
Mar 31, 2021
Messages
774
Reaction score
305
Credits
6,606
You could also do something like this.
This page has 2 examples (right way, wrong way)
AH! That pretty much is exactly what i had in mind (even though i don't have the time today to try it out...), stack exchange is a very helpful archive of coding and information technology tricks, it's too bad that sometimes its still confusing and weird to navigate...but that's what programming is in a nutshell: error correction, innovation, cryptography.

Of course...i wasn't talking about reading lines from inside of files, but of course the loops on the page could be used for any number of purposes.
 
$100 Digital Ocean Credit
Get a free VM to test out Linux!

Linux.org Hosting Donations
Consider making a donation

Members online


Top