### Simulate Installation with DESTDIR Source: https://devdocs.io/bash/installation-names Use the `DESTDIR` variable with `make install` to preview where files will be installed without actually modifying the system. The value of `DESTDIR` acts as a root for the simulated installation tree. ```bash mkdir /fs1/bash-install make install DESTDIR=/fs1/bash-install ``` -------------------------------- ### Tilde Expansion Examples Source: https://devdocs.io/bash/tilde-expansion Illustrates various forms of tilde expansion and their corresponding replacements. ```bash ~ ``` ```bash ~/foo ``` ```bash ~fred/foo ``` ```bash ~+/foo ``` ```bash ~-/foo ``` ```bash ~N ``` ```bash ~+N ``` ```bash ~-N ``` -------------------------------- ### Bash: Example of set -P behavior Source: https://devdocs.io/bash/the-set-builtin This example demonstrates the difference in directory traversal when `set -P` is enabled versus the default behavior. Without `set -P`, `cd ..` moves up the logical directory structure. With `set -P`, it moves up the physical directory structure. ```bash $ cd /usr/sys; echo $PWD /usr/sys $ cd ..; pwd / If `set -P` is on, then: $ cd /usr/sys; echo $PWD /usr/local/sys $ cd ..; pwd /usr/local ``` -------------------------------- ### Specify Separate Prefixes for Executable and Data Files Source: https://devdocs.io/bash/installation-names Use `exec_prefix` to set a specific installation path for programs and libraries, while `prefix` continues to be used for documentation and data files. This allows for architecture-specific and architecture-independent files to be installed in different locations. ```bash make install exec_prefix=/path/to/executables ``` -------------------------------- ### Directory Creation with Brace Expansion Source: https://devdocs.io/bash/brace-expansion Provides an example of using brace expansion to create multiple directories with common prefixes and suffixes. ```bash mkdir /usr/local/src/bash/{old,new,dist,bugs} ``` -------------------------------- ### Change Installation Locations for a Single Run Source: https://devdocs.io/bash/installation-names Modify installation directories for a single `make install` command by specifying `exec_prefix` and `prefix` as arguments. This is useful for testing or temporary installations. ```bash make install exec_prefix=/ ``` -------------------------------- ### Bash select Statement Example Source: https://devdocs.io/bash/conditional-constructs An example of the select statement that allows the user to pick a filename from the current directory and displays the chosen file and its index. ```bash select fname in *; do echo you picked $fname \($REPLY\) break; done ``` -------------------------------- ### Specify Installation Prefix with make Source: https://devdocs.io/bash/installation-names Use the `prefix` make variable to specify a custom installation path for Bash and related files. This variable also provides defaults for `exec_prefix` and other installation variables. ```bash make install prefix=/path/to/custom/install ``` -------------------------------- ### Create Build Directory with Symbolic Links Source: https://devdocs.io/bash/compiling-for-multiple-architectures Use the 'mkclone' script to create a build directory with symbolic links to the source directory. This method requires Bash to be installed for at least one architecture first. ```bash bash /usr/gnu/src/bash-2.0/support/mkclone -s /usr/gnu/src/bash-2.0 . ``` -------------------------------- ### Installing a Bash Completion Function Source: https://devdocs.io/bash/a-programmable-completion-example This command registers the previously defined completion function with the `complete` builtin, enabling programmable completions for the specified command. ```bash # Tell readline to quote appropriate and append slashes to directories; ``` -------------------------------- ### Bash case Statement Example Source: https://devdocs.io/bash/conditional-constructs An example demonstrating the use of the case statement to handle different animal inputs and describe their number of legs. 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://devdocs.io/bash/job-control-basics This is an example of the output Bash displays when a job is started asynchronously. It indicates the job number and the process ID of the last process in the pipeline. ```bash [1] 25647 ``` -------------------------------- ### Get Help on Bash Builtins Source: https://devdocs.io/bash/bash-builtins Provides help information for builtin commands. Use -d for short descriptions, -m for manpage-like format, or -s for a usage synopsis. ```bash help [-dms] [pattern] ``` -------------------------------- ### Bash OR List Example Source: https://devdocs.io/bash/lists Execute command2 only if command1 fails. The return status is that of the last command executed. ```bash command1 || command2 ``` -------------------------------- ### Bash AND List Example Source: https://devdocs.io/bash/lists Execute command2 only if command1 succeeds. The return status is that of the last command executed. ```bash command1 && command2 ``` -------------------------------- ### Get Length of Array Element or Count in Bash Source: https://devdocs.io/bash/arrays Get the length of a specific array element using `${#name[subscript]}`. Use `${#name[@]}` or `${#name[*]}` to get the number of elements in the array. ```bash ${#name[subscript]} ``` ```bash ${#name[@]} ``` -------------------------------- ### Bash --help Option Source: https://devdocs.io/bash/invoking-bash Displays a usage message on standard output and exits successfully. ```bash --help ``` -------------------------------- ### Bash Regex Match at Start of String Source: https://devdocs.io/bash/conditional-constructs Anchor a regular expression pattern to the start of a string using `^` within `[[ ... ]]`. Ensure the anchor is not quoted to maintain its special meaning. ```bash [[ $line =~ ^"initial string" ]] ``` -------------------------------- ### List All Shopt Options Source: https://devdocs.io/bash/the-shopt-builtin Lists all available shell options and their current settings. The `-p` flag formats the output for potential reuse as input. ```bash shopt -p ``` -------------------------------- ### Recommended Coprocess Form Source: https://devdocs.io/bash/coprocesses Presents the recommended syntax for using coprocesses, which is simpler and more complete for compound commands. ```bash coproc NAME { command; } ``` -------------------------------- ### Bash --init-file and --rcfile Options Source: https://devdocs.io/bash/invoking-bash Specifies an alternative initialization file to execute commands from instead of ~/.bashrc in an interactive shell. ```bash --init-file filename --rcfile filename ``` -------------------------------- ### Bash --version Option Source: https://devdocs.io/bash/invoking-bash Displays version information for the current Bash instance on standard output and exits successfully. ```bash --version ``` -------------------------------- ### List Enabled Shopt Options Source: https://devdocs.io/bash/the-shopt-builtin Shows only the shell options that are currently enabled. This is useful for understanding the active configurations. ```bash shopt -s ``` -------------------------------- ### Bash --debugger Option Source: https://devdocs.io/bash/invoking-bash Enables extended debugging mode by executing the debugger profile before the shell starts. ```bash --debugger ``` -------------------------------- ### Bash: Select Command by Prefix and Argument Source: https://devdocs.io/bash/word-designators Use `!fi:2` to select the second argument of the most recent command that started with `fi`. ```bash !fi:2 designates the second argument of the most recent command starting with the letters `fi`. ``` -------------------------------- ### Set Compiler Options via Environment Variables Source: https://devdocs.io/bash/compilers-and-options Use this method to pass specific compiler and linker flags to the `configure` script when it doesn't automatically detect them. Requires a Bourne-compatible shell. ```bash CC=c89 CFLAGS=-O2 LIBS=-lposix ./configure ``` -------------------------------- ### Conditional Initialization Based on Readline Version Source: https://devdocs.io/bash/conditional-init-constructs Sets 'show-mode-in-prompt' if the Readline version is 7.0 or newer. Use for version-specific configurations. ```bash $if version >= 7.0 set show-mode-in-prompt on $endif ``` -------------------------------- ### Bash Shell Level with SHLVL Source: https://devdocs.io/bash/bash-variables SHLVL is incremented each time a new Bash instance is started, indicating the depth of nested shells. ```bash SHLVL ``` -------------------------------- ### Include Bashrc in Bash Profile Source: https://devdocs.io/bash/bash-startup-files This snippet is typically placed in ~/.bash_profile to ensure that ~/.bashrc is sourced when an interactive non-login shell is started. ```bash if [ -f ~/.bashrc ]; then . ~/.bashrc; fi ``` -------------------------------- ### Include External Configuration File Source: https://devdocs.io/bash/conditional-init-constructs Reads configuration settings from another file, typically '/etc/inputrc'. Use this to modularize your inputrc configuration. ```bash $include /etc/inputrc ``` -------------------------------- ### Get Array Indices in Bash Source: https://devdocs.io/bash/arrays Expand to the assigned indices of an array variable. The behavior within double quotes is similar to `${name[@]}` and `${name[*]}`. ```bash ${!name[@]} ``` ```bash ${!name[*]} ``` -------------------------------- ### Shopt Builtin Syntax Source: https://devdocs.io/bash/the-shopt-builtin Displays the syntax for the `shopt` builtin command. Use options like `-s` to enable, `-u` to disable, and `-p` to list options. ```bash shopt [-pqsu] [-o] [optname …] ``` -------------------------------- ### Get Parameter Length Source: https://devdocs.io/bash/shell-parameter-expansion Substitutes the length in characters of the expanded value of parameter. For '*' or '@', it's the number of positional parameters. For arrays, it's the number of elements. ```bash ${#parameter} ``` -------------------------------- ### Bash Process Substitution Syntax Source: https://devdocs.io/bash/process-substitution Illustrates the basic syntax for process substitution using `<(list)` for input and `>(list)` for output. ```bash <(list) ``` ```bash >(list) ``` -------------------------------- ### Shell Uptime with SECONDS Source: https://devdocs.io/bash/bash-variables SECONDS expands to the number of seconds since the shell started. Assignment resets the counter. It loses special properties if unset. ```bash SECONDS ``` -------------------------------- ### Bash: Select First Word from Search Source: https://devdocs.io/bash/word-designators The word designator `%` selects the first word matched by a `?string?` search, provided the search string starts a word. ```bash % The first word matched by the most recent ‘?string?’ search, if the search string begins with a character that is part of a word. ``` -------------------------------- ### Executing a Shell Script Source: https://devdocs.io/bash/shell-scripts Demonstrates the syntax for executing a shell script with arguments. This is equivalent to invoking bash directly with the script. ```shell filename arguments ``` ```shell bash filename arguments ``` -------------------------------- ### Display Directory Stack with Full Pathnames Source: https://devdocs.io/bash/directory-stack-builtins Use the `-l` option with `dirs` to display full pathnames instead of using tilde expansion for the home directory. ```bash dirs -l ``` -------------------------------- ### Substring Expansion Source: https://devdocs.io/bash/shell-parameter-expansion Extract a substring from 'parameter' starting at 'offset' and extending up to 'length' characters. Negative offsets count from the end of the string. Omitting 'length' extracts to the end. ```bash $ string=01234567890abcdefgh $ echo ${string:7} 7890abcdefgh ``` ```bash $ echo ${string:7:0} ``` ```bash $ echo ${string:7:2} 78 ``` ```bash $ echo ${string:7:-2} 7890abcdef ``` ```bash $ echo ${string: -7} bcdefgh ``` ```bash $ echo ${string: -7:0} ``` ```bash $ echo ${string: -7:2} bc ``` ```bash $ echo ${string: -7:-2} bcdef ``` -------------------------------- ### Bash: Unset Positional Parameters with set -- Source: https://devdocs.io/bash/the-set-builtin Use `set --` to unset all positional parameters. If arguments follow, they are assigned to positional parameters, even if they start with a hyphen. ```bash set -- ``` -------------------------------- ### Alternative Coprocess Forms Source: https://devdocs.io/bash/coprocesses Illustrates other valid forms for defining coprocesses, including variations for compound and simple commands. ```bash coproc NAME compound-command coproc compound-command coproc simple-command ``` -------------------------------- ### Set Compiler Options using `env` Source: https://devdocs.io/bash/compilers-and-options An alternative method to set environment variables for `configure`, useful on systems with the `env` program. This allows specifying include paths and linker flags. ```bash env CPPFLAGS=-I/usr/local/include LDFLAGS=-s ./configure ``` -------------------------------- ### Read Lines into Array with mapfile Source: https://devdocs.io/bash/bash-builtins Use `mapfile` to read lines from standard input or a file descriptor into an indexed array. Options control delimiters, line limits, starting index, and trailing character removal. ```bash mapfile -d "," -n 10 -O 5 -s 2 -t -u 3 array ``` -------------------------------- ### Bash Regex Match with Optional Character Source: https://devdocs.io/bash/conditional-constructs Use the `=~` operator within `[[ ... ]]` for POSIX extended regular expression matching. This example checks if a string contains zero or more whitespace characters followed by an optional 'a' and then a 'b'. ```bash [[ $line =~ [[:space:]]*(a)?b ]] ``` -------------------------------- ### Bash --verbose Option Source: https://devdocs.io/bash/invoking-bash Prints shell input lines as they are read, equivalent to -v. ```bash --verbose ``` -------------------------------- ### Substring Expansion on Positional Parameters Source: https://devdocs.io/bash/shell-parameter-expansion Extract a substring from a positional parameter ('$1', '$2', etc.) starting at 'offset' and extending up to 'length' characters. Negative offsets count from the end of the string. Omitting 'length' extracts to the end. ```bash $ set -- 01234567890abcdefgh $ echo ${1:7} 7890abcdefgh ``` ```bash $ echo ${1:7:0} ``` ```bash $ echo ${1:7:2} 78 ``` ```bash $ echo ${1:7:-2} 7890abcdef ``` ```bash $ echo ${1: -7} bcdefgh ``` ```bash $ echo ${1: -7:0} ``` ```bash $ echo ${1: -7:2} bc ``` ```bash $ echo ${1: -7:-2} bcdef ``` -------------------------------- ### Basic Brace Expansion Source: https://devdocs.io/bash/brace-expansion Demonstrates generating strings with a common prefix and suffix from a comma-separated list within braces. ```bash echo a{d,c,b}e ``` -------------------------------- ### Assign Multiple Values to Indexed Array in Bash Source: https://devdocs.io/bash/arrays Assign multiple values to an indexed array using compound assignment. If no subscript is provided, elements are assigned to consecutive indices starting from the last assigned index plus one. ```bash name=(value1 value2 … ) ``` -------------------------------- ### List Specific Shopt Options Source: https://devdocs.io/bash/the-shopt-builtin Lists the status of specified shell options. This helps in quickly checking the configuration of particular behaviors. ```bash shopt -p assoc_expand_once cdspell ``` -------------------------------- ### Configure `cd` command completion in Bash Source: https://devdocs.io/bash/a-programmable-completion-example Use the `complete` command with options to enable filename completion, prevent automatic spacing, and include Bash defaults for the `cd` command. This setup calls the `_comp_cd` function for word completion. ```bash complete -o filenames -o nospace -o bashdefault -F _comp_cd cd ``` -------------------------------- ### Display Directory Stack with One Entry Per Line Source: https://devdocs.io/bash/directory-stack-builtins The `-p` option for `dirs` prints each directory on a new line. ```bash dirs -p ```