
Learn through the super-clean Baeldung Pro experience:
>> Membership and Baeldung Pro.
No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.
Last updated: August 26, 2024
Linux, renowned for its versatility and adaptability, offers a plethora of shell prompts tailored to diverse user needs. Each prompt serves a specific purpose, ranging from primary interaction to debugging and script customization.
In this tutorial, we’ll delve into the intricacies of Linux prompts, exploring their functionalities, use cases, and how to navigate between them.
The PS1 environment variable in Linux determines the appearance and content of the primary shell prompt. This can be crucial as that’s the first thing we see when we open a terminal or other command-line interface (CLI).
In general, the prompt often provides valuable information to the user:
Further, we can change PS1 to fit various use cases:
Now, let’s see a real-life example of utilizing PS1:
$ PS1="[\u@\h \W]\$ "
[john@mycomputer Documents]$
This command customizes the primary shell prompt, represented by the PS1 environment variable. Here’s a breakdown of what each element in the command means:
For example, in the above code snippet, the username is john, the hostname is mycomputer, and the current working directory is Documents.
Notably, these might differ from one setup to another.
PS2 is the secondary prompt that appears when a command spans multiple lines and requires further input. Moreover, it’s typically a > greater-than sign.
Importantly, this avoids the usual functionality of > as a redirection operator.
Similar to PS1, PS2 has multiple use cases:
Next, let’s see a real conditional statement that utilizes PS2:
$ if [ -z $var ]; then
> echo "Variable 'var' is empty."
> fi
In the above example, we used a condition to check whether the $var variable is empty or not. In particular, we apply the -z operation that checks if a string is empty. If so, then the condition is met and the statement Variable ‘var’ is empty is printed.
Importantly, we notice that the > operator is at the beginning of each line till the statement ends. This visually aids the reader to deduce where the code construct starts and ends, as well as distinguish its separate elements.
The command-line interface can sometimes present menus with different options. In such cases, we use PS3 in conjunction with the select loop.
Accordingly, this loop provides a menu of options for the user to choose from. Moreover, when a user is presented with a menu of options, PS3 is used to customize the prompt that appears before the options.
Now, let’s see this concept in action:
$ PS3="Please choose your favorite fruit: "
$ options=("Apple" "Banana" "Orange" "Exit")
$ select opt in "${options[@]}"
> do
> case $opt in
> "Apple")
> echo "You selected Apple."
> ;;
> "Banana")
> echo "You selected Banana."
> ;;
> "Orange")
> echo "You selected Orange."
> ;;
> "Exit")
> echo "Exiting..."
> break
> ;;
> *)
> echo "Invalid option. Please choose a number from 1 to 4."
> ;;
> esac
> done
First, the above snippet customizes the prompt message, prompting the user to select their preferred fruit.
In essence, we create an array called options with several elements: Apple, Banana, Orange, Exit. These are the available choices for the user.
Next, we initiate a select loop that displays a menu based on the options listed in the options array. Notably, the whole loop and its contents also employ the PS2 prompt. Finally, we employ a case statement case $opt that checks the value of $opt and triggers the associated code block.
Accordingly, this presents the user with a menu of fruit options. Hence, a user can type the number corresponding to their choice:
1) Apple
2) Banana
3) Orange
4) Exit
Please choose your favorite fruit: 2
You selected Banana.
In summary, some of the most common use cases of PS3 are creating interactive menus and guiding user input.
When debugging shell scripts, we employ several features. Two of them are usually the PS4 prompt in conjunction with the shell’s xtrace option.
In general, PS4 prefixes each line of a script’s output with a string that aids in tracing and debugging the script:
$ PS4='DEBUG: '
$ set -x
$ for i in {1..3}
> do
> echo "Iteration $i"
> done
$ set +x
In the above example, we first set PS4 as DEBUG:, followed by a space. This means that every line of output that is a result of set -x will be prefixed with DEBUG.
Next, set -x enables the debug mode. When active, each executed command is printed to the console with the prefix defined in PS4. On the other hand, set +x disables the debug mode, ending the script or terminal session debug output.
In our case, the basic script loop simply prints out Iteration X for three iterations, where X is a number from 1 to 3. Let’s see the result from the commands above:
DEBUG: for i in '{1..3}'
DEBUG: echo 'Iteration 1'
Iteration 1
DEBUG: for i in '{1..3}'
DEBUG: echo 'Iteration 2'
Iteration 2
DEBUG: for i in '{1..3}'
DEBUG: echo 'Iteration 3'
Iteration 3
DEBUG: set +x
In this example, we can see that each line of output begins with the contents of PS4. In essence, this provides additional context for understanding the script’s execution flow, which can be especially useful for identifying issues or understanding how the script or command operates.
In this article, we understood the distinct roles of PS1, PS2, PS3, and PS4. Moreover, we looked at a real-life example for each of these prompts.
By knowing how to navigate each prompt, we can unlock the full potential of Linux shell prompts.
Finally, this knowledge empowers us to customize the shell environment, enhance our interactions, and streamline debugging processes for an optimized Linux experience.