On Mon, Jul 09, 2018 at 09:08:17AM -0400, Greg Wooledge wrote: > On Sun, Jul 08, 2018 at 01:08:59PM +1000, Zenaan Harkness wrote: > > With a bash fn like: > > ttyGetNum () { return $(tty | grep -oE '[0-9]+$') ; } > > > > Then something like this will id your current tty: > > ttyGetNum; export Xorg_vt=vt$? > > Ewwwww!
:D > The return status of a shell function is NOT intended to be used this way. > It's analogous to the exit status of a shell command. It's a single > byte, unsigned, so you can only return 0 to 255. It's intended to convey > success or failure, not to carry an actual data payload. It's a byte, and I have slightly less than 255 Linux VTs, so it's adequate to this particular purpose ;) > I recommend that you switch to using a regular variable for this purpose. > I happen to like "r". With a regular variable, you're not restricted to > integers from 0 to 255, and you retain the ability to convey succss/failure > through the return code. > > As an aside, you can save the call to GNU grep by simply stripping the > leading "/dev/tty" from the output of tty using a shell parameter > expansion: > > ttyGetNum() { > local t > t=$(tty) || return 1 > [[ $t = /dev/tty+([0-9]) ]] || return 1 > r=${t#/dev/tty} > } > > if ttyGetNum; then export Xorg_vt=vt"$r"; fi I have been on a bit of a "support dash/ posix shell" and therefore removed various Bash-isms. There was at least one bash-ism (that I was using) that I was not able to remove. At least not easily. I don't remember what that was at the moment - haven't been hacking on this for some months.