How to make `screen` keep my execution environment (`alias` for instance) without writing this execution environment to a file (like `~/.screenrc` or `~/.bashrc`)?
Apr 28, 8:53pm

To keep your execution environment (such as alias definitions) available in a screen session without writing them to a file like ~/.screenrc or ~/.bashrc, you have a few options:


1. Use screen -X stuff to Send Commands to a Running Session

If you already have a screen session running, you can use the stuff command to send your aliases (or any shell commands) directly to that session. This is useful if you want to inject commands after the session has started.

Example:

bash
screen -S mysession -X stuff "alias ll='ls -la'$(printf '\r')"
  • -S mysession: Targets the session named mysession.
  • stuff: Sends the given string as input to the session.
  • $(printf '\r'): Simulates pressing Enter to execute the command.

Limitation: This only works for a single command and must be repeated for each alias or environment variable.


2. Start screen with a Custom Command

You can start a screen session and immediately pass your aliases or environment variables as part of the shell command. This is done using the -e flag (execute command) with screen.

Example:

bash
screen -S mysession bash -c "alias ll='ls -la'; bash"
  • This starts a new bash shell inside screen, defines the alias, and then starts another bash shell to keep the session interactive.

Limitation: This only works when starting a new session, and you must include all aliases in the command.


3. Use screen's -c Flag with a Temporary Configuration

While you mentioned not wanting to write to a file, you can create a temporary file (e.g., in /tmp) and pass it to screen using the -c flag. This file can contain your aliases or environment setup, and you can delete it afterward.

Example:

bash
echo "alias ll='ls -la'" > /tmp/myscreenrc
screen -c /tmp/myscreenrc
rm /tmp/myscreenrc
  • This avoids modifying your permanent .screenrc or .bashrc files.

Limitation: Still involves a file, but it's temporary and not in your home directory.


4. Use screen's source Command (Inside the Session)

If you are already inside a screen session, you can manually run:

bash
source <(echo "alias ll='ls -la'")
  • This sources the alias directly into the current shell.

Limitation: You must do this manually each time you start a new window or session.


5. Use screen's window Command with Environment Setup

You can create a new window in screen and pass environment variables or aliases as part of the command for that window.

Example:

bash
screen -S mysession -X screen -t "mywindow" bash -c "alias ll='ls -la'; bash"
  • This creates a new window named mywindow with the alias predefined.

Best Approach for Your Use Case

If you absolutely do not want to use any files, the most practical method is to:

  1. Start a screen session.
  2. Manually run your alias commands in that session.
  3. Use screen -X stuff to automate this if you need to script it.

If you are open to a temporary file, the -c flag method is clean and easy to automate.

--- Would you like help automating one of these methods for your workflow?

Apr 28, 8:53pm

This is a shared conversation and may include unverified content that doesn't reflect Mistral AI's views. Login to resume chatting.

Le Chat can make mistakes. Check answers. Learn more