← Back to list

Mastering PS3: Customizing Your Interactive Shell Prompts

Shell scripting is all about automation, but sometimes automation needs a little human touch. When your script needs to ask the user to…

Nirbhay Singh · 2025-11-13 09:53 · 6 claps · 3.1 min read paywalled
#ps3 #shell-script #devops #linux #aws
Open on Medium ↗
Wiki topics: ☁️ · DevOps & Cloud 🔓 · Open Source

Mastering PS3: Customizing Your Interactive Shell Prompts

Shell scripting is all about automation, but sometimes automation needs a little human touch. When your script needs to ask the user to make a choice from a list, the select command in Bash (and other compatible shells) is your best friend. But have you ever noticed the default prompt it gives you? It's often just a dull #?. This is where PS3 comes into play!

if free user , Please use below link.

https://medium.com/@nirbhaysingh281/mastering-ps3-customizing-your-interactive-shell-prompts-1bb7c7f7e303?sk=027f7028dc86c675f9badd52e5b3e855

What is PS3?

PS3 stands for Prompt String 3. It's a special shell variable specifically designed to set the prompt for the select command. While PS1 (primary prompt) and PS2 (secondary prompt for multi-line commands) are more commonly known, PS3 is equally powerful for enhancing the interactivity of your scripts.

By default, PS3 is often set to #?or similar, which isn't very descriptive for a user. Customizing PS3 allows you to provide a clear, user-friendly prompt that guides the user on what action to take.

The select Command and PS3

The select command presents a numbered list of options to the user and waits for them to enter the number corresponding to their choice. Here's a basic example without PS3 customization:

#!/bin/bash
options=("Start" "Stop" "Restart" "Exit")
select action in "${options[@]}"; do
    case "$action" in
        "Start") echo "Starting service..." ;;
        "Stop") echo "Stopping service..." ;;
        "Restart") echo "Restarting service..." ;;
        "Exit") echo "Exiting." ; break ;;
        *) echo "Invalid option $REPLY. Please choose a number from the list." ;;
    esac
done

When you run this, the prompt you’ll see before typing your selection might look like:

  1. Start
  2. Stop
  3. Restart
  4. Exit #? _

That #?isn't very informative, is it?

Customizing PS3 for a Better User Experience

Now, let’s make that prompt much clearer by setting the PS3 variable. You can set PS3 just like any other shell variable, typically before or within the select loop.

#!/bin/bash
echo "--- Service Management Menu ---"
# Customize the PS3 prompt
PS3="Please choose an action for the service (enter number): "
options=("Start" "Stop" "Restart" "View Status" "Exit")
select action in "${options[@]}"; do
    case "$action" in
        "Start")
            echo "Initiating service startup..."
            # Add actual start command here
            ;;
        "Stop")
            echo "Shutting down service..."
            # Add actual stop command here
            ;;
        "Restart")
            echo "Restarting service now..."
            # Add actual restart command here
            ;;
        "View Status")
            echo "Checking service status..."
            # Add actual status command here
            ;;
        "Exit")
            echo "Exiting service management script."
            break # Exit the select loop
            ;;
        *)
            echo "Invalid selection: '$REPLY'. Please enter a number from 1 to ${#options[@]}."
            ;;
    esac
    echo # Add a newline for better readability after each action
done
echo "--- Script Finished ---"

When you run this improved script, the output will be much more user-friendly:

— — Service Management Menu — -

  1. Start
  2. Stop
  3. Restart
  4. View Status
  5. Exit Please choose an action for the service (enter number): _

This simple change significantly improves the script’s usability by telling the user exactly what is expected.

How select and PS3 Work Together

The select command works in conjunction with a list of items to present a numbered menu to the user. When the select loop runs, it displays each item in the list, prefixed by a number, and then presents the PS3 prompt. The script then waits for the user to enter a number corresponding to one of the options.

Here’s the basic syntax of a select loop:

select variable_name in item1 item2 ... itemN
do
    # Commands to execute based on user selection
done

When the user enters a number, the variable_name will be set to the value of the selected item (e.g., "Option A", "Option B"), and the special shell variable REPLY will hold the number entered by the user.

Now, let’s see PS3 in action.

Basic Usage of PS3

Consider a simple script that presents a menu of fruits:

#!/bin/bash
echo "Please choose your favorite fruit:"
select fruit in Apple Banana Orange Grape
do
    echo "You selected: $fruit"
    break # Exit the loop after selection
done
echo "Script finished."

When you run this script, the output might look something like this:

Please choose your favorite fruit:

  1. Apple
  2. Banana
  3. Orange
  4. Grape ?#

The ?#is the default PS3 prompt. It's functional but not very descriptive. Let's make it better!

Customizing the PS3 Prompt

To customize the prompt, you simply assign a string to the PS3 variable before the select loop.

#!/bin/bash
# Set a custom PS3 prompt
PS3="Enter the number of your preferred fruit (1-4): "
echo "Please choose your favorite fruit:"
select fruit in Apple Banana Orange Grape
do
    echo "You selected: $fruit"
    break
done
echo "Script finished."

Now, when you run the script, the output will be much clearer:

Please choose your favorite fruit:

  1. Apple
  2. Banana
  3. Orange
  4. Grape Enter the number of your preferred fruit (1–4):

Much better! The user now knows exactly what kind of input is expected.


메타데이터
post_id
1bb7c7f7e303
slug
mastering-ps3-customizing-your-interactive-shell-prompts-1bb7c7f7e303
url
https://medium.com/@nirbhaysingh281/mastering-ps3-customizing-your-interactive-shell-prompts-1bb7c7f7e303
canonical_url
https://medium.com/@nirbhaysingh281/mastering-ps3-customizing-your-interactive-shell-prompts-1bb7c7f7e303
author_url
https://medium.com/@nirbhaysingh281
status
ok
fetched_at
2026-06-21 19:25:17