DEV Community

Discussion on: If Tmux

Collapse
 
thefluxapex profile image
Ian Pride • Edited

The thing about && and || in Bash (or normal Shell) is that it doesn't always work as expected depending on other factors (when errors are thrown etc...) and (imo) should be used only in the simplest of situations. Having said that; lots of situations are simple and not error prone.

Instead of ever using that method I now always resort to if/else. It's ugly, but it's safe in all situations and I don't have to ever think about it. Having said that; I don't see any issue with your use here and it should be fine.

Your 1st function is a bit redundant:

in_tmux () {
  if [ -n "$TMUX" ]; then
    return 0
  else
    return 1
  fi
  }
Enter fullscreen mode Exit fullscreen mode

Could be:

in_tmux () {
    if [ -z "$TMUX" ]; then
        return 1
    fi
}

# or even
in_tmux () { [ -z "$TMUX" ] && return 1; }
Enter fullscreen mode Exit fullscreen mode

as a return of 0 is the default return and the -z switch tests if empty.

Collapse
 
waylonwalker profile image
Waylon Walker

Oooh nice I like that, you are definitely helping me step up my bash skills here.

Collapse
 
thefluxapex profile image
Ian Pride

Glad to help. Been coding in Linux 15+ years and Windows 20+; about as long as I have been on a machine I have made it do what I wanted it to do and so if there's something I know or am familiar with it's a command line/shell environment and it feels good to pass on what I've learned; pay it forward. Lots of trial and error, happily enduring countless degradation from pretentious CS elitists, and finally some great groups of people in forums (most of which no longer exist) who finally helped me get to where I am today (knowledge-wise lol) and so anything I can help somebody with I try to.

Again, glad I could help.

Thread Thread
 
waylonwalker profile image
Waylon Walker

Lots of trial and error, happily enduring countless degradation from pretentious CS elitists, and finally some great groups of people in forums

Thank you so much, its people like you who have got me where I am now. My very first "Mentor" was that person, and would hold everything for himself. He inspired me to be nice to others and share as much as possible as I could see what being an ass did for him.