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












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 3 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 3 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,63594782




4,63594782





bumped to the homepage by Community 3 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 3 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 ... !

    – Jonas
    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 !

    – Jonas
    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

32.5k12154




32.5k12154













  • 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 ... !

    – Jonas
    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 !

    – Jonas
    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 ... !

    – Jonas
    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 !

    – Jonas
    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









JonasJonas

903517




903517













  • 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 ... !

    – Jonas
    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 !

    – Jonas
    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 ... !

    – Jonas
    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 !

    – Jonas
    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 ... !

– Jonas
Nov 4 '15 at 18:26





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

– Jonas
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 !

– Jonas
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 !

– Jonas
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

濃尾地震

How to rewrite equation of hyperbola in standard form

No ethernet ip address in my vocore2