-
Notifications
You must be signed in to change notification settings - Fork 1
/
bash.txt
60 lines (46 loc) · 2.24 KB
/
bash.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|| vs if/else pitfall
---------------------
touch /tmp/brr && ech 'created' || echo 'fail'
Here the touch succedes so we only want ech 'created' to be executed
but since that is wrong we also get the second echo
compare with:
if touch /tmp/brr
then
ech 'created'
else
echo 'fail'
fi
Quotes
------
Q. In FAQ096, why is this ok? ssh localhost '~/bin/args make CFLAGS="-g -O"'
Why do we end up with 2 args and not 3 because of remote word splitting?
A. it gets parsed by the local shell, then the remote shell
it'll run "$SHELL" -c '~/bin/args make CFLAGS="-g -O"' on the remote end
if you omit the '' quotes, it'll run "$SHELL" -c '/home/localhomedir/bin/args make CFLAGS=-g -O'
nesting single quotes:
ssh 'grep -iE '\''blah'\'' file'
1. ssh 'grep -iE ' 'blah' ' file' -> break it in 3 sets of quotes
2. \''blah'\' -> use literal 's that bash won't parse
ssh grep -iE 'blah' file -> our remote command
Completion
----------
complete -d -Fmyfunc cd: cd will use myfunc and dir completion
compgen -d m: generate completion list of dirs named m... (<=> -Adirectory)
compgen -a m: generate completion list of aliases named m...
compgen must put the possible completions in the COMPREPLY array
COMP_WORDS: command line args during completion - [ cd m ]<tab>
COMP_CWORD: index in COMP_WORDS of the word containing the cursor
Misc
----
parent shell PID: $$, current subshell: BASHPID
shell:
[ x"$var" = xyes ] && ... # the x at the front prevents a leading dash from being picked up as an option to test (-n, -z)
# Use PROMPT_COMMAND to aggregate users' history into a single file
# /var/log/user-history.log whoami | bash PID | history 1 | $?
# oge | [21118]: | 2013-09-09_10:46:34 su - | [1]
export PROMPT_COMMAND='RETRN_VAL=$?; logger -p local6.debug "$LOGNAME [$$]: $(history 1 | command sed "s/^[ ]*[0-9]\+[ ]*//" ) [$RETRN_VAL]"; ...'
source aka .
tells Bash to read the commands in myscript and run them in the current shell
environment. Since the commands are run in the current shell, they can change
the current shell's variables, working directory, open file descriptors,
functions, etc.