Choose random string from a few and set it as a variable?Test variable if its string or notIn BASH is there a...

UX writing: When to use "we"?

Applying for mortgage when living together but only one will be on the mortgage

Can birds evolve without trees?

Why are Star Wars Rebel Alliance ships named after letters from the Latin alphabet?

Why do my fried eggs start browning very fast?

Word to describe someone doing something even though told not to

How to power down external drive safely

Does KNN have a loss function?

δόλος = deceit in John 1:47

How did Biff return to 2015 from 1955 without a lightning strike?

Basic theorem proving in Mathematica?

Accurately recalling the key - can everyone do it?

Plotting Chebyshev polynomials using PolarPlot and FilledCurve

Matrix condition number and reordering

Move label of an angle in Tikz

Can I shorten this filter, that finds disk sizes over 100G?

How to get maximum number that newcount can hold?

Why do player start with fighting for the corners in go?

"Will flex for food". What does this phrase mean?

Skipping same old introductions

What's the term for a group of people who enjoy literary works?

What do the screens say after you are set free?

Can I say "Gesundheit" if someone is coughing?

A wiild aanimal, a cardinal direction, or a place by the water



Choose random string from a few and set it as a variable?


Test variable if its string or notIn BASH is there a way to read Variable names from a variable?Local variable as a part of global onempack description (-d) from a script variableCan I introspect the type of a bash variable?How can I use the expanded value of a shell variable in the name of another variable?Makefile: Default Value of Variable that is set but has null valueBash: Set variable equal to a string substitutionHow do I specify chance when setting a variable to random item in array?Evaluate command based on variable at bash






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







0















I have a few strings, and I want to set a variable to one of them, randomly. Say the strings are test001, test002, test003 and test004.



If I set it like normal I'd obviously do it like this:



test=test001


But I want it to choose a random one from the for strings I have. I know I could do something like this, which I have done previously, but that was when choosing a random file from a directory:



test="$(printf "%sn" "${teststrings[RANDOM % ${#teststrings[@]}]}")"


But in this case I am not sure how to set testrings.










share|improve this question

















bumped to the homepage by Community 36 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.

















  • set the teststrings array like this: teststrings=( test001 test002 test003 test004 ), then your code will work. To save running in a subshell, printf -v test "%sn" "${teststrings[RANDOM % ${#teststrings[@]}]}"

    – glenn jackman
    Nov 4 '15 at 15:01




















0















I have a few strings, and I want to set a variable to one of them, randomly. Say the strings are test001, test002, test003 and test004.



If I set it like normal I'd obviously do it like this:



test=test001


But I want it to choose a random one from the for strings I have. I know I could do something like this, which I have done previously, but that was when choosing a random file from a directory:



test="$(printf "%sn" "${teststrings[RANDOM % ${#teststrings[@]}]}")"


But in this case I am not sure how to set testrings.










share|improve this question

















bumped to the homepage by Community 36 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.

















  • set the teststrings array like this: teststrings=( test001 test002 test003 test004 ), then your code will work. To save running in a subshell, printf -v test "%sn" "${teststrings[RANDOM % ${#teststrings[@]}]}"

    – glenn jackman
    Nov 4 '15 at 15:01
















0












0








0








I have a few strings, and I want to set a variable to one of them, randomly. Say the strings are test001, test002, test003 and test004.



If I set it like normal I'd obviously do it like this:



test=test001


But I want it to choose a random one from the for strings I have. I know I could do something like this, which I have done previously, but that was when choosing a random file from a directory:



test="$(printf "%sn" "${teststrings[RANDOM % ${#teststrings[@]}]}")"


But in this case I am not sure how to set testrings.










share|improve this question
















I have a few strings, and I want to set a variable to one of them, randomly. Say the strings are test001, test002, test003 and test004.



If I set it like normal I'd obviously do it like this:



test=test001


But I want it to choose a random one from the for strings I have. I know I could do something like this, which I have done previously, but that was when choosing a random file from a directory:



test="$(printf "%sn" "${teststrings[RANDOM % ${#teststrings[@]}]}")"


But in this case I am not sure how to set testrings.







bash variable






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 4 '15 at 14:49







DisplayName

















asked Nov 4 '15 at 14:34









DisplayNameDisplayName

4,88612 gold badges47 silver badges87 bronze badges




4,88612 gold badges47 silver badges87 bronze badges






bumped to the homepage by Community 36 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.









bumped to the homepage by Community 36 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.







bumped to the homepage by Community 36 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.















  • set the teststrings array like this: teststrings=( test001 test002 test003 test004 ), then your code will work. To save running in a subshell, printf -v test "%sn" "${teststrings[RANDOM % ${#teststrings[@]}]}"

    – glenn jackman
    Nov 4 '15 at 15:01





















  • set the teststrings array like this: teststrings=( test001 test002 test003 test004 ), then your code will work. To save running in a subshell, printf -v test "%sn" "${teststrings[RANDOM % ${#teststrings[@]}]}"

    – glenn jackman
    Nov 4 '15 at 15:01



















set the teststrings array like this: teststrings=( test001 test002 test003 test004 ), then your code will work. To save running in a subshell, printf -v test "%sn" "${teststrings[RANDOM % ${#teststrings[@]}]}"

– glenn jackman
Nov 4 '15 at 15:01







set the teststrings array like this: teststrings=( test001 test002 test003 test004 ), then your code will work. To save running in a subshell, printf -v test "%sn" "${teststrings[RANDOM % ${#teststrings[@]}]}"

– glenn jackman
Nov 4 '15 at 15:01












2 Answers
2






active

oldest

votes


















0














You can still do something similar:



v=$(printf "test%03d" $(($RANDOM%4+1)))
v=${!v}


where bash ${!variable} does one level of indirection towards the real variable test001 etc.





When the names of the variables can be anything, eg test001 somevar anothervar, setup an array:



declare -a teststrings=(test001 somevar anothervar)
v=${teststrings[$(($RANDOM % ${#teststrings[*]}))]}
w=${!v}
echo $w





share|improve this answer




























  • Good answer, but unfortunetaly I might have written my question a bit wrong. The strings can be anything, not just the same thing with increasing numbers. Sorry.

    – DisplayName
    Nov 4 '15 at 14:54











  • can you put all the variable names in an array?

    – meuh
    Nov 4 '15 at 14:58



















0














array=(test001 test002 test003 test004)          
totalstr=$((${#array[@]} - 1)) # -1 because 1st string = ${array[0]} , and last one(4th) = ${array[3]
randomnum=$(($(($RANDOM % $totalstr)) + 0)) # get random number between 0 and $totalstr-1
your_random_var=$(echo ${array[$randomnum]})





share|improve this answer




























  • That method to count how many elements the array has will break if any element contains whitespace, or if there are glob-patterns that match files. Simply use totalstr=${#array[@]}.

    – glenn jackman
    Nov 4 '15 at 15:58











  • You don't need to nest arithmetic expressions: $(( (RANDOM % totalstr) + 0 )) will do, but you don't need to add zero: why do you do that?

    – glenn jackman
    Nov 4 '15 at 15:58











  • Thanks , I d no idea about the array count, that's why ... !

    – Jonah
    Nov 4 '15 at 18:26











  • the '-1' & '+ 0' in both cmds , because the array starts from 0 , ends in totalstr-1 , if not , we won(t be able to call the first string inside the array , and we will call the empty one ${array[4]} as in this example !

    – Jonah
    Nov 5 '15 at 13:38














Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f240751%2fchoose-random-string-from-a-few-and-set-it-as-a-variable%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














You can still do something similar:



v=$(printf "test%03d" $(($RANDOM%4+1)))
v=${!v}


where bash ${!variable} does one level of indirection towards the real variable test001 etc.





When the names of the variables can be anything, eg test001 somevar anothervar, setup an array:



declare -a teststrings=(test001 somevar anothervar)
v=${teststrings[$(($RANDOM % ${#teststrings[*]}))]}
w=${!v}
echo $w





share|improve this answer




























  • Good answer, but unfortunetaly I might have written my question a bit wrong. The strings can be anything, not just the same thing with increasing numbers. Sorry.

    – DisplayName
    Nov 4 '15 at 14:54











  • can you put all the variable names in an array?

    – meuh
    Nov 4 '15 at 14:58
















0














You can still do something similar:



v=$(printf "test%03d" $(($RANDOM%4+1)))
v=${!v}


where bash ${!variable} does one level of indirection towards the real variable test001 etc.





When the names of the variables can be anything, eg test001 somevar anothervar, setup an array:



declare -a teststrings=(test001 somevar anothervar)
v=${teststrings[$(($RANDOM % ${#teststrings[*]}))]}
w=${!v}
echo $w





share|improve this answer




























  • Good answer, but unfortunetaly I might have written my question a bit wrong. The strings can be anything, not just the same thing with increasing numbers. Sorry.

    – DisplayName
    Nov 4 '15 at 14:54











  • can you put all the variable names in an array?

    – meuh
    Nov 4 '15 at 14:58














0












0








0







You can still do something similar:



v=$(printf "test%03d" $(($RANDOM%4+1)))
v=${!v}


where bash ${!variable} does one level of indirection towards the real variable test001 etc.





When the names of the variables can be anything, eg test001 somevar anothervar, setup an array:



declare -a teststrings=(test001 somevar anothervar)
v=${teststrings[$(($RANDOM % ${#teststrings[*]}))]}
w=${!v}
echo $w





share|improve this answer















You can still do something similar:



v=$(printf "test%03d" $(($RANDOM%4+1)))
v=${!v}


where bash ${!variable} does one level of indirection towards the real variable test001 etc.





When the names of the variables can be anything, eg test001 somevar anothervar, setup an array:



declare -a teststrings=(test001 somevar anothervar)
v=${teststrings[$(($RANDOM % ${#teststrings[*]}))]}
w=${!v}
echo $w






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 4 '15 at 15:06

























answered Nov 4 '15 at 14:46









meuhmeuh

33.7k1 gold badge25 silver badges59 bronze badges




33.7k1 gold badge25 silver badges59 bronze badges
















  • Good answer, but unfortunetaly I might have written my question a bit wrong. The strings can be anything, not just the same thing with increasing numbers. Sorry.

    – DisplayName
    Nov 4 '15 at 14:54











  • can you put all the variable names in an array?

    – meuh
    Nov 4 '15 at 14:58



















  • Good answer, but unfortunetaly I might have written my question a bit wrong. The strings can be anything, not just the same thing with increasing numbers. Sorry.

    – DisplayName
    Nov 4 '15 at 14:54











  • can you put all the variable names in an array?

    – meuh
    Nov 4 '15 at 14:58

















Good answer, but unfortunetaly I might have written my question a bit wrong. The strings can be anything, not just the same thing with increasing numbers. Sorry.

– DisplayName
Nov 4 '15 at 14:54





Good answer, but unfortunetaly I might have written my question a bit wrong. The strings can be anything, not just the same thing with increasing numbers. Sorry.

– DisplayName
Nov 4 '15 at 14:54













can you put all the variable names in an array?

– meuh
Nov 4 '15 at 14:58





can you put all the variable names in an array?

– meuh
Nov 4 '15 at 14:58













0














array=(test001 test002 test003 test004)          
totalstr=$((${#array[@]} - 1)) # -1 because 1st string = ${array[0]} , and last one(4th) = ${array[3]
randomnum=$(($(($RANDOM % $totalstr)) + 0)) # get random number between 0 and $totalstr-1
your_random_var=$(echo ${array[$randomnum]})





share|improve this answer




























  • That method to count how many elements the array has will break if any element contains whitespace, or if there are glob-patterns that match files. Simply use totalstr=${#array[@]}.

    – glenn jackman
    Nov 4 '15 at 15:58











  • You don't need to nest arithmetic expressions: $(( (RANDOM % totalstr) + 0 )) will do, but you don't need to add zero: why do you do that?

    – glenn jackman
    Nov 4 '15 at 15:58











  • Thanks , I d no idea about the array count, that's why ... !

    – Jonah
    Nov 4 '15 at 18:26











  • the '-1' & '+ 0' in both cmds , because the array starts from 0 , ends in totalstr-1 , if not , we won(t be able to call the first string inside the array , and we will call the empty one ${array[4]} as in this example !

    – Jonah
    Nov 5 '15 at 13:38
















0














array=(test001 test002 test003 test004)          
totalstr=$((${#array[@]} - 1)) # -1 because 1st string = ${array[0]} , and last one(4th) = ${array[3]
randomnum=$(($(($RANDOM % $totalstr)) + 0)) # get random number between 0 and $totalstr-1
your_random_var=$(echo ${array[$randomnum]})





share|improve this answer




























  • That method to count how many elements the array has will break if any element contains whitespace, or if there are glob-patterns that match files. Simply use totalstr=${#array[@]}.

    – glenn jackman
    Nov 4 '15 at 15:58











  • You don't need to nest arithmetic expressions: $(( (RANDOM % totalstr) + 0 )) will do, but you don't need to add zero: why do you do that?

    – glenn jackman
    Nov 4 '15 at 15:58











  • Thanks , I d no idea about the array count, that's why ... !

    – Jonah
    Nov 4 '15 at 18:26











  • the '-1' & '+ 0' in both cmds , because the array starts from 0 , ends in totalstr-1 , if not , we won(t be able to call the first string inside the array , and we will call the empty one ${array[4]} as in this example !

    – Jonah
    Nov 5 '15 at 13:38














0












0








0







array=(test001 test002 test003 test004)          
totalstr=$((${#array[@]} - 1)) # -1 because 1st string = ${array[0]} , and last one(4th) = ${array[3]
randomnum=$(($(($RANDOM % $totalstr)) + 0)) # get random number between 0 and $totalstr-1
your_random_var=$(echo ${array[$randomnum]})





share|improve this answer















array=(test001 test002 test003 test004)          
totalstr=$((${#array[@]} - 1)) # -1 because 1st string = ${array[0]} , and last one(4th) = ${array[3]
randomnum=$(($(($RANDOM % $totalstr)) + 0)) # get random number between 0 and $totalstr-1
your_random_var=$(echo ${array[$randomnum]})






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 5 '15 at 13:15

























answered Nov 4 '15 at 15:08









JonahJonah

9036 silver badges17 bronze badges




9036 silver badges17 bronze badges
















  • That method to count how many elements the array has will break if any element contains whitespace, or if there are glob-patterns that match files. Simply use totalstr=${#array[@]}.

    – glenn jackman
    Nov 4 '15 at 15:58











  • You don't need to nest arithmetic expressions: $(( (RANDOM % totalstr) + 0 )) will do, but you don't need to add zero: why do you do that?

    – glenn jackman
    Nov 4 '15 at 15:58











  • Thanks , I d no idea about the array count, that's why ... !

    – Jonah
    Nov 4 '15 at 18:26











  • the '-1' & '+ 0' in both cmds , because the array starts from 0 , ends in totalstr-1 , if not , we won(t be able to call the first string inside the array , and we will call the empty one ${array[4]} as in this example !

    – Jonah
    Nov 5 '15 at 13:38



















  • That method to count how many elements the array has will break if any element contains whitespace, or if there are glob-patterns that match files. Simply use totalstr=${#array[@]}.

    – glenn jackman
    Nov 4 '15 at 15:58











  • You don't need to nest arithmetic expressions: $(( (RANDOM % totalstr) + 0 )) will do, but you don't need to add zero: why do you do that?

    – glenn jackman
    Nov 4 '15 at 15:58











  • Thanks , I d no idea about the array count, that's why ... !

    – Jonah
    Nov 4 '15 at 18:26











  • the '-1' & '+ 0' in both cmds , because the array starts from 0 , ends in totalstr-1 , if not , we won(t be able to call the first string inside the array , and we will call the empty one ${array[4]} as in this example !

    – Jonah
    Nov 5 '15 at 13:38

















That method to count how many elements the array has will break if any element contains whitespace, or if there are glob-patterns that match files. Simply use totalstr=${#array[@]}.

– glenn jackman
Nov 4 '15 at 15:58





That method to count how many elements the array has will break if any element contains whitespace, or if there are glob-patterns that match files. Simply use totalstr=${#array[@]}.

– glenn jackman
Nov 4 '15 at 15:58













You don't need to nest arithmetic expressions: $(( (RANDOM % totalstr) + 0 )) will do, but you don't need to add zero: why do you do that?

– glenn jackman
Nov 4 '15 at 15:58





You don't need to nest arithmetic expressions: $(( (RANDOM % totalstr) + 0 )) will do, but you don't need to add zero: why do you do that?

– glenn jackman
Nov 4 '15 at 15:58













Thanks , I d no idea about the array count, that's why ... !

– Jonah
Nov 4 '15 at 18:26





Thanks , I d no idea about the array count, that's why ... !

– Jonah
Nov 4 '15 at 18:26













the '-1' & '+ 0' in both cmds , because the array starts from 0 , ends in totalstr-1 , if not , we won(t be able to call the first string inside the array , and we will call the empty one ${array[4]} as in this example !

– Jonah
Nov 5 '15 at 13:38





the '-1' & '+ 0' in both cmds , because the array starts from 0 , ends in totalstr-1 , if not , we won(t be able to call the first string inside the array , and we will call the empty one ${array[4]} as in this example !

– Jonah
Nov 5 '15 at 13:38


















draft saved

draft discarded




















































Thanks for contributing an answer to Unix & Linux Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f240751%2fchoose-random-string-from-a-few-and-set-it-as-a-variable%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Taj Mahal Inhaltsverzeichnis Aufbau | Geschichte | 350-Jahr-Feier | Heutige Bedeutung | Siehe auch |...

Baia Sprie Cuprins Etimologie | Istorie | Demografie | Politică și administrație | Arii naturale...

Ciclooctatetraenă Vezi și | Bibliografie | Meniu de navigare637866text4148569-500570979m