Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm writing a script to build a software from sources, and there's a --platforms option. I would like to allow the user to select multiple items, but I don't know how to prevent them from making a mistake.


read -p "For what platforms do you wish to build [mac/win/linux32/linux64/all] ? "

if [[ -n "`echo $REPLY | grep 'win\|mac\|linux32\|linux64\|all`" ]] ; then
    echo "ok"
    echo "not ok"

If the user answers linux32, it should be OK (and it is)

If the user answers linux32,mac, it should be OK (and it is)

If the user answers lulz, it should NOT be OK (and it is not)

If the user answers linux32,lulz, it should NOT be OK (and it is, that's my issue)

I was wondering if you knew a way to allow the user to input whatever they want separated by commas, but only if it's one of the options the script is offering, so in this case linux32 linux64 mac win all.

Maybe with case there is a way to allow multiple inputs, or maybe add an elif $REPLY contains anything else than what we want. Another idea, could awk be used? I can't figure out myself how to do that.

share|improve this question
Mac is not a platform, OS X is. (Not Mac OS X!) – nyuszika7h Jun 2 '14 at 13:03
Mac is the option that --platforms= reads, and not OSX. That is how the build works, and I can't do anything about it. – MrVaykadji Jun 2 '14 at 13:16
up vote 1 down vote accepted

A simplified/improved version of arnefm's answer:

read -p 'Enter a comma-separated list of platforms to build for [win/mac/linux32/linux64/all]: ' input
IFS=',' read -a options <<< "$input"

shopt -s extglob

for option in "${options[@]}"; do
    case "$option" in
            printf 'Invalid option "%s" ignored.\n' "$option" >&2;;

IFS=',' read -a options <<< "$buildcommand"

for option in "${options[@]}"; do
    if [[ $option == 'all' ]]; then

if (( !buildvar )); then
    echo 'Incorrect input. Default build selected.' >&2
share|improve this answer
osx is not a valid choice for --platforms= option. – MrVaykadji Jun 2 '14 at 13:16
I see. I updated my answer to reflect that. – nyuszika7h Jun 2 '14 at 13:18
As far as I'm concern, you're right, it should be "osx" but not according to the sources I'm building : github.com/popcorn-official/popcorn-app/blob/dev-0.3/… – MrVaykadji Jun 2 '14 at 13:19
Also, you might want to take a look at mywiki.wooledge.org. It's a really good resource for shell/bash scripting. Specifically this and this is relevant. – nyuszika7h Jun 2 '14 at 15:46
I meant better than MrVaykadji's proposal, which might work in this particular case, but isn't really a clean solution. Passing a variable to echo, especially as first argument, and it's not even quoted (see my previous links). That grep might match more than expected, though not in this particular case. – nyuszika7h Jun 2 '14 at 16:34

read can break the input into words and store the result in an array. Set the IFS variable to the word separator character (it needs to be a single character, not a string — if the value of IFS contains multiple characters, then each character is a word separator).

IFS=, read -a platforms

Then check each element of the array against the set of supported platforms.

for p in "${platforms[@]}"; do
  case "$p" in
    win|mac|linux32|linux64) :;;
    all) platforms=(win mac linux32 linux64);;
    *) printf 1>&2 "Unsupported platform: %s\n" "$p"; return 2;;

You can also compare the set of platforms in one go. This is more convenient if you don't want to hardcode the set of supported platforms in the checking code¹.

supported_platforms=(win mac linux32 linux64)
IFS=, read -a platforms
bad_platform_names=($(comm -23 <(printf '%s\n' all "${platforms[@]}" | sort -u) \
                               <(printf '%s\n' "${supported_platforms[@]}" | sort -u)))
if [[ ${#bad_platform_names[@]} -ne 0 ]]; then
  printf "Unsupported platform: %s\n" "${bad_platform_names[@]}"
  exit 1
if printf '%s\n' "${platforms[@]}" | grep -qx all; then

A different approach would be to prompt for platforms one at a time using the select builtin.

¹ Of course you can do this in pure bash if you prefer.

share|improve this answer
This doesn't seem to work. I always have the "unsupported platform:" message and it also only records the first input. – MrVaykadji Jun 1 '14 at 23:54
@MrVaykadji Typo fixed – Gilles Jun 2 '14 at 0:01
It still only records the first input, and the second is always stated as unsupported, even if it should be ok. I don't understand – MrVaykadji Jun 2 '14 at 0:06
@MrVaykadji I've tested both snippets this time. The first worked, the second had several more typos. – Gilles Jun 2 '14 at 0:22
@MrVaykadji The bug is in your last line: $platforms is the first element of the array (this is a quirk of ksh and bash syntax). You need "${platforms[@]}" to get the list of elements in the array. – Gilles Jun 2 '14 at 0:31

Try this!

read -p "For what platforms do you wish to build [mac/win/linux32/linux64/all] ? " input
IFS=',' read -a options <<< "$input"

for option in "${options[@]}"
  case "$option" in
        buildcommand="linux32" && buildvar=1
        [ $buildvar == 1 ] && buildcommand="$buildcommand",linux64 || buildcommand="linux64" && buildvar=1
        [ $buildvar == 1 ] && buildcommand="$buildcommand",mac || buildcommand="mac" && buildvar=1
        [ $buildvar == 1 ] && buildcommand="$buildcommand",win || buildcommand="win" && buildvar=1
        buildcommand="all" && buildvar=1
        echo "'$option' was ignored." 

[ $buildvar == "0" ] && echo "Incorrect input. Default build selected." && buildcommand="default"

echo "=> $buildcommand"

This will prompt for a comma separated list of options. It will then split this list into an array and iterate through the elements checking each element separately, then combine all "good" elements into a single variable.

Hope this helps!

share|improve this answer
Thanks, I had to rewrite some of it, but it's working like a charm :) – MrVaykadji Jun 2 '14 at 0:35
You have set IFS generally for the script which can cause unexpected consequences later. You can set it only for the environment of the read command by prefixing the IFS=, there as in Gilles answer. – Matt Jun 2 '14 at 9:14

You can read a single line of input with sed and translate the delimiter to newlines like:

% sed 'y/,/\n/;q' /dev/tty
> this,is,a,single,line

Because sed writes the result to stdout as a text file, following that up with an explicit grep is easy and occurs in a single stream. And, in fact, you can use sed like a smart tee if you make use of its write to file function; and, if you write to file descriptor devices, you can split its output based on rules you define.

A function that prompts for input and outputs only a newline delimited list of acceptable arguments to stdout and erroneous output to stderr might look like:

_accept_prompt() (
   . /dev/fd/0
   _prompt "$@" >&2
   { _read_split "$@" |
        err=all _grep_ok "$@" |
        sed '1{$d}' >&2
   } 3>&1 | _grep_ok "$@"

_prompt() {
    cat ; printf ' : '
    Choose from : $(printf "'%s' " "$@")
    Enter a '$IFS'-delimited selection below...

_read_split() {
    sed -ne "H;x;s/^/Invalid input IGNORED:/;${y};p;x" \
        -ne "/all/s/.*/$*/;${y};w /dev/fd/3" -ne q
} </dev/tty

_grep_ok() {
    grep -${err+v}xF "$(printf '%s\n' "$@" $err)"

I split this into hopefully more descriptively named helper functions in lieu of the comments and attached them to the main function. So the flow all happens in the first few lines. I hoped to make this clearer.

_read_split outputs two streams - >&1 and >&3. _grep_ok picks up the first with $err defined and writes to >&2 all lines contained in its input that are not among _accept_prompt's positional parameters.

_grep_ok also concurrently picks up the second stream - >&3 and writes to its >&1 stdout all lines in its input that are among _accept_prompt's positional parameters.

Running it:

% _accept_prompt this is the list of acceptable parameters
    Choose from : 'this' 'is' 'the' 'list' 'of' 'acceptable' 'parameters'
    Enter a ','-delimited selection below...
 : all,invalid
Invalid input IGNORED:

You can alter the default ,comma delimiter on invocation like:

delim=? _accept_prompt $args
share|improve this answer
I find that code difficult to read. – nyuszika7h Jun 2 '14 at 15:48
It might be just me, but I find it too complicated. – nyuszika7h Jun 2 '14 at 16:08
I also am kind of lost reading that^^ But it's interresting – MrVaykadji Jun 2 '14 at 16:39
@MrVaykadji - is it any clearer now? I tried... – mikeserv Jun 2 '14 at 19:30
Yes, very much ! – MrVaykadji Jun 2 '14 at 20:01

I am presuming you already have some way of getting the variables in from input, since you mentioned a --platforms option. You can use regular expressions to match on the valid conditions.

if [[ $1 =~ $regex ]]; then 
  echo "ok"
  echo "not ok"

That should satisfy all the requirements:

$ ./stack.sh linux32
$ ./stack.sh linux32,mac
$ ./stack.sh lulz
not ok
$ ./stack.sh linux32,lulz
not ok

Granted, this doesn't stop people from using "all,all,all,all", but that at least can be trimmed down when actually running stuff later.

Benefits of this method include being able to loop until the user gets it right, if you're using the read command you have in the first post. Drawbacks include being a bit confusing to just glance at.

while ! [[ $REPLY =~ $regex ]]; do
  read -p "For what platforms do you wish to build [mac/win/linux32/linux64/all] ? "
share|improve this answer

The alternate method as suggested by Gilles, get users to input as little as possible by prompting them for the options.

available_platforms="mac win linux32 linux64"

positive() {
  printf "%s (Answer y for yes or anything else for no) " "$1"
  read answer
  if [ "$answer" != "y" ] && [ "$answer" != "Y" ]; then
    return 1

if positive "Do you want to build for all platforms [$available_platforms]?"; then
  for platform in $available_platforms; do
    if positive "Do you want to build for [$platform]?"; then
      build_platforms="${build_platforms}${platform} "

printf "build_platforms: %s\n" "$build_platforms"
share|improve this answer

Your Answer


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.