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

CrazedNerd

Well-Known Member
Joined
Mar 31, 2021
Messages
962
Reaction score
425
Credits
8,299
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,636
Reaction score
2,345
Credits
12,537
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
962
Reaction score
425
Credits
8,299
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,654
Reaction score
1,270
Credits
10,769
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
962
Reaction score
425
Credits
8,299
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,654
Reaction score
1,270
Credits
10,769
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
962
Reaction score
425
Credits
8,299
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.
 
OP
CrazedNerd

CrazedNerd

Well-Known Member
Joined
Mar 31, 2021
Messages
962
Reaction score
425
Credits
8,299
To provide a clear and simple answer to my question, you can use the commands "readarray" and "mapfile" to generate variables based on lines in a file, they don't have their own man or info pages, but you can search for them in the "bash" manpage...
 
$200 Digital Ocean Credit
Get a free VM to test out Linux!



Top