#!/usr/bin/env bash getOutOfHere() { echo 'So long, and thanks for all the fish!' exit } echo 'BEFORE' getOutOfHere echo 'AFTER'
BEFORE So long, and thanks for all the fish!
exit ends the script.
This command MUSTN'T be used on mounted filesystems.
Flag | Usage |
---|---|
-a | same as -p (option kept for backwards compatibility) |
-c | check disk for new bad blocks, and append them to the list of bad blocks |
-f | force checking even if the file system seems clean |
-n | Assume an answer of no to all questions. Warn only, don't write anything to the disk. Conflicts with -p and -y. |
-p | repair automatically without asking : fix any problem that can be safely fixed without human intervention. |
-v | verbose mode |
-w | write changes to disk immediately (for fsck.msdos only) |
-y | Assume an answer of yes to all questions. Conflicts with -n and -p. |
Bit | Usage |
---|---|
0 | No error |
1 | All errors have been corrected |
2 | All errors have been corrected but system should be rebooted |
4 | Some errors left uncorrected |
8 | Operational error |
16 | Usage or syntax error |
32 | Program cancelled by user request |
64 | (unused) |
128 | Shared library error |
name=value
. On invocation, the shell scans its own environment and creates a parameter for each name found, automatically marking it for export to child processes. Executed commands inherit the environment.name=value; export name
export name=value
Flag | Usage |
---|---|
-f name | name being exported is a function |
-n name | unexport the variable name |
-p | print all exported variables |
$foo='Hello, World'; echo $foo
Hello, World
$bar='Goodbye'; export foo; bash bash-3.2$echo $foo Hello, World bash-3.2$echo $bar (nothing)
bash-3.2$exit $export -n foo; bash bash-3.2$echo $foo (nothing) bash-3.2$exit
File descriptors 0, 1 and 2 are "reserved". You can still use them, but don't forget you're not the only one using them (details)
exec
lines of gitMergeSquash.sh.exec > /path/to/logFile 2>&1
construct (source, use with care in one-liners) :This is a trick so that stdout and stderr are redirected to /path/to/logFile for ALL commands of a script. Let's experiment this :
#!/usr/bin/env bash logFile=$(mktemp --tmpdir tmp.logFile.XXXXXXXX) >"$logFile" purge the log file of previous tests exec 2>> "$logFile" redirect stderr to log file ls . list the current directory contents in the shell window ls aFileThatIsNotHere the error message is appended to the log file but not shown in the shell window exec &>> "$logFile" redirecting stdout and stderr to log file ls .. list the parent directory contents in the log file ls aFileThatIsNotHere the error message is appended to the log file but not shown in the shell window
exec > /path/to/logFile 2>&1
can be shortened to exec &> /path/to/logFile
>>
because I don't want successive calls to exec from the same script to overwrite the log file. In the "real word", calling exec only once should suffice and single redirection (>
) should be enough, unless you want to append to the same log file forever . Check it :
hello world
world
hello world
hello world
HELLO hello world
The exec &> /path/to/logFile
construct is extremely convenient but should be used with care in one-liners. Indeed, such commands affect the current shell :
ls: cannot access |: No such file or directory ls: cannot access less: No such file or directoryThis fails because ls tries to list files named | and less instead of considering these as the continuation of the command. ls needs to be taught that this is a whole command.
ls: cannot access with: No such file or directory
ls: cannot access space: No such file or directory
-rw-r----- 1 bob users 0 Sep 3 15:23 withoutSpace
-rw-r----- 1 bob users 0 Sep 3 15:23 withoutSpace -rw-r----- 1 bob users 0 Sep 3 15:23 with space
ls: cannot access "with: No such file or directory
ls: cannot access space": No such file or directory
-rw-r----- 1 bob users 0 Sep 3 15:24 withoutSpace
-rw-r----- 1 bob users 0 Sep 3 15:24 withoutSpace -rw-r----- 1 bob users 0 Sep 3 15:24 with space
-rw-r----- 1 bob users 0 Sep 3 15:25 withoutSpace -rw-r----- 1 bob users 0 Sep 3 15:25 with space
doesn't seem to make a difference
Flag | Usage |
---|---|
-e | enable interpretation of backslash-escaped characters
the output string must be quoted (either single or double).
|
-n | do not output a trailing newline |
hello world
applebanana coconut