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

C

CrazedNerd

Guest
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'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?!
 
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
 
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.
 
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 by a moderator:
You could also do something like this.
This page has 2 examples (right way, wrong way)
 
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.
 
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...
 

Members online


Top