How to read only certain parts of a string using read command? [duplicate]












-1
















This question already has an answer here:




  • How to use the read command in Bash?

    8 answers




Let's make this example:



echo "Choose your color and number: "


My answer: Blue 2



I want to read the first word and the second word, something like this:



read COLOR NUMBER


and then I can use $COLOR (first word, or Blue) and $NUMBER (second word, or 2) as variables



How can I do it? Thanks










share|improve this question















marked as duplicate by jww, tripleee bash
Users with the  bash badge can single-handedly close bash questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 4 at 5:32


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • 3





    Uhm.. That's exactly how you do it.

    – that other guy
    Jan 2 at 22:30











  • Did you actually try your pseudocode before asking? :)

    – Charles Duffy
    Jan 2 at 22:36






  • 2





    BTW, better to use lowercase names for your own variables -- that way you don't stomp on reserved names meaningful to the shell. See the relevant standards documentation at pubs.opengroup.org/onlinepubs/9699919799/basedefs/…: The name space of environment variable names containing lowercase letters is reserved for applications. Applications can define any environment variables with names from this name space without modifying the behavior of the standard utilities -- true for regular shell variables too, because setting one overwrites any like-named environment variable.

    – Charles Duffy
    Jan 2 at 22:36








  • 1





    Possible duplicate of How to use the read command in Bash?

    – jww
    Jan 4 at 5:16


















-1
















This question already has an answer here:




  • How to use the read command in Bash?

    8 answers




Let's make this example:



echo "Choose your color and number: "


My answer: Blue 2



I want to read the first word and the second word, something like this:



read COLOR NUMBER


and then I can use $COLOR (first word, or Blue) and $NUMBER (second word, or 2) as variables



How can I do it? Thanks










share|improve this question















marked as duplicate by jww, tripleee bash
Users with the  bash badge can single-handedly close bash questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 4 at 5:32


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • 3





    Uhm.. That's exactly how you do it.

    – that other guy
    Jan 2 at 22:30











  • Did you actually try your pseudocode before asking? :)

    – Charles Duffy
    Jan 2 at 22:36






  • 2





    BTW, better to use lowercase names for your own variables -- that way you don't stomp on reserved names meaningful to the shell. See the relevant standards documentation at pubs.opengroup.org/onlinepubs/9699919799/basedefs/…: The name space of environment variable names containing lowercase letters is reserved for applications. Applications can define any environment variables with names from this name space without modifying the behavior of the standard utilities -- true for regular shell variables too, because setting one overwrites any like-named environment variable.

    – Charles Duffy
    Jan 2 at 22:36








  • 1





    Possible duplicate of How to use the read command in Bash?

    – jww
    Jan 4 at 5:16
















-1












-1








-1









This question already has an answer here:




  • How to use the read command in Bash?

    8 answers




Let's make this example:



echo "Choose your color and number: "


My answer: Blue 2



I want to read the first word and the second word, something like this:



read COLOR NUMBER


and then I can use $COLOR (first word, or Blue) and $NUMBER (second word, or 2) as variables



How can I do it? Thanks










share|improve this question

















This question already has an answer here:




  • How to use the read command in Bash?

    8 answers




Let's make this example:



echo "Choose your color and number: "


My answer: Blue 2



I want to read the first word and the second word, something like this:



read COLOR NUMBER


and then I can use $COLOR (first word, or Blue) and $NUMBER (second word, or 2) as variables



How can I do it? Thanks





This question already has an answer here:




  • How to use the read command in Bash?

    8 answers








linux bash shell






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 4 at 5:14









jww

54.1k41234513




54.1k41234513










asked Jan 2 at 22:24









MELEgrane DellabiMELEgrane Dellabi

14




14




marked as duplicate by jww, tripleee bash
Users with the  bash badge can single-handedly close bash questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 4 at 5:32


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by jww, tripleee bash
Users with the  bash badge can single-handedly close bash questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 4 at 5:32


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 3





    Uhm.. That's exactly how you do it.

    – that other guy
    Jan 2 at 22:30











  • Did you actually try your pseudocode before asking? :)

    – Charles Duffy
    Jan 2 at 22:36






  • 2





    BTW, better to use lowercase names for your own variables -- that way you don't stomp on reserved names meaningful to the shell. See the relevant standards documentation at pubs.opengroup.org/onlinepubs/9699919799/basedefs/…: The name space of environment variable names containing lowercase letters is reserved for applications. Applications can define any environment variables with names from this name space without modifying the behavior of the standard utilities -- true for regular shell variables too, because setting one overwrites any like-named environment variable.

    – Charles Duffy
    Jan 2 at 22:36








  • 1





    Possible duplicate of How to use the read command in Bash?

    – jww
    Jan 4 at 5:16
















  • 3





    Uhm.. That's exactly how you do it.

    – that other guy
    Jan 2 at 22:30











  • Did you actually try your pseudocode before asking? :)

    – Charles Duffy
    Jan 2 at 22:36






  • 2





    BTW, better to use lowercase names for your own variables -- that way you don't stomp on reserved names meaningful to the shell. See the relevant standards documentation at pubs.opengroup.org/onlinepubs/9699919799/basedefs/…: The name space of environment variable names containing lowercase letters is reserved for applications. Applications can define any environment variables with names from this name space without modifying the behavior of the standard utilities -- true for regular shell variables too, because setting one overwrites any like-named environment variable.

    – Charles Duffy
    Jan 2 at 22:36








  • 1





    Possible duplicate of How to use the read command in Bash?

    – jww
    Jan 4 at 5:16










3




3





Uhm.. That's exactly how you do it.

– that other guy
Jan 2 at 22:30





Uhm.. That's exactly how you do it.

– that other guy
Jan 2 at 22:30













Did you actually try your pseudocode before asking? :)

– Charles Duffy
Jan 2 at 22:36





Did you actually try your pseudocode before asking? :)

– Charles Duffy
Jan 2 at 22:36




2




2





BTW, better to use lowercase names for your own variables -- that way you don't stomp on reserved names meaningful to the shell. See the relevant standards documentation at pubs.opengroup.org/onlinepubs/9699919799/basedefs/…: The name space of environment variable names containing lowercase letters is reserved for applications. Applications can define any environment variables with names from this name space without modifying the behavior of the standard utilities -- true for regular shell variables too, because setting one overwrites any like-named environment variable.

– Charles Duffy
Jan 2 at 22:36







BTW, better to use lowercase names for your own variables -- that way you don't stomp on reserved names meaningful to the shell. See the relevant standards documentation at pubs.opengroup.org/onlinepubs/9699919799/basedefs/…: The name space of environment variable names containing lowercase letters is reserved for applications. Applications can define any environment variables with names from this name space without modifying the behavior of the standard utilities -- true for regular shell variables too, because setting one overwrites any like-named environment variable.

– Charles Duffy
Jan 2 at 22:36






1




1





Possible duplicate of How to use the read command in Bash?

– jww
Jan 4 at 5:16







Possible duplicate of How to use the read command in Bash?

– jww
Jan 4 at 5:16














1 Answer
1






active

oldest

votes


















1
















  • Use read word1 word2 ... wordN to assign words to $wordN:



    echo "word1: $word1"
    echo "word2: $word2"
    echo "wordN: $wordN"



  • Use -a array to access the words using their index.



    echo "${array[0]}"
    echo "${array[1]}"



See man page:



read [-a array][name ...]



The line is split into fields as with word
splitting, and the first word is assigned to the first NAME, the second
word to the second NAME, and so on, with any leftover words assigned to
the last NAME.



-a array assign the words read to sequential indices of the array
variable ARRAY, starting at zero







share|improve this answer


























  • Ok, this did work. Thank to you both!

    – MELEgrane Dellabi
    Jan 2 at 22:49


















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1
















  • Use read word1 word2 ... wordN to assign words to $wordN:



    echo "word1: $word1"
    echo "word2: $word2"
    echo "wordN: $wordN"



  • Use -a array to access the words using their index.



    echo "${array[0]}"
    echo "${array[1]}"



See man page:



read [-a array][name ...]



The line is split into fields as with word
splitting, and the first word is assigned to the first NAME, the second
word to the second NAME, and so on, with any leftover words assigned to
the last NAME.



-a array assign the words read to sequential indices of the array
variable ARRAY, starting at zero







share|improve this answer


























  • Ok, this did work. Thank to you both!

    – MELEgrane Dellabi
    Jan 2 at 22:49
















1
















  • Use read word1 word2 ... wordN to assign words to $wordN:



    echo "word1: $word1"
    echo "word2: $word2"
    echo "wordN: $wordN"



  • Use -a array to access the words using their index.



    echo "${array[0]}"
    echo "${array[1]}"



See man page:



read [-a array][name ...]



The line is split into fields as with word
splitting, and the first word is assigned to the first NAME, the second
word to the second NAME, and so on, with any leftover words assigned to
the last NAME.



-a array assign the words read to sequential indices of the array
variable ARRAY, starting at zero







share|improve this answer


























  • Ok, this did work. Thank to you both!

    – MELEgrane Dellabi
    Jan 2 at 22:49














1












1








1









  • Use read word1 word2 ... wordN to assign words to $wordN:



    echo "word1: $word1"
    echo "word2: $word2"
    echo "wordN: $wordN"



  • Use -a array to access the words using their index.



    echo "${array[0]}"
    echo "${array[1]}"



See man page:



read [-a array][name ...]



The line is split into fields as with word
splitting, and the first word is assigned to the first NAME, the second
word to the second NAME, and so on, with any leftover words assigned to
the last NAME.



-a array assign the words read to sequential indices of the array
variable ARRAY, starting at zero







share|improve this answer

















  • Use read word1 word2 ... wordN to assign words to $wordN:



    echo "word1: $word1"
    echo "word2: $word2"
    echo "wordN: $wordN"



  • Use -a array to access the words using their index.



    echo "${array[0]}"
    echo "${array[1]}"



See man page:



read [-a array][name ...]



The line is split into fields as with word
splitting, and the first word is assigned to the first NAME, the second
word to the second NAME, and so on, with any leftover words assigned to
the last NAME.



-a array assign the words read to sequential indices of the array
variable ARRAY, starting at zero








share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 2 at 22:43









Charles Duffy

181k28205261




181k28205261










answered Jan 2 at 22:28









Laurens DeprostLaurens Deprost

978114




978114













  • Ok, this did work. Thank to you both!

    – MELEgrane Dellabi
    Jan 2 at 22:49



















  • Ok, this did work. Thank to you both!

    – MELEgrane Dellabi
    Jan 2 at 22:49

















Ok, this did work. Thank to you both!

– MELEgrane Dellabi
Jan 2 at 22:49





Ok, this did work. Thank to you both!

– MELEgrane Dellabi
Jan 2 at 22:49





Popular posts from this blog

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith