Skip to content

Instantly share code, notes, and snippets.

@jcanfield
Created September 11, 2022 18:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jcanfield/e1057e4fa93e6d8b9953346b7fa3d114 to your computer and use it in GitHub Desktop.
Save jcanfield/e1057e4fa93e6d8b9953346b7fa3d114 to your computer and use it in GitHub Desktop.
Simple Bash Script to Create a New Session
#!/bin/bash
# USAGE: ./tmux-new.sh SESSION_NAME
#
# Credit to Keon Woortman
# URL: https://koenwoortman.com/tmux-sessions-should-be-nested-with-care-unset-tmux-to-force/
session_name="$1"
# 1. First you check if a tmux session exists with a given name.
tmux has-session -t=$session_name 2> /dev/null
# 2. Create the session if it doesn't exists.
if [[ $? -ne 0 ]]; then
TMUX='' tmux new-session -d -s "$session_name"
fi
# 3. Attach if outside of tmux, switch if you're in tmux.
if [[ -z "$TMUX" ]]; then
tmux attach -t "$session_name"
else
tmux switch-client -t "$session_name"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment