bash script. if I invoke it directly, like ./myscript it should enter a subshell, change PS1 to something, and source current file to make all functions available in the new shell.

bash script subshell change PS1 source current file functions available

Removed unnecessary phrases and focused on key actions and terms to enhance search relevance, ensuring the query is concise while still capturing the user's intent.

To create a Bash script that enters a subshell, changes the command prompt (PS1), and sources the current file to make all functions available, you can follow the steps outlined below. This guide will provide a detailed explanation along with a sample script you can use.

Creating the Bash Script

Step 1: Open a Terminal

Begin by opening your terminal on a Unix-like operating system (Linux, macOS, etc.).

Step 2: Create a New Bash Script

You can use any text editor to create a new script file. For this example, we’ll create a file called myscript.sh.

touch myscript.sh
chmod +x myscript.sh  # Make the script executable

Step 3: Edit the Script

Next, open the file in your favorite text editor:

nano myscript.sh

Step 4: Write the Script

Copy and paste the following code into myscript.sh. This script sets the new PS1 prompt, enters a subshell, and sources the current file:

#!/bin/bash

# Change the prompt for the subshell
export PS1="(mysubshell) \$ "

# Source the current file to make all functions available
source "${BASH_SOURCE[0]}"

# Start a new interactive bash shell
bash --rcfile <(echo "export PS1='(mysubshell) \$ '")

Step 5: Save and Exit

If you are using nano, save the changes by pressing CTRL + O, then exit with CTRL + X.

Explanation of the Script

  • Shebang (#!/bin/bash): This line tells the system to execute the script using Bash.

  • Changing PS1: The export PS1="(mysubshell) \$ " command modifies the command prompt to indicate that you are inside a subshell.

  • Sourcing the Current Script: The line source "${BASH_SOURCE[0]}" ensures all functions defined in the current script are loaded into the subshell. ${BASH_SOURCE[0]} refers to the path of the script being executed.

  • Starting a New Interactive Shell: The command bash --rcfile <(echo "export PS1='(mysubshell) \$ '") starts a new interactive Bash shell with the modified PS1 prompt. The <(echo ...) syntax creates a temporary file containing the command you want to run, allowing the new shell to use it.

Step 6: Running the Script

Now, you can run your script directly from the terminal with the following command:

./myscript.sh

You should see the prompt change to (mysubshell) $, indicating that you are in a new subshell. Any functions defined in the script will be available in this subshell.

Tips and Additional Information

  • Exiting the Subshell: To exit the subshell, simply type exit or press Ctrl + D. This action will take you back to the original shell.

  • Use Cases: This script can be useful for testing changes in an isolated environment without affecting the main shell session.

  • Modification and Customization: You can modify the PS1 variable further to include colors or additional information, such as the current working directory. For example, you could set export PS1="\[\e[32m\](mysubshell) \[\e[0m\]\$ " to add color.

By following these steps, you will successfully create a Bash script that facilitates entering a subshell with a modified prompt and access to all defined functions. This technique enhances your scripting capabilities and provides a controlled testing environment.

Sources

10
1
Changing the PS1 on an interactive bash subshell easily
Unix

I would like to change the prompt to remind the user that they are in a special subshell and haven't gone through the rest of the process yet.

2
Update variable in bash function and use it in prompt (PS1)
Stack Overflow

I want to do is to call a function in PS1 to update a variable inside the function. Then I like to use that variable to add another line to PS1.

3
How to use source within a bash script to change the terminal prompt?
Superuser

First off, you don't need to write and source a file to change an environment variable--you can set PS1 just by executing PS1='$\ '.

4
[SOLVED] PS1: not working in .sh files, and why use 'export'?
Linuxquestions

The only purpose of export is to make the value available to sub-processes. Without it they are generally limited to the current shell environment.

5
Run an interactive bash subshell with initial commands without ...
Serverfault

If you simply pass an --init-file option, your script would replace the default, rather than augment it. The solution is to pass, using the <(..

6
Subshells and Functions – BASH Programming for Workflow ...
Uomresearchit

Subshells are separate instances of the command process, run as a new process, and defined within your scripts using () .

7
What's so special about PS1? Fun with customizing Bash command ...
Warp

Discover how to customize the PS1 prompt in Bash and how Warp integrates it to enhance your terminal experience.

8
Extending the Bash Prompt | Linux Journal
Linuxjournal

To change the way the prompt appears, change the PS1 variable. For experimentation purposes, the PS1 string can be entered at the prompt to show ...

9
how to export VARs from a subshell to a parent shell? - Server Fault
Serverfault

The answer is sourcing. Sourcing allows you to include variables in a script in the current shell, but never a parent of that.

10
GNU Bash Reference Manual
Gnu

This manual is meant as a brief introduction to features found in Bash. The Bash manual page should be used as the definitive reference on shell behavior.