Feature Group
bash internal (293)
bash
This code attempts to create a lock file using the noclobber
option to prevent overwriting. If the lock file already exists, it outputs an error message, demonstrating a simple file locking mechanism in Bash.
( set -o noclobber; echo > my.lock ) || echo 'Failed to create lock file'
bash internalfile and stream operationsfile operationsfile locking
bash
This demonstrates the use of a case
statement in Bash for pattern matching and conditional execution.
case "$Variable" in # List patterns for the conditions you want to meet 0) echo "There is a zero.";; 1) echo "There is a one.";; *) echo "It is not null.";; # match everything esac
bash internalflow controltests (conditions)case statement (pattern matching)
bash
This comprehensive snippet demonstrates various parameter expansion techniques in Bash, including default value substitution, conditional assignment, error handling, and advanced string manipulation.
#foo=<value> ${foo:-val} # $foo, or val if unset (or null) ${foo:=val} # Set $foo to val if unset (or null) ${foo:+val} # val if $foo is set (and not null) ${foo:?message} # Show error message and exit if $foo is unset (or null) ${foo%suffix} # Remove suffix ${foo#prefix} # Remove prefix ${foo%%suffix} # Remove long suffix ${foo/%suffix} # Remove long suffix ${foo##prefix} # Remove long prefix ${foo/#prefix} # Remove long prefix ${foo/from/to} # Replace first match ${foo//from/to} # Replace all ${foo/%from/to} # Replace suffix ${foo/#from/to} # Replace prefix
bash internaldata manipulationsstring manipulation and expansionsparameter expansion
bash
This demonstrates the use of brace expansion in Bash to generate sequences of numbers and letters, while noting the limitation of not being able to use variables within brace expansions.
echo {1..10} # => 1 2 3 4 5 6 7 8 9 10 echo {a..z} # => a b c d e f g h i j k l m n o p q r s t u v w x y z # This will output the range from the start value to the end value. # Note that you can't use variables here: from=1 to=10 echo {$from..$to} # => {$from..$to} echo {{1..3},{7..9}} # => 1 2 3 7 8 9
bash internaldata manipulationsstring manipulation and expansionsparameter expansionbrace expansion
bash
This snippet demonstrates file path manipulation using parameter expansion in Bash, including removing file extensions, extracting directory paths, getting file extensions, and replacing substrings.
str="/path/to/foo.cpp" echo "$str" # /path/to/foo (variable expansion) echo "${str}" # /path/to/foo (variable expansion) echo '$str' # '$str' (disabled variable expansion) echo "${str%.cpp}" # /path/to/foo echo "${str%.cpp}.o" # /path/to/foo.o echo "${str%/*}" # /path/to echo "${str##*.}" # cpp (extension) echo "${str##*/}" # foo.cpp (basepath) echo "${str#*/}" # path/to/foo.cpp echo "${str##*/}" # foo.cpp echo "${str/foo/bar}" # /path/to/bar.cpp
bash internaldata manipulationsstring manipulation and expansionsparameter expansionpath manipulation
bash
This demonstrates string substitution using parameter expansion to replace the first occurrence of a substring.
echo "${variable/Some/A}" # => A string # This will substitute the first occurrence of "Some" with "A".
bash internaldata manipulationsstring manipulation and expansionsparameter expansionstring substitution