Choose random string from a few and set it as a variable?
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
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.
add a comment |
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
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
add a comment |
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
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
bash variable
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
add a comment |
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
add a comment |
2 Answers
2
active
oldest
votes
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
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
add a comment |
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]})
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 usetotalstr=${#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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
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
add a comment |
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
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
add a comment |
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
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
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
add a comment |
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
add a comment |
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]})
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 usetotalstr=${#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
add a comment |
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]})
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 usetotalstr=${#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
add a comment |
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]})
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]})
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 usetotalstr=${#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
add a comment |
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 usetotalstr=${#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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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