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:
screen -X stuff to Send Commands to a Running SessionIf 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:
bashscreen -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.
screen with a Custom CommandYou 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:
bashscreen -S mysession bash -c "alias ll='ls -la'; bash"
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.
screen's -c Flag with a Temporary ConfigurationWhile 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:
bashecho "alias ll='ls -la'" > /tmp/myscreenrc screen -c /tmp/myscreenrc rm /tmp/myscreenrc
.screenrc or .bashrc files.Limitation: Still involves a file, but it's temporary and not in your home directory.
screen's source Command (Inside the Session)If you are already inside a screen session, you can manually run:
bashsource <(echo "alias ll='ls -la'")
Limitation: You must do this manually each time you start a new window or session.
screen's window Command with Environment SetupYou can create a new window in screen and pass environment variables or aliases as part of the command for that window.
Example:
bashscreen -S mysession -X screen -t "mywindow" bash -c "alias ll='ls -la'; bash"
mywindow with the alias predefined.If you absolutely do not want to use any files, the most practical method is to:
screen session.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?
Le Chat can make mistakes. Check answers. Learn more