### Set Installation Directory with DESTDIR Source: https://github.com/bminor/bash/blob/master/doc/bashref.html This example demonstrates how to specify a DESTDIR variable to simulate an installation without actually modifying the system. It creates a directory and then runs 'make install' with DESTDIR set to this directory. The output will be placed under this path, allowing for inspection before final installation. ```bash mkdir /fs1/bash-install make install DESTDIR=/fs1/bash-install ``` -------------------------------- ### Bash Basic Installation Steps Source: https://github.com/bminor/bash/blob/master/doc/bashref.html This snippet outlines the fundamental commands required to configure, compile, test, and install Bash from its source code. It emphasizes running `configure`, `make`, `make tests`, and `make install` in sequence. ```shell cd /path/to/bash/source ./configure make make tests make install ``` -------------------------------- ### C Loadable TOPS-20 Push Example Source: https://github.com/bminor/bash/blob/master/examples/INDEX.txt A loadable C module ('push') that serves as an example, possibly related to the 'push' functionality reminiscent of TOPS-20 systems. It demonstrates a specific command behavior. ```c // Anyone remember TOPS-20? // push.c ``` -------------------------------- ### Configure with --prefix Option Source: https://github.com/bminor/bash/blob/master/doc/bashref.html This command shows how to set the installation prefix during the configure stage. The --prefix=PATH option tells the configure script the desired base directory for installation, which will be used by 'make install' subsequently. ```bash ./configure --prefix=/path/to/installation ``` -------------------------------- ### Install MO Files with TEXTDOMAINDIR and LC_MESSAGES Source: https://github.com/bminor/bash/blob/master/doc/bashref.html This example demonstrates installing compiled Message Object (.mo) files into a structured directory that respects language codes and message domains. It utilizes the TEXTDOMAINDIR and LC_MESSAGES environment variables to specify the installation path, ensuring that gettext can locate the correct translation files based on user locale settings. ```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 ``` -------------------------------- ### Bash: Basic Compilation and Installation Source: https://context7.com/bminor/bash/llms.txt Provides the fundamental commands to compile and install the Bash shell from its source code. This includes configuring the build, compiling the source, running tests, and finally installing the compiled binaries and associated files. ```bash # Basic compilation cd /path/to/bash-5.3 ./configure make make test sudo make install ``` -------------------------------- ### Bash Brace Expansion: Basic Example Source: https://github.com/bminor/bash/blob/master/doc/bash.html A fundamental example of brace expansion, showing how a common prefix and suffix can be combined with comma-separated alternatives within braces to generate distinct strings. ```bash echo a**{**d,c,b**}**e ``` -------------------------------- ### Install Bash Completion Function Source: https://github.com/bminor/bash/blob/master/doc/bashref.html Example of how to install a custom Bash completion function using the 'complete' builtin with the '-F' option. This directive tells the readline library to use the specified function for providing completions for a given command. ```bash # Tell readline to quote appropriate and append slashes to directories; complete -F _comp_cd cd ``` -------------------------------- ### Bash: Pattern Substitution Example (Replacement) Source: https://github.com/bminor/bash/blob/master/doc/bashref.html Demonstrates basic pattern substitution and how the replacement string is expanded. Shows the effect of quoting. ```bash var=abcdef rep='& ' echo ${var/abc/& } echo "${var/abc/& }" echo ${var/abc/$rep} echo "${var/abc/$rep}" ``` -------------------------------- ### C Loadable Hello World Module Source: https://github.com/bminor/bash/blob/master/examples/INDEX.txt An obligatory 'Hello World' example implemented as a loadable C module. This serves as a basic template and demonstration for creating loadable builtins. ```c // Obligatory "Hello World" / sample loadable. // hello.c ``` -------------------------------- ### C-style Hello World Loadable Source: https://github.com/bminor/bash/blob/master/examples/INDEX.html This is an obligatory 'Hello World' example demonstrating a loadable builtin in C. It serves as a basic template for creating new loadable modules. ```c // Obligatory "Hello World" / sample loadable. ``` -------------------------------- ### Bash Readline Key Binding Examples Source: https://github.com/bminor/bash/blob/master/doc/bashref.html Examples demonstrating how to bind keys to Readline functions or macros in an initialization file. This includes binding to function names, simple text macros, and sequences. It also showcases escaping special characters within macro definitions. ```bash Control-u: universal-argument Meta-Rubout: backward-kill-word Control-o: "> output" "\C-u": universal-argument "\C-x\C-r": re-read-init-file "\e\[11~": "Function Key 1" "\C-x\\": "\\" ``` -------------------------------- ### Specify Execution Prefix with make install Source: https://github.com/bminor/bash/blob/master/doc/bashref.html This command demonstrates how to specify a separate installation prefix for executable files using the 'exec_prefix' variable with 'make install'. This allows for architecture-specific files to be installed in a different location than architecture-independent files. ```bash make install exec_prefix=/ ``` -------------------------------- ### Specify Installation Prefix with make install Source: https://github.com/bminor/bash/blob/master/doc/bashref.html This command shows how to override the default installation prefix during the 'make install' process. By specifying 'prefix=PATH', you can change where Bash and its associated files are installed. This example sets the installation prefix to '/'. ```bash make install prefix=/ ``` -------------------------------- ### C-style Loadable Builtin Template Source: https://github.com/bminor/bash/blob/master/examples/INDEX.html This C file serves as an example template for creating loadable builtins in Bash. It provides a structure that developers can adapt for their own custom functions. ```c // Example template for loadable builtin. ``` -------------------------------- ### C Loadable Builtin Template Source: https://github.com/bminor/bash/blob/master/examples/INDEX.txt An example template file ('template.c') for creating loadable builtins in C. Provides a basic structure and placeholders for developing new loadable modules. ```c // Example template for loadable builtin. // template.c ``` -------------------------------- ### Bash Loadable Builtin Example (C) Source: https://context7.com/bminor/bash/llms.txt An example C code snippet demonstrating how to create a loadable builtin for Bash. It includes functions for plugin initialization, cleanup, the builtin's core logic, and a descriptor structure. This code requires Bash development headers for compilation. ```c /* Example loadable builtin: examples/loadables/template.c */ #include "config.h" #include "builtins.h" #include "shell.h" #include "bashgetopt.h" /* Plugin initialization */ int my_plugin_builtin_load(char *name) { printf("Loading plugin: %s\n", name); return 1; /* Success */ } /* Plugin cleanup */ void my_plugin_builtin_unload(char *name) { printf("Unloading plugin: %s\n", name); } /* Builtin implementation */ int my_plugin_builtin(WORD_LIST *list) { int opt; reset_internal_getopt(); while ((opt = internal_getopt(list, "h")) != -1) { switch (opt) { case 'h': printf("Usage: my_plugin [-h] [args]\n"); return EXECUTION_SUCCESS; default: builtin_usage(); return EX_BADUSAGE; } } list = loptend; /* Process arguments */ while (list) { printf("Arg: %s\n", list->word->word); list = list->next; } return EXECUTION_SUCCESS; } /* Builtin descriptor */ char *my_plugin_doc[] = { "My plugin builtin command.", "", "Does something useful with the arguments.", (char *)NULL }; struct builtin my_plugin_struct = { "my_plugin", /* name */ my_plugin_builtin, /* function */ BUILTIN_ENABLED, /* flags */ my_plugin_doc, /* long_doc */ "my_plugin [-h] [args]", /* short_doc */ (char *)0 /* handle */ }; /* Compile loadable builtin: gcc -fPIC -shared -O2 \ -I/usr/local/include/bash \ -I/usr/local/include/bash/include \ -o my_plugin.so my_plugin.c Load in bash: enable -f ./my_plugin.so my_plugin my_plugin arg1 arg2 Unload: enable -d my_plugin */ ``` -------------------------------- ### Bash ksh Environment Emulation Source: https://github.com/bminor/bash/blob/master/examples/INDEX.txt Provides Bash functions and aliases to create a ksh-like environment. This aims to offer users a familiar starting point for shell scripting if they are accustomed to ksh. ```bash # Functions and aliases for a ksh environment # kshenv ``` -------------------------------- ### Emacs Mode Key Bindings Example Source: https://github.com/bminor/bash/blob/master/doc/bashref.html This is a sample inputrc configuration demonstrating key bindings for Emacs mode. It includes bindings for 'backward-kill-word' and various arrow key sequences for navigation and history. ```inputrc 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 ``` -------------------------------- ### Bash Brace Expansion Example Source: https://github.com/bminor/bash/blob/master/doc/bashref.html Demonstrates basic brace expansion with comma-separated strings. This feature generates strings by combining a common prefix and suffix with provided alternatives. ```bash echo a{d,c,b}e ``` -------------------------------- ### Bash Installation with Separate Build Directory Source: https://github.com/bminor/bash/blob/master/doc/bashref.html This shows how to compile Bash in a directory distinct from the source code, useful for multi-architecture builds. It involves creating a new directory, navigating into it, and then running the `configure` script with the source path. ```shell mkdir /usr/local/build/bash-4.4 cd /usr/local/build/bash-4.4 bash /usr/local/src/bash-4.4/configure make ``` -------------------------------- ### Ksh Substring Emulation (Bash) Source: https://github.com/bminor/bash/blob/master/examples/INDEX.html These functions emulate the ancient Ksh builtin for substring extraction in Bash. They provide a way to get a portion of a string based on start index and length. ```bash #!/bin/bash # A function to emulate the ancient ksh builtin ``` ```bash #!/bin/bash # A function to emulate the ancient ksh builtin ``` -------------------------------- ### Exporting PS1 in Bash Source: https://github.com/bminor/bash/blob/master/doc/article.txt Demonstrates exporting the PS1 variable in Bash. By exporting PS1, its value is made available to child shells, ensuring that nested shells inherit the prompt customization. The example shows the prompt after exporting PS1 and starting a new Bash instance. ```shell export PS1 # Output after starting a new bash shell: # chet@odin [21:17:40] src(2:639)$ ``` -------------------------------- ### Bash help: Display command descriptions and synopses Source: https://github.com/bminor/bash/blob/master/doc/bashref.html The `help` command displays descriptions of patterns, either as short usage synopses or detailed manpage-like formats. It supports pattern matching for help topics and returns a zero status if a command matches, otherwise non-zero. ```bash help help -s help pattern help topic_name ``` -------------------------------- ### Get Array Element Length in Bash Source: https://github.com/bminor/bash/blob/master/doc/bashref.html Gets the length of a specific element in an indexed array or the total number of elements in an array. `${#name[subscript]}` gives the element length, and `${#name[@]}` gives the total count. ```bash echo ${#my_array[0]} echo ${#my_array[@]} ``` -------------------------------- ### Configure with --exec-prefix Option Source: https://github.com/bminor/bash/blob/master/doc/bashref.html This command illustrates how to specify a separate installation prefix for executable files during the configure stage using the --exec-prefix=PATH option. This allows for distinct locations for programs and libraries compared to documentation and data files. ```bash ./configure --exec-prefix=/path/to/executables ``` -------------------------------- ### Vi-style Key Binding for Universal Argument Source: https://github.com/bminor/bash/blob/master/doc/bash.html This example shows how to set up a key binding in the .inputrc file for the 'universal-argument' Readline command using a vi-style notation. This binding enables users to input numeric arguments for Readline commands. ```inputrc C-Meta-u: universal-argument ``` -------------------------------- ### Bash Sequence Generation Source: https://github.com/bminor/bash/blob/master/examples/INDEX.txt Bash functions 'seq' and 'seq2' for generating sequences of numbers. They allow specifying a start and end point, with a default start of 1. Useful for loops and numerical series. ```bash # Generate a sequence from m to n, m defaults to 1 # seq ``` ```bash # Generate a sequence from m to n, m defaults to 1 # seq2 ``` -------------------------------- ### Shellmath: Example of Decimal Addition and Output Capture in Bash Source: https://github.com/bminor/bash/blob/master/examples/shellmath/README.md This example demonstrates how to use `_shellmath_add` with decimal and scientific notation inputs, and then capture the result using `_shellmath_getReturnValue` into a variable named `sum`. ```bash _shellmath_add 1.009 4.223e-2 _shellmath_getReturnValue sum echo "The sum is $sum" ``` -------------------------------- ### Bash ksh-like 'cd' Function Source: https://github.com/bminor/bash/blob/master/examples/INDEX.txt Implements a 'cd' command in Bash that mimics the behavior of ksh's 'cd', including support for options like '-LP' and optional directory arguments. Provides enhanced directory navigation capabilities. ```bash # ksh-like 'cd': cd [-LP] [dir [change]] # ksh-cd ``` -------------------------------- ### Shellmath: Example of Multiplication and Output Capture (Factorial) in Bash Source: https://github.com/bminor/bash/blob/master/examples/shellmath/README.md This example calculates 6 factorial using `_shellmath_multiply` with multiple arguments and captures the result into the `sixFactorial` variable. It highlights the arbitrary arity of multiplication. ```bash _shellmath_multiply 1 2 3 4 5 6 _shellmath_getReturnValue sixFactorial echo "6 factorial is $sixFactorial" ``` -------------------------------- ### Bash Whatis Command Implementation Source: https://github.com/bminor/bash/blob/master/examples/INDEX.html This script implements the 'whatis(1)' command, a builtin from the 10th Edition Unix sh. It provides a way to display a one-line description of a command. ```bash #!/bin/bash # An implementation of the 10th Edition Unix sh builtin 'whatis(1)' command ``` -------------------------------- ### Bash History Expansion Examples Source: https://github.com/bminor/bash/blob/master/doc/article.txt Examples demonstrating various forms of command history expansion in Bash. These allow users to recall and reuse previous commands with different modifications. The '!' character typically initiates history expansion. ```bash $ echo a b c d e a b c d e $ !! f g h i echo a b c d e f g h i a b c d e f g h i $ !-2 echo a b c d e a b c d e $ echo !-2:1-4 echo a b c d a b c d ``` -------------------------------- ### Background Job Execution - Bash Source: https://github.com/bminor/bash/blob/master/doc/bash.html The 'bg' command resumes suspended jobs in the background, similar to starting them with an ampersand (&). If no jobspec is provided, it operates on the current job. It requires job control to be enabled and returns an error if the jobspec is not found or the job was not started with job control. ```bash bg [jobspec ...] ``` -------------------------------- ### C Loadable Directory Creation Source: https://github.com/bminor/bash/blob/master/examples/INDEX.txt A loadable C module ('mkdir') for creating directories. It provides the functionality to make new directories in the file system. ```c // Make directories. // mkdir.c ``` -------------------------------- ### Bash: Installing to a Custom Staging Location Source: https://context7.com/bminor/bash/llms.txt Shows how to install the compiled Bash into a custom directory using the `DESTDIR` variable. This is commonly used for creating staging directories for packaging or deployment, ensuring files are placed relative to a specified root. ```bash # Install to custom location make install DESTDIR=/tmp/staging ``` -------------------------------- ### Bash: Custom Installation Prefix Configuration Source: https://context7.com/bminor/bash/llms.txt Illustrates how to specify a custom directory for installing the Bash shell. The `./configure --prefix=/usr/local/bash` command ensures that the compiled Bash and its related files are placed in the specified location, rather than the default system directories. ```bash # Custom installation prefix ./configure --prefix=/usr/local/bash make sudo make install ``` -------------------------------- ### Listing Readline Key Bindings (Bash Command) Source: https://github.com/bminor/bash/blob/master/doc/bashref.html Demonstrates how to list current Readline key bindings using the `bind` command in Bash. `bind -P` provides a full list, while `bind -p` offers a terse format suitable for inputrc files. These commands help in understanding and managing key mappings. ```bash bind -P bind -p ``` -------------------------------- ### Ksh Whence Emulation (Bash) Source: https://github.com/bminor/bash/blob/master/examples/INDEX.html This script provides an almost Ksh-compatible emulation of the 'whence(1)' command in Bash. It helps determine the type and location of commands. ```bash #!/bin/bash # An almost-ksh compatible 'whence(1)' command ``` -------------------------------- ### Create Build Tree with mkclone Script Source: https://github.com/bminor/bash/blob/master/doc/bashref.html This command uses the 'mkclone' script to create a build directory with symbolic links to the source files. This is useful for systems that do not support the VPATH variable in make. The script requires an existing Bash installation. ```bash bash /usr/gnu/src/bash-2.0/support/mkclone -s /usr/gnu/src/bash-2.0 . ``` -------------------------------- ### C-style System Information Source: https://github.com/bminor/bash/blob/master/examples/INDEX.html This C module prints system information, similar to the 'uname' command. It provides details about the operating system and hardware. ```c // Print system information. ``` -------------------------------- ### Bash: Pattern Substitution Example (Literal &) Source: https://github.com/bminor/bash/blob/master/doc/bashref.html Illustrates how to use backslashes to escape special characters like '&' in the replacement string for pattern substitution. ```bash var=abcdef rep='& ' echo ${var/abc/\& } echo "${var/abc/\& }" echo ${var/abc/"& "} echo ${var/abc/"$rep"} ``` -------------------------------- ### Bash Redirection Order Example Source: https://github.com/bminor/bash/blob/master/doc/bash.html Demonstrates how the order of redirection operators affects the outcome. Redirections are processed from left to right, influencing which stream is redirected where. ```bash # Redirects both stdout and stderr to 'dirlist' ls > dirlist 2>&1 # Redirects stderr to stdout (which is then redirected to 'dirlist') ls 2>&1 > dirlist ``` -------------------------------- ### Bash: Regular Expression Operator Precedence Source: https://github.com/bminor/bash/blob/master/doc/bashref.html Provides examples of common logical operators used in Bash conditional expressions, illustrating their precedence and short-circuiting behavior. ```bash # Grouping and overriding precedence ( expression ) # Logical NOT ! expression # Logical AND (short-circuiting) expression1 && expression2 # Logical OR (short-circuiting) expression1 || expression2 ``` -------------------------------- ### Bash which Command Emulation (FreeBSD) Source: https://github.com/bminor/bash/blob/master/examples/INDEX.txt Provides an emulation of the 'which(1)' command as it appears in FreeBSD. This utility searches the user's PATH to find the full path of executable commands. ```bash # Emulation of 'which(1)' from FreeBSD # which ``` -------------------------------- ### Bash: Cross-Compilation Setup Source: https://context7.com/bminor/bash/llms.txt Details the configuration steps for cross-compiling Bash, which is necessary when building for a different architecture than the host system. The `--host` and `--build` options specify the target and build environments, respectively. ```bash # Cross-compilation ./configure --host=arm-linux-gnueabihf \ --build=x86_64-linux-gnu ``` -------------------------------- ### Bash Brace Expansion: Sequence Expression with Increment Source: https://github.com/bminor/bash/blob/master/doc/bash.html Example of using a sequence expression with a custom increment value. This generates terms with a specified step difference. ```bash echo {1..10..2} ``` ```bash {10..0..-2} ``` -------------------------------- ### C Example: Registering and Generating Bash Completions Source: https://context7.com/bminor/bash/llms.txt Demonstrates how to create a `COMPSPEC` to register file and directory completions for a command named 'mycommand'. It also shows the `gen_command_matches` function which generates completion suggestions by checking against builtins, shell functions, and external commands from the system's PATH. ```c /* Register completion for command */ COMPSPEC *cs = compspec_create(); cs->actions = CA_FILE | CA_DIRECTORY; cs->options = COPT_FILENAMES; progcomp_insert("mycommand", cs); /* Generate completions */ STRINGLIST *gen_command_matches(const char *text) { STRINGLIST *matches = strlist_create(0); struct builtin *builtin; SHELL_VAR **funcs; /* Add builtin commands */ for (builtin = shell_builtins; builtin->name; builtin++) { if (strncmp(builtin->name, text, strlen(text)) == 0) { strlist_append(matches, builtin->name); } } /* Add function names */ funcs = all_shell_functions(); for (int i = 0; funcs[i]; i++) { if (strncmp(funcs[i]->name, text, strlen(text)) == 0) { strlist_append(matches, funcs[i]->name); } } /* Add external commands from PATH */ char **commands = programmable_completions( "command", text, 0, 0, &matches ); return matches; } ``` -------------------------------- ### Emacs Mode Conditional Variable Setting Source: https://github.com/bminor/bash/blob/master/doc/bashref.html This example illustrates how to conditionally set a Readline variable when the editing mode is 'emacs'. This is useful for applying specific settings only when in Emacs mode. ```inputrc $if mode=emacs set editing-mode emacs $endif ``` -------------------------------- ### Ksh Environment for Bash Source: https://github.com/bminor/bash/blob/master/examples/INDEX.html This script provides functions and aliases to create a basic Ksh environment within Bash. It aims to offer a more familiar interface for users accustomed to Ksh. ```bash #!/bin/bash # Functions and aliases to provide the beginnings of a ksh environment for bash ``` -------------------------------- ### Readline Version Conditional Initialization Source: https://github.com/bminor/bash/blob/master/doc/bashref.html This example demonstrates how to conditionally set a Readline variable based on the version of the Readline library being used. It checks if the version is greater than or equal to 7.0. ```inputrc $if version >= 7.0 set show-mode-in-prompt on $endif ``` -------------------------------- ### Emacs-style Key Binding for Universal Argument Source: https://github.com/bminor/bash/blob/master/doc/bash.html This example demonstrates how to configure a key binding in the .inputrc file to execute the 'universal-argument' Readline command using Emacs-style notation. This allows users to specify numeric arguments for Readline commands. ```inputrc M-Control-u: universal-argument ```