Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I have the following script:

#!/bin/bash
# Bash Menu Script Example

PS3='Please enter your choice: '
options=("Option 1" "Option 2" "Option 3" "Quit")
select opt in "${options[@]}"
do
    case $opt in
        "Option 1")
            echo "you chose choice 1"
            ;;
        "Option 2")
            echo "you chose choice 2"
            ;;
        "Option 3")
            echo "you chose choice 3"
            ;;
        "Quit")
            break
            ;;
        *) echo invalid option;;
    esac
done

My problem is that I don't know how to remove a choice from the list after it has been selected. Is that possible? How?

share|improve this question
1  
I removed the dash tag since you're using bash, a very different beast. Please clarify what exactly you mean by "removing", do you want to shift all other array entries downwards, or simply leave one of the keys blank? What will you use this for? –  terdon Mar 18 at 17:23
    
out teacher ask us to create a game where you create for themes using the case statement each theme contains several question after the user selects a theme and answers the question it will prompted to select another theme , –  user259584 Mar 18 at 17:32

1 Answer 1

up vote 1 down vote accepted

The simplest way is to use unset:

$ options=(aa bb cc dd)
$ echo ${options[@]}
aa bb cc dd
## Remove the 3d element of the array (arrays start at 0)
$ unset options[2]
$ echo ${options[@]}
aa bb dd

For more details, see help unset:

unset: unset [-f] [-v] [name ...]
    Unset values and attributes of shell variables and functions.

    For each NAME, remove the corresponding variable or function.

    Options:
      -f    treat each NAME as a shell function
      -v    treat each NAME as a shell variable

    Without options, unset first tries to unset a variable, and if that fails,
    tries to unset a function.

    Some variables cannot be unset; also see `readonly'.

    Exit Status:
    Returns success unless an invalid option is given or a NAME is read-only.
share|improve this answer
    
but how would i know which array was selected –  user259584 Mar 18 at 17:40
    
@user259584 huh? What do you mean? You give the array name. For example foo=(aa bb cc); unset foo[2]. In my example the name of the array is actually array. I have edited to avoid confusion. –  terdon Mar 18 at 17:44
    
Thank you very much terdon worked perfectly –  user259584 Mar 18 at 17:47
    
after a user chooses lets say Option 1 i don't want it to appear in the menu the next time all i want there is Option 2 Option 3 and Quit @terdon –  user259584 Mar 18 at 19:00
    
@user259584 that's a bit more complex. It depends on how you are re-displaying the menu to the user. Personally I would use a function to do this and pass the $options array as a parameter to that function. That way, it will only show the values that are currently in the array. –  terdon Mar 18 at 19:01

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.