Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7065241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:55:57+00:00 2026-05-28T04:55:57+00:00

I am trying to do a very simple bash script which emulates the behavior

  • 0

I am trying to do a very simple bash script which emulates the behavior of checkboxes in appearance!

I want it to show some options and move the cursor to the next checkbox according to the key press of the left or right arrow keys. I’ve already managed to do this using READ and Ansii escape sequences to detect the arrow keys and I use tput to move the cursor.

My problem with this it’s that I need to read a certain letter (x for instance) to be pressed and then take another action. But how can I detect this key press and at the same time detect if an arrow key it’s being pressed or not?

To detect the ansii codes I need to read 3 characters and with the X character (the key to "select") I need to read just one, how can I read 3 characters and at the same time read just one?

Also I’ve been trying to make something so the user can JUST press the left or right arrow keys or the x key but if he presses ANY other key, nothing should happen!

I’ve done this this far:

#!/bin/bash
## Here I just print in the screen the "form" with the "checkboxes"
function screen_info(){
clear
cat <<EOF

    /\_/\_/\_/\_/\_/\_/\_/\_/\_/\_
    ||
    ||      1[ ]    2[ ]    3[ ]
    ||
    #############################



EOF
}

## This function detects the arrow keys and moves the cursor
function arrows(){

## I use ANSII escape sequences to detect the arrow keys
left_arrow=$'\x1b\x5b\x44'  #leftuierda
right_arrow=$'\x1b\x5b\x43' #rightecha
just_x_key=""

## With tput I move the cursor accordingly
cursor_x=14
cursor_y=3
tput cup $cursor_y $cursor_x

while [ -z "$just_x_key" -o "$just_x_key" != "$just_x_key" ]; do
    read -s -n3 key
    while [ `expr length "$key"` -ne 3 ]; do
        key=""
        read -s -n3 key
        break
    done
    case "$key" in
    $left_arrow)
        if [ $cursor_x -gt 14 ]; then
            cursor_x=`expr $cursor_x - 8`
        fi
        tput cup $cursor_y $cursor_x
        #This is supposed to be a simple condition detecting the x key pressed which I want to trigger something... But how to read a single character to this condition and 3 characters at the same time if the user presses the arrow key ???? =/
        #read -s just_x_key
        #if [ $just_x_key == x ]; then
        #   echo X
        #   tput cup 7 15
        #   echo "YOU PRESSED THE RIGHT KEY!!! =D"
        #fi 
        ;;
    $right_arrow)
        if [ $cursor_x -lt 28 ]; then
            cursor_x=`expr $cursor_x + 8`
        fi
        tput cup $cursor_y $cursor_x
        #read -s just_x_key
        #if [ $just_x_key == x ]; then
        #   echo X
        #   tput cup 7 15
        #   echo "YOU PRESSED THE RIGHT KEY!!! =D"
        #fi 
        ;;
    esac
done

exit $?
}


#EXECUTION
#=========

## I just call the functions!
screen_info
arrows

Yes I know, it’s not the most perfect code ever but I’m trying to learn. Suggestions will be very appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T04:55:57+00:00Added an answer on May 28, 2026 at 4:55 am

    If you just want the checkboxes in a script, you can use the whiptail(1) or dialog(1) tools to create the checkboxes:

    $ whiptail --checklist "Please pick one" 10 60 5 one one off two two off\
      three three off four four off  five five off
    
    
                     ┌──────────────────────────────────────────────────────────┐
                     │    [ ] one    one                                        │ 
                     │    [*] two    two                                        │ 
                     │    [ ] three  three                                      │ 
                     │    [*] four   four                                       │ 
                     │    [ ] five   five                                       │ 
                     │                                                          │ 
                     │              <Ok>                  <Cancel>              │ 
                     │                                                          │ 
                     └──────────────────────────────────────────────────────────┘ 
    
    "two" "four"$ 
    

    The final "two" "four" is the selected entries returned from the whiptail(1) program.

    If you’re programming this for the fun of it yourself, let me know and I’ll just convert this to a comment in the hopes that someone else will find the hint useful in the future.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i am trying to make a very simple bash script to find files matching
I am trying something very simple, but for some reason it does not work.
I'm trying to do a very simple INSERT using VB.NET. For some reason I'm
I am trying to do some very simple form validation checking for null or
I am trying to write a simple bash script that is listening on a
I'm trying to write very simple program which will imitate simple DeadLock, where Thread
Trying to process a very simple html5 script and render it using html5lib import
I am trying to do something very simple. I want to display the video
I am trying a very simple Fluent Nhibernate example: SQL 2005 database with one
Below I have a very simple example of what I'm trying to do. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.