Linux Bourne BASH Shell One Liners

From richud.com
Jump to: navigation, search

Sub Shells in BASH

() create subshell, vars dont remain, {} not sub shell but need spaces and ending semi colon e.g.

$(cat /proc/mounts | grep -q "/mnt/$i") && { mnt=1 ;umount /mnt/$i/; }


Gotchas

The dreaded double nested faux pax - if the second command in the && {...} fails, it will do the || {...}

Simple example ('lg' is just nonsense to cause an error)

rich@PORTEGER830:~$ echo "aaa" && { lg;echo ok; } || { echo fail;echo fail2; }
aaa
lg: command not found
ok
rich@PORTEGER830:~$ echo "aaa" && { echo ok;lg; } || { echo fail;echo fail2; }
aaa
ok
lg: command not found
fail
fail2
rich@PORTEGER830:~$