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.
-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.
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.