The "controlling terminal" aka. ctty, is distincted from "the terminal a process is interacting with".
Standard way of getting the path of ctty is ctermid(3). Upon calling this,
In freebsd since release 10, an actual path is looked up[1],
while older freebsd and glibc implementations[2] unconditionally returns "/dev/tty"].
ps(1) from the linux procps 3.2.8 package, read the numerical entry in /proc/*/stat[3], and then deduct the pathname partially by guessing[4, 5] due to lack of system support[6].
However if we are not strictly interested in the ctty but any terminal associated with stdio, tty(1) prints the terminal path connected to stdin, which is identical to ttyname(fileno(stdin))
in c, and an alternative is readlink /proc/self/fd/0
.
Less important thought regarding the unconditional "/dev/tty" behavior: Specs merely say the string returned by ctermid "when used as a path name, refer to the current controlling terminal", instead of some straightforward "is the path name of the current controlling terminal". It might be interpreted as that "/dev/tty" is not the controlling terminal, but only refer to the controlling terminal if the same process open(3) it. Thus not violating the
"a terminal may be ctty for at most one session" rule[7].
Another consequence is that when I am without any controlling terminal, ctermid does not fail -- such failing is allowed by specs[8] --, so only can I become aware of my ctty'lessness until failing a subsequent open(3), which is okay since specs also say calling open(3) on it is not guarranteed to succeed.
The grammar will only assign one word; successive words after that will be treated as a command.
Keep in mind that the rule you are citing refers to a TOKEN
, and that cannot contain an unquoted space. See 2.3 Token Recognition, e.g.,
- If the current character is an unquoted
<blank>
, any token containing the previous character is delimited and the current character shall be discarded.
Best Answer
If your shell is bash: within double brackets, the right-hand side of the == operator is a pattern unless fully quoted:
Or more tersely:
[[ world == w* ]] && echo true || echo false
[*]If you are not targetting bash specifically: use the case statement for pattern matching
[*] but you need to be careful with the
A && B || C
form becauseC
will be executed if either A fails or B fails. Theif A; then B; else C; fi
form will only execute C if A fails.