I switched to fish
shell and quite happy with it. I didn't get how can I handle booleans. I managed to write config.fish
that executes tmux
on ssh
(see: How can I start tmux automatically in fish shell while connecting to remote server via ssh) connection but I'm not happy with code readability and want to learn more about fish
shell (I've already read tutorial and looked through reference). I want code to look like that (I know that syntax isn't correct, I just want to show the idea):
set PPID (ps --pid %self -o ppid --no-headers)
if ps --pid $PPID | grep ssh
set attached (tmux has-session -t remote; and tmux attach-session -t remote)
if not attached
set created (tmux new-session -s remote; and kill %self)
end
if !\(test attached -o created\)
echo "tmux failed to start; using plain fish shell"
end
end
I know that I can store $status
es and compare them with test
as integers but I think it's ugly and even more unreadable. So the problem is to reuse $status
es and use them in if
and test
.
How can I achieve something like this?
Best Answer
You can structure this as an if/else chain. It's possible (though unwieldy) to use begin/end to put a compound statement as an if condition:
A nicer style is boolean modifiers. begin/end take the place of parenthesis:
(The first begin/end is not strictly necessary, but improves clarity IMO.)
Factoring out functions is a third possibility: