bash
Extract the last 3 characters from the variable `foo`. This demonstrates how to use substring expansion in Bash to get a specific portion of a string.
bash
Extract the last 3 characters from the variable `foo`. This demonstrates how to use substring expansion in Bash to get a specific portion of a string.
bash
Extract the first 3 characters from the variable `foo`. This demonstrates how to get a substring from a variable in Bash.
bash
This snippet demonstrates how to create a multi-line comment in Bash using the `:` (colon) command followed by a single-quoted block. This is a common technique for adding comments that span multiple lines in Bash scripts.
bash
This code demonstrates prefix name expansion in Bash. It uses `${!prefix_*}` to list all variable names that start with `prefix_`. In this case, it outputs `prefix_a prefix_b`, showing the variables `prefix_a` and `prefix_b` that match the prefix pattern. This is useful for dynamically accessing or listing variables with a common naming pattern.
echo
bash
Remove the `prefix` from the beginning of the variable `foo`. This demonstrates parameter expansion to strip a prefix from a string.
bash
Extract a substring from the variable `name` starting at index 0 with a length of 2 characters. This demonstrates basic string manipulation in Bash.
echo
bash
This code demonstrates variable indirection in Bash. It assigns the value `joe` to the variable `name`, then uses a pointer variable to reference `name` and prints its value using `${!pointer}`. This shows how to indirectly access the value of a variable through another variable.
echo
bash
Extract the base filename (`foo.cpp`) and directory path (`/path/to/`) from a given source file path (`/path/to/foo.cpp`) using parameter expansion. This demonstrates basic string manipulation in Bash to separate file paths into components.
bash
This snippet demonstrates various string manipulations in Bash using parameter expansion. It shows how to remove or replace parts of a string, such as removing a file extension (`%.cpp`), extracting the directory path (`%/*`), getting the file extension (`##*.`), and replacing a substring (`/foo/bar`). This is a basic example of string manipulation in Bash.
bash
Extract a substring from a string in Bash. The first example extracts `"world"` starting at index 6 with a length of 5. The second example uses negative indexing to achieve the same result. This demonstrates basic string manipulation in Bash.
bash
This snippet demonstrates various string manipulation techniques in Bash, including substitution, slicing, and default value assignment. It shows how to modify and extract parts of a string using parameter expansion. This is an example of basic string operations in Bash.
bash
Check if a string is empty or not using `-z` and `-n` conditional expressions. If the string is empty, it prints "String is empty"; otherwise, it prints "String is not empty". This demonstrates basic string conditionals in Bash.
echo
bash
Enable strict mode in Bash by setting `-e` (exit on error), `-u` (treat unset variables as an error), and `-o pipefail` (return the exit status of the last command in a pipeline that fails). Additionally, set the `IFS` (Internal Field Separator) to newline and tab to prevent word splitting issues. This demonstrates how to make Bash scripts more robust and error-resistant.
bash
This code defines a function `get_name` that returns the string "John". It then uses command substitution to call this function and embed its output in a string, resulting in the output "You are John". This demonstrates the use of functions and command substitution in Bash.
echo
bash
This code prints the current working directory using two different methods: `$(pwd)` and `` `pwd` ``. Both methods execute the `pwd` command and embed its output into the string. This demonstrates shell command execution within a string.
pwd
bash
This is an example script with a hashbang line (`#!`) for Bash and how to load definitions from the `/root/.bashrc` file. This is a useful example of how to start scripts for cronjob tasks with access to root environment variables.
source
bash
This demonstrates using wildcard characters and command-line options with the `cp` command.
cp
bash
The `type` command determines whether a given command is a built-in, external binary, alias, or function. This snippet checks the type of the specified command.
bash
The code prompts the user to enter a password (from standard input) and stores the input in the environment variable PASSWORD.
bash
The code exports the content read from standard input (stdin) as the environment variable PASSWORD.