Bash - yszheda/wiki GitHub Wiki
- http://tldp.org/LDP/Bash-Beginners-Guide/html/index.html
- http://www.tldp.org/LDP/abs/html/special-chars.html
-
set -n
orset -o noexec
: checks for syntax errors without actually running the script -
set -v
orset -o verbose
: echoes each command before executing it. -
set -x
orset -o xtrace
: echoes the result each command, but in an abbreviated manner. -
set -u
orset -o nounset
: gives an unbound variable error message and aborts the script -
set -e
orset -o errexit
: Abort script at first error, when a command exits with non-zero status (except in until or while loops, if-tests, list constructs)
- Adventures with bash's word erase
- Delete last word (Ctrl-W): Make bash shell behave like vim command line
- Is it possible to configure ctrl-w (delete word)?
Alt + Backspace
- http://stackoverflow.com/questions/3005963/how-can-i-have-a-newline-in-a-string-in-sh
- http://stackoverflow.com/questions/9139401/trying-to-embed-newline-in-a-variable-in-bash
- http://stackoverflow.com/questions/17698700/shell-script-to-add-header-to-a-file
- http://www.theunixschool.com/2011/03/different-ways-to-add-header-and.html
- http://stackoverflow.com/questions/5980153/insert-header-to-a-file
- http://askubuntu.com/questions/151674/how-do-i-insert-a-line-at-the-top-of-a-text-file-using-the-command-line
- http://stackoverflow.com/questions/21852630/using-awk-to-put-a-header-in-a-text-file
- http://unix.stackexchange.com/questions/53600/how-to-add-header-and-footer-to-the-flat-file
- http://www.unix.com/shell-programming-and-scripting/100424-insert-header-huge-data-file-without-using-intermediate-file.html
- http://stackoverflow.com/questions/487894/how-can-i-add-a-line-to-a-file-in-a-shell-script
- http://stackoverflow.com/questions/226703/how-do-i-prompt-for-input-in-a-linux-shell-script
- http://stackoverflow.com/questions/2654009/how-to-make-bash-script-ask-for-a-password
- http://stackoverflow.com/questions/226703/how-do-i-prompt-for-input-in-a-linux-shell-script
- http://stackoverflow.com/questions/5456120/how-to-only-get-file-name-with-linux-find
- http://stackoverflow.com/questions/27161454/silver-searcher-how-to-return-filename-without-path
- http://stackoverflow.com/questions/18713316/search-for-files-file-names-using-silver-searcher
-G --file-search-regex PATTERN
Only search files whose names match PATTERN.
grep -B 3 -A 2 foo README.txt
grep -C 3 foo README.txt
- http://www.thegeekstuff.com/2010/06/bash-array-tutorial/
- http://snailwarrior.blog.51cto.com/680306/154704
- http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_10_02.html
- http://wiki.bash-hackers.org/syntax/arrays
-
http://www.artificialworlds.net/blog/2012/10/17/bash-associative-array-examples/
-
http://linuxpoison.blogspot.sg/2012/07/bash-script-create-use-associative.html
-
http://stackoverflow.com/questions/3112687/how-to-iterate-over-associative-array-in-bash
-
http://stackoverflow.com/questions/13648410/how-can-i-get-unique-values-from-an-array-in-linux-bash
# Bash 4
animals=( ["moo"]="cow" ["woof"]="dog")
# animals['key']='value' to set value
# "${animals[@]}" to expand the values
# "${!animals[@]}" (notice the !) to expand the keys
- http://stackoverflow.com/questions/14366390/bash-if-condition-check-if-element-is-present-in-array
- http://stackoverflow.com/questions/3685970/check-if-an-array-contains-a-value
- http://superuser.com/questions/231881/push-pop-current-directory
- http://unix.stackexchange.com/questions/77077/how-do-i-use-pushd-and-popd-commands
- http://stackoverflow.com/questions/3667329/listing-only-directories-in-unix
- http://unix.stackexchange.com/questions/1645/is-there-any-option-with-ls-command-that-i-see-only-the-directories
- http://stackoverflow.com/questions/14352290/listing-only-directories-using-ls-in-bash-an-examination
-v
— natural sort of (version) numbers within text
- http://stackoverflow.com/questions/2013547/assigning-default-values-to-shell-variables-with-a-single-command-in-bash
- http://www.cyberciti.biz/faq/bash-ksh-if-variable-is-not-defined-set-default-variable/
- http://unix.stackexchange.com/questions/40786/how-to-do-integer-float-calculations-in-bash-or-other-languages-frameworks
- http://stackoverflow.com/questions/3279932/bash-multiplying-decimal-to-int
- http://stackoverflow.com/questions/27218141/multiplication-with-expr-in-shell-script
- https://nixshell.wordpress.com/2007/01/29/simple-maths-in-the-unix-shell/
- http://stackoverflow.com/questions/11039876/multiplication-on-command-line-terminal-unix
- http://askubuntu.com/questions/179898/how-to-round-decimals-using-bc-in-bash
- http://stackoverflow.com/questions/2394988/get-ceiling-integer-from-number-in-linux-bash
- http://www.unix.com/shell-programming-and-scripting/122398-how-get-ceiling-value-floating-numbers.html
- http://stackoverflow.com/questions/8078167/bizarre-issue-with-printf-in-bash-script09-and-08-are-invalid-numbers-07
- http://unix.stackexchange.com/questions/170700/how-to-use-printf-to-show-the-results-of-a-calculation
- http://stackoverflow.com/questions/6400249/shell-printf-invalid-number-error
- http://stackoverflow.com/questions/12845997/unexplicable-error-in-bash-printf-command-inside-a-script-returns-invalid-numb
- http://unix.stackexchange.com/questions/24822/printf-white-space-character-in-bash-script
- http://www.unix.com/shell-programming-and-scripting/83530-how-use-not-equal-if-statement.html
- http://stackoverflow.com/questions/15471264/how-to-check-if-is-not-equal-to-zero-in-unix-shell-scripting
- http://stackoverflow.com/questions/8654051/how-to-compare-two-floating-point-numbers-in-a-bash-script
- http://stackoverflow.com/questions/2424770/floating-point-comparison-in-shell-script
- http://unix.stackexchange.com/questions/83038/remove-last-character-from-line
- http://unix.stackexchange.com/questions/144298/delete-the-last-character-of-a-string-using-string-manipulation-in-shell-script
- http://stackoverflow.com/questions/9293887/in-bash-how-do-i-convert-a-space-delimited-string-into-an-array
- http://stackoverflow.com/questions/15575976/bash-script-to-convert-a-string-with-space-delimited-tokens-to-an-array
- http://stackoverflow.com/questions/4595078/how-to-check-return-value-of-find-statment-in-shell-script
- http://unix.stackexchange.com/questions/192439/find-result-used-in-if-statement
- http://unix.stackexchange.com/questions/46541/how-can-i-use-bashs-if-test-and-find-commands-together
- http://unix.stackexchange.com/questions/137101/find-no-such-file-or-directory-while-using-find-on-the-current-directory
- http://unix.stackexchange.com/questions/115863/delete-files-and-directories-by-their-names-no-such-file-or-directory
- http://unix.stackexchange.com/questions/180613/bash-find-get-directory-of-found-file
- http://stackoverflow.com/questions/9202495/have-find-print-just-the-filenames-not-full-paths
- http://stackoverflow.com/questions/5456120/how-to-only-get-file-name-with-linux-find
- http://www.cyberciti.biz/faq/unix-linux-find-all-empty-files/
- http://stackoverflow.com/questions/9964823/how-to-check-if-a-file-is-empty-in-bash-shell
find /path ! -type f -newermt "YYYY-MM-DD HH:MM:SS" -delete.
- Linux Find Files By Date And List Files Modified On a Specific Date
- Linux: using find to locate files older than
- https://unix.stackexchange.com/questions/42841/how-to-skip-permission-denied-errors-when-running-find-in-linux
- https://stackoverflow.com/questions/762348/how-can-i-exclude-all-permission-denied-messages-from-find
http://stackoverflow.com/questions/4509624/how-to-limit-depth-for-recursive-file-list
- How to combine 2 -name conditions in find?
- find -name “.xyz” -o -name “.abc” -exec to Execute on all found files, not just the last suffix specified
find . \( -name "*.xyz" -o -name "*.abc" \)
find . -type d -links 2
- http://stackoverflow.com/questions/242538/unix-shell-script-find-out-which-directory-the-script-file-resides
- http://stackoverflow.com/questions/6121091/get-file-directory-path-from-filepath
- http://stackoverflow.com/questions/59895/can-a-bash-script-tell-what-directory-its-stored-in
- http://stackoverflow.com/questions/3915040/bash-fish-command-to-print-absolute-path-to-a-file
- http://unix.stackexchange.com/questions/159206/making-a-shell-script-to-write-to-xml-file
- http://stackoverflow.com/questions/23925525/create-xml-file-using-shell-script
- http://www.cnblogs.com/Mainz/archive/2012/09/22/2697955.html
- http://stackoverflow.com/questions/8467424/echo-new-line-in-bash-prints-literal-n
- http://unix.stackexchange.com/questions/67898/using-the-not-equal-operator-for-string-comparison
- http://stackoverflow.com/questions/5080988/how-to-extract-string-following-a-pattern-with-grep-regex-or-perl
- http://unix.stackexchange.com/questions/32569/removing-all-spaces-tabs-newlines-etc-from-a-variable
-
http://stackoverflow.com/questions/9533679/how-to-insert-a-text-at-the-beginning-of-a-file
-
http://stackoverflow.com/questions/10849297/compare-a-string-in-unix
-
http://stackoverflow.com/questions/20536112/how-to-insert-a-new-line-in-linux-shell-script
-
How to join multiple lines of file names into one with custom delimiter?
e.g.
export PATH=`echo $PATH | sed 's/:/\n/g' | grep -v ros | paste -sd ":" -`
-
http://unix.stackexchange.com/questions/114754/extract-only-the-substring-after-double-quotes-grep
-
http://stackoverflow.com/questions/16675179/how-to-use-sed-to-extract-substring
-
http://stackoverflow.com/questions/20237966/extract-substring-using-regex-shell
-
How to extract the first two characters of a string in shell scripting?
$ long="USCAGol.blah.blah.blah"
$ short="${long:0:2}" ; echo "${short}"
US
echo 'time="2020-07-24 14:38:16" level=info msg="Networks: map[live:10.142.128.12]"' \
| grep -Po "(?<=live:).*(?=])"
# equivalent to:
echo 'time="2020-07-24 14:38:16" level=info msg="Networks: map[live:10.142.128.12]"' \
| grep -o "live:.*\]" \
| cut -d':' -f 2 \
| cut -d']' -f 1
-
(?=pattern)
: positive lookahead -
(?!pattern)
: negative lookahead -
(?<=)
: positive lookbehind -
(?!=)
: negative lookbehind
- http://stackoverflow.com/questions/16679369/count-occurrences-of-char-in-string-using-bash
- http://stackoverflow.com/questions/8629410/unix-count-occurrences-of-character-per-line-field
- http://stackoverflow.com/questions/1603566/count-occurrences-of-a-char-in-plain-text-file
- http://stackoverflow.com/questions/11832358/shell-script-to-count-the-occurrence-of-a-word-in-a-file
- http://stackoverflow.com/questions/16367598/grep-shell-scripting-how-do-i-count-the-number-of-occurrences-of-each-substring
- http://stackoverflow.com/questions/19771965/split-bash-string-by-newline-characters
- http://stackoverflow.com/questions/10586153/split-string-into-an-array-in-bash
- http://stackoverflow.com/questions/3236871/how-to-return-a-string-value-from-a-bash-function
- http://stackoverflow.com/questions/8742783/returning-value-from-called-function-in-shell-script
- http://stackoverflow.com/questions/19262313/using-functions-in-bash
- http://stackoverflow.com/questions/4111475/how-to-do-a-logical-or-operation-in-shell-scripting
- http://www.cyberciti.biz/faq/comparing-numbers-in-bash-shell/
- http://stackoverflow.com/questions/2188199/how-to-use-double-or-single-bracket-parentheses-curly-braces
- http://stackoverflow.com/questions/669452/is-preferable-over-in-bash-scripts
- http://unix.stackexchange.com/questions/226206/how-to-add-a-header-and-or-footer-to-a-sed-or-awk-stream
- http://unix.stackexchange.com/questions/113898/how-to-merge-two-files-based-on-the-matching-of-two-columns
- http://stackoverflow.com/questions/17698700/shell-script-to-add-header-to-a-file
- http://www.unix.com/unix-for-dummies-questions-and-answers/74435-adding-header-existing-file.html
- http://stackoverflow.com/questions/12902497/unix-linux-add-a-header-to-a-tab-delimited-file
- http://superuser.com/questions/228878/how-can-i-concatenate-two-files-in-unix
- http://stackoverflow.com/questions/22100384/awk-to-compare-two-files
- http://stackoverflow.com/questions/12854289/exclude-some-patterns-or-binary-files-using-sed-awk
- http://www.heapoverflow.me/question-using-awk-to-find-differences-in-two-files-using-specific-field-36103682
- http://www.commandlinefu.com/commands/view/6872/exclude-a-column-with-awk
-
http://unethicalblogger.com/2010/11/11/gnu-parallel-changed-my-life.html
-
http://stackoverflow.com/questions/11087256/parallel-processing-or-threading-in-shell-scripting
-
[*]https://www.codeword.xyz/2015/09/02/three-ways-to-script-processes-in-parallel/
-
https://www.gnu.org/software/parallel/parallel_tutorial.html
-
http://stackoverflow.com/questions/30315532/parallel-shell-execution-and-parameters
-
How to run command or code in parallel in bash shell under Linux or Unix
- http://stackoverflow.com/questions/5504414/unix-command-to-find-most-recent-directory-created
- http://unix.stackexchange.com/questions/136976/get-the-latest-directory-not-the-latest-file
- http://stackoverflow.com/questions/3915040/bash-fish-command-to-print-absolute-path-to-a-file
- http://stackoverflow.com/questions/59895/can-a-bash-script-tell-which-directory-it-is-stored-in
- http://stackoverflow.com/questions/4774054/reliable-way-for-a-bash-script-to-get-the-full-path-to-itself
# Eternal bash history.
# ---------------------
# Undocumented feature which sets the size to "unlimited".
# http://stackoverflow.com/questions/9457233/unlimited-bash-history
export HISTFILESIZE=
export HISTSIZE=
export HISTTIMEFORMAT="[%F %T] "
# Change the file location because certain bash sessions truncate .bash_history file upon close.
# http://superuser.com/questions/575479/bash-history-truncated-to-500-lines-on-each-login
export HISTFILE=~/.bash_eternal_history
# Force prompt to write history after every command.
# http://superuser.com/questions/20900/bash-history-loss
PROMPT_COMMAND="history -a; $PROMPT_COMMAND"
n. Setting HISTSIZE to a value less than zero causes the history list to be unlimited (setting it 0 zero disables the history list).
o. Setting HISTFILESIZE to a value less than zero causes the history file size to be unlimited (setting it to 0 causes the history file to be truncated to zero size).
# Avoid duplicates
HISTCONTROL=ignoredups:erasedups
# When the shell exits, append to the history file instead of overwriting it
shopt -s histappend
# After each command, append to the history file and reread it
PROMPT_COMMAND="${PROMPT_COMMAND:+$PROMPT_COMMAND$'\n'}history -a; history -c; history -r"
export LC_ALL=C
- Switch command output language from native language to english
- 让ubuntu 终端显示英文提示,而桌面环境显示中文的方法
- How to change language only for terminal
- Duplicate entries in $PATH a problem?
- $PATH is filled with duplicates
- Why do I get duplicated entries in my $PATH?
- Remove duplicate $PATH entries with awk command
- How can I clean out duplicate entries from my PATH? [duplicate]
diff -rq dir1 dir2
diff <(ls old) <(ls new)
diff -y <(xxd foo1.bin) <(xxd foo2.bin)
printf '01%.0s' {1..31616} | xxd -r -p - input0.bin
- How to split a large text file into smaller files with equal number of lines?
- https://linux.die.net/man/1/split
- Split large files into a number of smaller files in Unix
- How to get execution time of a script effectively?
- Get program execution time in the shell
- How to measure time of program execution and store that inside a variable
- Linux process - find files open by a Linux process
- Identify the files opened a particular process on linux
- Linux / UNIX: List Open Files for Process
- “ sudo: source: command not found”
- sudo: source: command not found
- How to keep environment variables when using sudo
sudo env PATH=$PATH
- Get environment variable of other user [closed]
- How to get another user’s PATH in Bash on Linux?
- Environment variables when run with 'sudo'
bind 'set show-all-if-ambiguous on'
bind 'TAB:menu-complete'