### Simulate Installation with DESTDIR Source: https://www.gnu.org/software/bash/manual/html_node/Installation-Names.html Use the 'DESTDIR' variable with 'make install' to simulate an installation and preview file locations without modifying the system. The value of DESTDIR acts as the root for the sample installation tree. ```bash mkdir /fs1/bash-install make install DESTDIR=/fs1/bash-install ``` -------------------------------- ### Specify Installation Prefixes for a Single Run Source: https://www.gnu.org/software/bash/manual/html_node/Installation-Names.html Override installation locations for a single 'make install' run by specifying variables like 'exec_prefix' as arguments to make. ```bash make install exec_prefix=/ ``` -------------------------------- ### Install compiled MO files Source: https://www.gnu.org/software/bash/manual/html_node/Creating-Internationalized-Scripts.html Install the compiled MO files into the appropriate directory structure. This involves setting `TEXTDOMAIN` and `TEXTDOMAINDIR` and copying the MO files to their final locations. ```bash TEXTDOMAIN=example TEXTDOMAINDIR=/usr/local/share/locale cp es.mo ${TEXTDOMAINDIR}/es/LC_MESSAGES/${TEXTDOMAIN}.mo cp eo.mo ${TEXTDOMAINDIR}/eo/LC_MESSAGES/${TEXTDOMAIN}.mo ``` -------------------------------- ### Specify Installation Prefix with make Source: https://www.gnu.org/software/bash/manual/html_node/Installation-Names.html Use the 'prefix' make variable to specify a custom installation prefix other than the default /usr/local. ```bash make install prefix=/path/to/install ``` -------------------------------- ### AND List Example Source: https://www.gnu.org/software/bash/manual/html_node/Lists.html Execute command2 if and only if command1 returns a zero exit status (success). ```bash command1 && command2 ``` -------------------------------- ### Bash select Statement Example Source: https://www.gnu.org/software/bash/manual/html_node/Conditional-Constructs.html An example of the select statement that allows the user to pick a filename from the current directory and displays the selection. ```bash select fname in *; do echo you picked $fname \($REPLY\) break; done ``` -------------------------------- ### Install Bash Completion Function for `cd` Source: https://www.gnu.org/software/bash/manual/html_node/A-Programmable-Completion-Example.html This command installs the `_comp_cd` function as the completion handler for the `cd` builtin. It uses options to control filename handling, quoting, and default Bash completions. ```bash # Tell readline to quote appropriate and append slashes to directories; # use the bash default completion for other arguments complete -o filenames -o nospace -o bashdefault -F _comp_cd cd ``` -------------------------------- ### Specify Separate Prefixes for Executable and Data Files Source: https://www.gnu.org/software/bash/manual/html_node/Installation-Names.html Use the '--exec-prefix' option with configure to set a separate installation prefix for programs and libraries, while documentation and data files use the regular prefix. ```bash configure --exec-prefix=/path/to/executables ``` -------------------------------- ### Sample inputrc Configuration Source: https://www.gnu.org/software/bash/manual/html_node/Sample-Init-File.html This is a comprehensive example of an inputrc file. It shows how to include system-wide settings, configure emacs editing mode, bind keys for emacs and default modes, and define application-specific macros for Bash and FTP. It also demonstrates setting various Readline variables. ```inputrc # This file controls the behavior of line input editing for # programs that use the GNU Readline library. Existing # programs include FTP, Bash, and GDB. # # You can re-read the inputrc file with C-x C-r. # Lines beginning with '#' are comments. # # First, include any system-wide bindings and variable # assignments from /etc/Inputrc $include /etc/Inputrc # # Set various bindings for emacs mode. set editing-mode emacs $if mode=emacs Meta-Control-h: backward-kill-word Text after the function name is ignored # # Arrow keys in keypad mode # #"\M-OD": backward-char #"\M-OC": forward-char #"\M-OA": previous-history #"\M-OB": next-history # # Arrow keys in ANSI mode # "\M-[D": backward-char "\M-[C": forward-char "\M-[A": previous-history "\M-[B": next-history # # Arrow keys in 8 bit keypad mode # #"\M-\C-OD": backward-char #"\M-\C-OC": forward-char #"\M-\C-OA": previous-history #"\M-\C-OB": next-history # # Arrow keys in 8 bit ANSI mode # #"\M-\C-[D": backward-char #"\M-\C-[C": forward-char #"\M-\C-[A": previous-history #"\M-\C-[B": next-history C-q: quoted-insert $endif # An old-style binding. This happens to be the default. TAB: complete # Macros that are convenient for shell interaction $if Bash # edit the path "\C-xp": "PATH=${PATH}\e\C-e\C-a\ef\C-f" # prepare to type a quoted word -- # insert open and close double quotes # and move to just after the open quote "\C-x\"": ""\C-b" # insert a backslash (testing backslash escapes # in sequences and macros) "\C-x\\": "\\" # Quote the current or previous word "\C-xq": "\eb"\ef"" # Add a binding to refresh the line, which is unbound "\C-xr": redraw-current-line # Edit variable on current line. "\M-\C-v": "\C-a\C-k$\C-y\M-\C-e\C-a\C-y=" $endif # use a visible bell if one is available set bell-style visible # don't strip characters to 7 bits when reading set input-meta on # allow iso-latin1 characters to be inserted rather # than converted to prefix-meta sequences set convert-meta off # display characters with the eighth bit set directly # rather than as meta-prefixed characters set output-meta on # if there are 150 or more possible completions for a word, # ask whether or not the user wants to see all of them set completion-query-items 150 # For FTP $if Ftp "\C-xg": "get \M-?" "\C-xt": "put \M-?" "\M-.": yank-last-arg $endif ``` -------------------------------- ### OR List Example Source: https://www.gnu.org/software/bash/manual/html_node/Lists.html Execute command2 if and only if command1 returns a non-zero exit status (failure). ```bash command1 || command2 ``` -------------------------------- ### Bash case Statement Example Source: https://www.gnu.org/software/bash/manual/html_node/Conditional-Constructs.html An example demonstrating the use of the case statement to describe an animal based on user input. It uses a default '*' pattern. ```bash echo -n "Enter the name of an animal: " read ANIMAL echo -n "The $ANIMAL has " case $ANIMAL in horse | dog | cat) echo -n "four";; man | kangaroo ) echo -n "two";; *) echo -n "an unknown number of";; esac echo " legs." ``` -------------------------------- ### Bash Job Notification Example Source: https://www.gnu.org/software/bash/manual/html_node/Job-Control-Basics.html This is an example of the notification Bash displays when a job starts asynchronously. It indicates the job number and the process ID of the last process in the pipeline. ```bash [1] 25647 ``` -------------------------------- ### Bash 'set -P' Symbolic Link Handling Example Source: https://www.gnu.org/software/bash/manual/html_node/The-Set-Builtin.html Demonstrates how 'set -P' prevents Bash from resolving symbolic links when changing directories, affecting the reported PWD. ```bash $ cd /usr/sys; echo $PWD /usr/sys $ cd ..; pwd /usr ``` ```bash If `set -P` is on, then: $ cd /usr/sys; echo $PWD /usr/local/sys $ cd ..; pwd /usr/local ``` -------------------------------- ### Display Configure Script Help Source: https://www.gnu.org/software/bash/manual/html_node/Basic-Installation.html View available options for the configure script to customize the build process. ```bash bash-4.2$ ./configure --help ``` -------------------------------- ### Bash Regex Match Anchored at Start Source: https://www.gnu.org/software/bash/manual/html_node/Conditional-Constructs.html This example demonstrates matching a literal string at the beginning of a line using regular expressions within Bash. Quoting the pattern correctly is crucial for the anchor character `^` to be interpreted as a regex operator. ```bash [[ $line =~ ^"initial string" ]] ``` -------------------------------- ### Getting the Number of Elements in an Array Source: https://www.gnu.org/software/bash/manual/html_node/Arrays.html Get the total number of elements in an array by using the '#' operator with the '@' or '*' subscript. ```bash ${#name[@]} ``` ```bash ${#name[*]} ``` -------------------------------- ### Getting the Length of an Array Element Source: https://www.gnu.org/software/bash/manual/html_node/Arrays.html Get the length of a specific array element by using the '#' operator before the array expansion. ```bash ${#name[subscript]} ``` -------------------------------- ### Create Build Tree with mkclone Source: https://www.gnu.org/software/bash/manual/html_node/Compiling-For-Multiple-Architectures.html Use the `mkclone` script to create a build directory with symbolic links to source files. This method requires Bash to be already built for at least one architecture. ```bash bash /usr/gnu/src/bash-2.0/support/mkclone -s /usr/gnu/src/bash-2.0 . ``` -------------------------------- ### Display 'set' Commands for Current Options Source: https://www.gnu.org/software/bash/manual/html_node/The-Set-Builtin.html Demonstrates how to print a series of 'set' commands that would recreate the current option settings by using 'set +o' without any specific option name. ```bash set +o ``` -------------------------------- ### Bash Long Option: --help Source: https://www.gnu.org/software/bash/manual/html_node/Invoking-Bash.html Displays a usage message on standard output and exits. ```bash --help ``` -------------------------------- ### Create gettext template file Source: https://www.gnu.org/software/bash/manual/html_node/Creating-Internationalized-Scripts.html Generate a template file (.pot) from your script to extract translatable strings. This file serves as the basis for creating translations. ```bash bash --dump-po-strings scriptname > domain.pot ``` -------------------------------- ### Alternative Coprocess Forms Source: https://www.gnu.org/software/bash/manual/html_node/Coprocesses.html Demonstrates other valid syntaxes for coprocesses, including variations with and without a NAME. ```bash coproc NAME compound-command coproc compound-command coproc simple-command ``` -------------------------------- ### Get Parameter Attributes Source: https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html Returns a string consisting of flag values representing the parameter's attributes. ```shell ${parameter@a} ``` -------------------------------- ### Bash Long Option: --init-file Source: https://www.gnu.org/software/bash/manual/html_node/Invoking-Bash.html Specifies a file to execute commands from instead of ~/.bashrc in an interactive shell. ```bash --init-file filename ``` -------------------------------- ### Set Editing Mode to Vi Source: https://www.gnu.org/software/bash/manual/html_node/Readline-Init-File-Syntax.html Example of setting the Readline editing mode to 'vi' for line editing commands. ```bash set editing-mode vi ``` -------------------------------- ### Bash Long Option: --debugger Source: https://www.gnu.org/software/bash/manual/html_node/Invoking-Bash.html Enables extended debugging mode by executing the debugger profile before the shell starts. ```bash --debugger ``` -------------------------------- ### Select Second Argument of Matching Command Source: https://www.gnu.org/software/bash/manual/html_node/Word-Designators.html The `!fi:2` syntax selects the second argument (word 2) of the most recent command starting with 'fi'. ```bash !fi:2 designates the second argument of the most recent command starting with the letters `fi`. ``` -------------------------------- ### Bind: Display Reusable Bindings Source: https://www.gnu.org/software/bash/manual/html_node/Bash-Builtins.html The '-p' option displays current Readline bindings in a format suitable for input to 'bind' or a Readline initialization file. ```bash bind -p ``` -------------------------------- ### Recommended Coprocess Form Source: https://www.gnu.org/software/bash/manual/html_node/Coprocesses.html This form is preferred for its simplicity and completeness, especially when dealing with compound commands. ```bash coproc NAME { command; } ``` -------------------------------- ### Exporting a Variable with Assignment Source: https://www.gnu.org/software/bash/manual/html_node/Shell-Parameters.html Shows how to use the 'command export' built-in to assign a value to a variable and export it. This is an example of an assignment statement used with a declaration command. ```shell command export var=value ``` -------------------------------- ### Bash 'set --' Assigning Positional Parameters Source: https://www.gnu.org/software/bash/manual/html_node/The-Set-Builtin.html Shows how '--' can be used to unset positional parameters or assign remaining arguments to them, even if they start with a hyphen. ```bash The remaining N arguments are positional parameters and are assigned, in order, to `$1`, `$2`, … `$N`. The special parameter `#` is set to N. ``` -------------------------------- ### Shopt Builtin Syntax Source: https://www.gnu.org/software/bash/manual/html_node/The-Shopt-Builtin.html Displays the general syntax for using the 'shopt' builtin command. It can be used with various options to manage shell behavior settings. ```bash shopt [-pqsu] [-o] [optname ...] ``` -------------------------------- ### Set Active Region Start Color Source: https://www.gnu.org/software/bash/manual/html_node/Readline-Init-File-Syntax.html Controls the text color and background for the active region using terminal escape sequences. This variable is reset when the terminal type changes. ```bash set active-region-start-color "\e[01;33m" ``` -------------------------------- ### Bash read command usage Source: https://www.gnu.org/software/bash/manual/html_node/Bash-Builtins.html Demonstrates the basic syntax and options for the `read` builtin command to capture user input. ```bash read [-Eers] [-a aname] [-d delim] [-i text] [-n nchars] [-N nchars] [-p prompt] [-t timeout] [-u fd] [name ...] ``` -------------------------------- ### Basic Pipeline Syntax Source: https://www.gnu.org/software/bash/manual/html_node/Pipelines.html Illustrates the fundamental structure of a Bash pipeline, connecting commands with '|' or '|&'. ```bash [time [-p]] [!] command1 [ | or |& command2 ] ... ``` -------------------------------- ### Non-interactive Script Execution with BASH_ENV Source: https://www.gnu.org/software/bash/manual/html_node/Bash-Startup-Files.html When Bash is invoked non-interactively to run a script, it checks for the BASH_ENV variable and sources the specified file if it is set. This allows for environment setup for scripts. ```bash if [ -n "$BASH_ENV" ]; then . "$BASH_ENV"; fi ``` -------------------------------- ### Assigning Values to an Indexed Array Source: https://www.gnu.org/software/bash/manual/html_node/Arrays.html Assign multiple values to an indexed array using compound assignment. Elements are assigned sequentially starting from index 0 unless a specific index is provided. ```bash name=(value1 value2 ... ) ``` -------------------------------- ### Bash help command syntax Source: https://www.gnu.org/software/bash/manual/html_node/Bash-Builtins.html Displays helpful information about builtin commands. It can show detailed help for matching patterns or a list of all builtins. ```bash help [-dms] [pattern] ``` -------------------------------- ### Basic Coprocess Syntax Source: https://www.gnu.org/software/bash/manual/html_node/Coprocesses.html The fundamental syntax for initiating a coprocess. NAME is optional and defaults to COPROC if not provided. ```bash coproc [NAME] command [redirections] ``` -------------------------------- ### Specifying Bash Interpreter with Shebang Source: https://www.gnu.org/software/bash/manual/html_node/Shell-Scripts.html This is a common idiom to ensure Bash interprets the script, even if installed in a different directory. It uses 'env' to find the first occurrence of 'bash' in the system's PATH. ```shell #! /bin/bash ``` ```shell #!/usr/bin/env bash ``` -------------------------------- ### Bash ulimit command syntax Source: https://www.gnu.org/software/bash/manual/html_node/Bash-Builtins.html Shows the general syntax for the ulimit builtin command. Use this command to control resource limits. ```bash ulimit [-H | -S] [-a] [-c | -d | -f | -l | -m | -n | -p | -t | -u | -v | -x] [limit] ``` -------------------------------- ### Bash Long Option: --version Source: https://www.gnu.org/software/bash/manual/html_node/Invoking-Bash.html Displays version information for the current Bash instance and exits. ```bash --version ``` -------------------------------- ### Substring Expansion on Positional Parameters with Offset and Length Source: https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html Extracts a specified number of positional parameters starting from a given offset. Negative offsets count from the end. A length of 0 results in no parameters being output. ```shell $ set -- 1 2 3 4 5 6 7 8 9 0 a b c d e f g h $ echo ${@:7} 7 8 9 0 a b c d e f g h ``` ```shell $ echo ${@:7:0} ``` ```shell $ echo ${@:7:2} 7 8 ``` ```shell $ echo ${@:7:-2} bash: -2: substring expression < 0 ``` ```shell $ echo ${@: -7:2} b c ``` ```shell $ echo ${@:0} ./bash 1 2 3 4 5 6 7 8 9 0 a b c d e f g h ``` ```shell $ echo ${@:0:2} ./bash 1 ``` ```shell $ echo ${@: -7:0} ``` -------------------------------- ### compgen command syntax Source: https://www.gnu.org/software/bash/manual/html_node/Programmable-Completion-Builtins.html Shows the general syntax for the `compgen` builtin. This command generates possible completion matches for a given word. ```bash compgen [-V varname] [option] [word] ``` -------------------------------- ### Substring Expansion on Positional Parameters Source: https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html Extracts substrings from positional parameters using offset and length. Indexing starts at 1 by default. `$0` is prefixed if offset is 0. Negative offsets count from the end. ```shell $ set -- 01234567890abcdefgh $ echo ${1:7} 7890abcdefgh ``` ```shell $ echo ${1:7:0} ``` ```shell $ echo ${1:7:2} 78 ``` ```shell $ echo ${1:7:-2} 7890abcdef ``` ```shell $ echo ${1: -7} bcdefgh ``` ```shell $ echo ${1: -7:0} ``` ```shell $ echo ${1: -7:2} bc ``` ```shell $ echo ${1: -7:-2} bcdef ``` -------------------------------- ### Get Caller Subroutine Context Source: https://www.gnu.org/software/bash/manual/html_node/Bash-Builtins.html The `caller` command returns the context of an active subroutine call. Without arguments, it shows the current line and filename; with an integer argument, it shows details for that frame in the call stack. ```bash caller [expr] ``` -------------------------------- ### Executing a Shell Script Source: https://www.gnu.org/software/bash/manual/html_node/Shell-Scripts.html This demonstrates the equivalent ways to execute a shell script. The first is by directly calling the script file, and the second is by explicitly invoking the bash interpreter. ```shell filename arguments ``` ```shell bash filename arguments ``` -------------------------------- ### Substring Expansion on Indexed Arrays with Offset and Length Source: https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html Extracts a specified number of elements from an indexed array starting from a given offset. Negative offsets count from the end. A length of 0 results in no elements being output. ```shell $ array=(0 1 2 3 4 5 6 7 8 9 0 a b c d e f g h) $ echo ${array[@]:7} 7 8 9 0 a b c d e f g h ``` ```shell $ echo ${array[@]:7:2} 7 8 ``` ```shell $ echo ${array[@]: -7:2} b c ``` ```shell $ echo ${array[@]: -7:-2} bash: -2: substring expression < 0 ``` ```shell $ echo ${array[@]:0} 0 1 2 3 4 5 6 7 8 9 0 a b c d e f g h ``` ```shell $ echo ${array[@]:0:2} 0 1 ``` ```shell $ echo ${array[@]: -7:0} ``` -------------------------------- ### Bind: Execute Readline Command Line Source: https://www.gnu.org/software/bash/manual/html_node/Bash-Builtins.html A raw readline command line can be passed directly to 'bind' for execution. ```bash bind readline-command-line ``` -------------------------------- ### Basic Brace Expansion with Comma-Separated Strings Source: https://www.gnu.org/software/bash/manual/html_node/Brace-Expansion.html Demonstrates generating strings by expanding a comma-separated list within braces, with a common prefix and suffix. Brace expansion is performed before other expansions. ```bash echo a{d,c,b}e ``` -------------------------------- ### Bash Character Class Example Source: https://www.gnu.org/software/bash/manual/html_node/Pattern-Matching.html This pattern matches a whitespace character, followed by an uppercase letter or an exclamation mark, then a dot, and finally a lowercase letter or a hyphen. Ensure the 'extglob' shell option is enabled for extended pattern matching. ```bash [[:space:]][[:upper:]!].[-[:lower:]] ``` -------------------------------- ### Bash Regex Match with Quoted Anchor (Incorrect) Source: https://www.gnu.org/software/bash/manual/html_node/Conditional-Constructs.html Illustrates an incorrect way to match a string at the start of a line using regex in Bash. Quoting the `^` character prevents it from being interpreted as a regular expression anchor, causing it to be matched literally. ```bash [[ $line =~ "^initial string" ]] ``` -------------------------------- ### complete command syntax Source: https://www.gnu.org/software/bash/manual/html_node/Programmable-Completion-Builtins.html Displays the syntax for the `complete` builtin, used to specify how arguments to commands should be completed. ```bash complete [-abcdefgjksuv] [-o comp-option] [-DEI] [-A action] [-G globpat] [-W wordlist] [-F function] [-C command] [-X filterpat] [-P prefix] [-S suffix] name [name ...] ``` ```bash complete -pr [-DEI] [name ...] ``` -------------------------------- ### Bash Regex Matching with Backslashes and Quotes Source: https://www.gnu.org/software/bash/manual/html_node/Conditional-Constructs.html Demonstrates how backslashes and quoting affect regular expression matching in Bash's `[[ ... =~ ... ]]` construct. The first two examples match a literal dot, while the latter two do not due to quoting. ```bash pattern='\\.' [[ . =~ $pattern ]] [[ . =~ \\. ]] [[ . =~ "$pattern" ]] [[ . =~ '\\.' ]] ``` -------------------------------- ### Bind: Display Key Sequences for Function Source: https://www.gnu.org/software/bash/manual/html_node/Bash-Builtins.html Use the '-q' option to display the key sequences that invoke a named Readline function. ```bash bind -q function ``` -------------------------------- ### Creating Directories with Brace Expansion Source: https://www.gnu.org/software/bash/manual/html_node/Brace-Expansion.html Uses brace expansion to create multiple directories with a common path and varying subdirectories. This is a common shorthand for repetitive directory creation. ```bash mkdir /usr/local/src/bash/{old,new,dist,bugs} ``` -------------------------------- ### Include External Readline Configuration File Source: https://www.gnu.org/software/bash/manual/html_node/Conditional-Init-Constructs.html Includes commands and key bindings from an external file, such as '/etc/inputrc'. This is useful for modular configuration. ```bash $include /etc/inputrc ``` -------------------------------- ### Bash read command with prompt Source: https://www.gnu.org/software/bash/manual/html_node/Bash-Builtins.html Demonstrates displaying a prompt before reading input using the `-p` option. ```bash read -p prompt ``` -------------------------------- ### Bind: List Readline Functions Source: https://www.gnu.org/software/bash/manual/html_node/Bash-Builtins.html Use the '-l' option with 'bind' to list all available Readline function names. ```bash bind -l ``` -------------------------------- ### Complex File Path Generation with Brace Expansion Source: https://www.gnu.org/software/bash/manual/html_node/Brace-Expansion.html Illustrates a more complex use of brace expansion for generating file paths, including nested expansions and wildcard-like patterns. This is useful for managing related files or configurations. ```bash chown root /usr/{ucb/{ex,edit},lib/{ex?.?*,how_ex}} ``` -------------------------------- ### Bind: List Current Bindings Source: https://www.gnu.org/software/bash/manual/html_node/Bash-Builtins.html Use the '-P' option to list current Readline function names and their associated bindings. ```bash bind -P ``` -------------------------------- ### Process Substitution for Writing Input Source: https://www.gnu.org/software/bash/manual/html_node/Process-Substitution.html Use the `>(list)` form to provide input to a process as if it were a file. Ensure no space between '>' and '('. ```bash >(list) ``` -------------------------------- ### Bash source command usage Source: https://www.gnu.org/software/bash/manual/html_node/Bash-Builtins.html Demonstrates the syntax for the 'source' command, used to execute commands from a file in the current shell. It is a synonym for '.'. ```bash source [-p path] filename [arguments] ``` -------------------------------- ### Process Substitution for Reading Output Source: https://www.gnu.org/software/bash/manual/html_node/Process-Substitution.html Use the `<(list)` form to read the output of a process as if it were a file. Ensure no space between '<' and '('. ```bash <(list) ``` -------------------------------- ### Set Compiler and Flags via Environment Source: https://www.gnu.org/software/bash/manual/html_node/Compilers-and-Options.html Use this method to specify the C compiler (CC) and compiler flags (CFLAGS) when running the `configure` script. This is useful for systems requiring specific options not automatically detected. ```bash CC=c89 CFLAGS=-O2 LIBS=-lposix ./configure ``` -------------------------------- ### Using fc for command re-execution with substitution Source: https://www.gnu.org/software/bash/manual/html_node/Bash-History-Builtins.html Demonstrates how to use the 'fc -s' command to re-execute a previous command after replacing a pattern with a replacement string. ```bash r='fc -s' ```