FUNDAMENTALS A Complete Guide for Beginners
The PS3 variable in Bash in conjunction with the ‘select’ statement avails more efficient and user-friendly Bash prompts by creating an intuitive selection list. It helps to handle errors by preventing unexpected inputs gracefully. In this article, I will explain the PS3 variable in Bash with some practical examples.
Key Takeaways
- Learning about the PS3 variable in Bash.
- Exploring practical examples of the PS3 variable in Bash.
Free Downloads
What is PS3 Variable in Bash?
In Bash, PS3 is an environment variable that stands for ‘Prompt String 3’. When the ‘select’ statement is invoked inside a Bash script, this PS3 variable serves as the displayed user prompt. The ‘select’ construct displays a list of choices and allows users to select options from the interactive menus.
3 Examples of Using the PS3 Variable Through Bash Scripting
You can altogether align the Bash PS3 variable and the ‘select’ construct to illustrate the customized prompt to the user. Here, I’ll explain three practical examples of the Bash PS3 variable:
Example 1: Navigating Directory Interactively Using PS3 Variable
You can select a directory from an interactive menu and navigate into it using the PS3 variable. Follow the steps below to do so:
Steps to Follow >
➊ Open your Ubuntu Terminal and navigate to Desktop by running:
cd Desktop
- cd: It helps to navigate to a directory.
- Desktop: The directory I navigated.
➋ Now, to open a script in the nano text editor, write the command below:
nano navigate.sh
- nano: A text editor.
- navigate.sh: This is a script. Here, I have named the script ‘navigate.sh’. You can name any of your choices.
➌ Hereafter, write the following script inside the editor:
Script (navigate.sh) >
#!/bin/bash
PS3="Select a directory: "
#Listing up directories for selection
select directory in */ "Exit"; do
case $directory in
"Exit")
echo "Exit the directory."
break
;;
*)
cd "$directory" || exit
echo "You are now in: $PWD"
;;
esac
done
In #!/bin/bash, ‘#!’ is called ‘Shebang’ or ‘Hashbang’. In the line ‘PS3=”Select a directory: “’, the PS3 variable is set to Select a directory. Whenever the ‘select’ statement is executed, this prompt will be displayed. ‘select directory in */ “Exit”; do’ starts the ‘select’ statement and displays an ‘Exit’ option and the list of directories (*/) in the current directory where the user can select one by entering the corresponding number which is stored in the ‘directory’ variable.
Then, ‘case $directory in’ checks the selected option stored in the ‘directory’ variable. Now, if the user selects the ‘Exit’ option, the echo command will display ‘Exit the directory.’ and break out of the ‘select’ statement’s loop. Next, ‘*)’ indicates if the user selects any directory, the script enters the ‘case’ statement.
After that, ‘cd “$directory” || exit’ changes the directory and ensures that if the selected directory is invalid, the script exits and the user stays in the current directory. Hereafter, in the line ‘echo “You are now in: $PWD”’, the echo command prints the current working directory ($PWD). Finally, ‘esac’ and ‘done’ means the ‘case’ statement is closed and the loop is terminated.
➍ Then, press CTRL+S to save the file & press CTRL+X to exit.
➎ After that, use the command below to make the script executable:
chmod u+x navigate.sh
- chmod: Changes the permission of the files and directories.
- u+x: Adds the executable permission for the user.
- navigate.sh: The file which you want to make executable.
➏ Finally, run the script by the following command:
./navigate.sh
You can see from the above snapshot that I have navigated to my selected directory ‘linuxsimply’ and then left it by selecting the ‘Exit’ option.
Example 2: Selecting a Configuration Option in Bash
A basic example of the PS3 variable with the ‘select’ statement for selecting a configuration option is given below:
Script (configure.sh) >
#!/bin/bash
PS3="Select a configuration option: "
select option in "Client" "Server" "Exit"; do
case $option in
"Exit")
echo "Quitting"
break
;;
"Client")
echo "Client configuring.."
;;
"Server")
echo "Server configuring.."
;;
*)
echo "Invalid"
;;
esac
done
Here, in the line ‘PS3=”Select a configuration option: “’, the PS3 variable is set to Select a configuration option. Whenever the ‘select’ statement is executed, this prompt will be displayed. ‘select option in “Client” “Server” “Exit”; do’ initiates the ‘select’ statement and displays three options ‘“Client” “Server” “Exit”’. The ‘option’ variable stores the selected option.
Then, ‘case $option in’ checks the selected option stored in the ‘option’ variable. Now, if the user selects ‘Exit’, the echo command will display ‘Quitting’ and break out of the ‘select’ statement’s loop. But if the user selects ‘Client’ or, ‘Server’, the script will echo ‘Client configuring..’ or, ‘Server configuring..’ respectively. Moreover, if the user selects any option not included in the options, the echo command will display ‘Invalid’.
Now, run the script by the following command:
./configure.sh
In the above image, you can see that first I have selected ‘Server configuration’, then ‘Client configuration’, and after that the ‘Exit’ option. You can also choose any option of your need.
Example 3: Choosing the Favorite Color Using the PS3 Variable
The following script depicts the use of the PS3 variable for choosing a favorite color from an interactive list. Let’s see and break down the script:
Script (colors.sh) >
#!/bin/bash
PS3="Pick your favorite color: "
colors=("White" "Black" "Blue" "Quit")
select fav_color in "${colors[@]}"; do
case $fav_color in
"Quit")
echo "Quitting.."
break
;;
*)
echo "You have picked: $fav_color"
;;
esac
done
Here, the line ‘PS3=”Pick your favorite color: “’ explains that the PS3 variable is set to Pick your favorite color. ‘colors=(“White” “Black” “Blue” “Quit”)’ demonstrates the options to choose. The line ‘select fav_color in “${colors[@]}”; do’ starts the ‘select’ statement and displays the options. Here, the ‘fav_color’ variable stores the selected color.
Now, ‘case $fav_color in’ checks the selected option stored in the ‘fav_color’ variable. If the user selects ‘Quit’, the echo command will display ‘Quitting..’ and break out of the ‘select’ statement’s loop. But if the user selects any color option, the script will echo ‘You have picked: $fav_color’.
Now, run the script by the following command:
./colors.sh
From the above image, you can see that I have picked up my favorite color ‘Blue’ from the list, and after that, I have selected the ‘Quit’ option.
How to Set PS3 Prompt Non-interactively?
Generally, The PS3 variable in Bash prompts interactively to the user. However, if you want to provide and pass any value non-interactively to the PS3 prompt, you can try using both command substitution and redirection. Let’s see the example below:
Script (noninteractive.sh) >
#!/bin/bash
#Setting the desired value into the variable
PS3="You have already selected menu 3"
menu=("menu 1" "menu 2" "menu 3" "Exit")
select option in "${menu[@]}"; do
case $option in
"menu 1")
echo "menu 1 selected"
;;
"menu 2")
echo "menu 2 selected"
;;
"menu 3")
echo "menu 3 selected"
;;
"Exit")
echo "Exiting the menu"
break
;;
*)
echo "Invalid menu"
;;
esac
#Redirecting input from /dev/null to simulate user input
done < /dev/null
Here, ‘PS3=”You have already selected menu 3″’ specifies the setting of the desired option directly into the PS3 variable. The array ‘menu=(“menu 1” “menu 2” “menu 3” “Exit”)’ demonstrates the options to choose. Now, ‘select option in “${menu[@]}”; do’ starts the ‘select’ statement displaying the options of the menu. Here, the ‘option’ variable stores the selected color. In ‘case $option in’, the ‘case’ statement checks the selected option stored in the ‘option’ variable.
If the user selects ‘menu 1’, ‘menu 2’, ‘menu 3’, the script will echo ‘menu 1 selected’, ‘menu 2 selected’, ‘menu 3 selected’ respectively. But if the user selects ‘Exit’, the script will echo ‘Exiting the menu’ and break out of the ‘select’ loop. Moreover, if the user chooses any option not listed in the menu, the echo command will display ‘Invalid menu’. Finally, ‘done < /dev/null’ demonstrates that the script redirects the input from /dev/null to simulate the non-interactive user input ensuring the termination of the script.
Now, run the script by the following command:
./noninteractive.sh
From the above image, you can see that after running the script I didn’t have to select any option from the menu and the script automatically simulate the pre-selecting ‘menu 3’ using the pre-assigned value of the PS3 variable.
Conclusion
Precisely saying, the PS3 variable is an essential variable for Bash scripting scenarios that magnifies user interaction while creating the command line interfaces. Whether you’re simulating a simple command-line interface or a complex structure, the PS3 variable significantly intensifies your user experience.
People Also Ask
Related Articles
- What Are Built-in Variables in Bash [2 Cases With Examples]
- An Ultimate Guide of Using Bash Environment Variables
- The “.bashrc” Environment Variables [4 Cases]
- String Variables in Bash [Manipulation, Interpolation & Testing]
- What is Variable Array in Bash? [4 Cases]
- An Extensive Exploration of Bash Special Variables [9 Examples]
- What is Boolean Variable in Bash? [3 Cases With Examples]
- What is HereDoc Variable in Bash? [5 Practical Cases]
- What is PS1 Variable in Bash? [3 Customization Examples]
<< Go Back to Types of Variables in Bash | Bash Variables | Bash Scripting Tutorial