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

CrazedNerd

Well-Known Member
Joined
Mar 31, 2021
Messages
1,144
Reaction score
523
Credits
9,864
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,674
Reaction score
2,427
Credits
13,194
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
1,144
Reaction score
523
Credits
9,864
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,677
Reaction score
1,296
Credits
11,063
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
1,144
Reaction score
523
Credits
9,864
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,677
Reaction score
1,296
Credits
11,063
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
1,144
Reaction score
523
Credits
9,864
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
1,144
Reaction score
523
Credits
9,864
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...
 
MALIBAL Linux Laptops

Linux Laptops Custom Built for You
MALIBAL is an innovative computer manufacturer that produces high-performance, custom laptops for Linux.

For more info, visit: https://www.malibal.com

Members online


Latest posts

Top