bash
Check if a file or directory exists using the `-e` flag in a conditional expression. This demonstrates how to test for the existence of a file or directory in Bash.
bash
Check if a file or directory exists using the `-e` flag in a conditional expression. This demonstrates how to test for the existence of a file or directory in Bash.
bash
Check if the `noclobber` option is enabled in the shell. This option prevents overwriting existing files with output redirection. This demonstrates how to test shell options using the `-o` flag in a conditional expression.
bash
This code checks if two strings are not equal using the `[[ ... ]]` conditional expression in Bash. It demonstrates string comparison in a conditional context.
bash
Check if the `STRING` on the left matches the `STRING` on the right. This demonstrates basic string comparison using double square brackets `[[ ]]` in Bash.
bash
Check if `STRING` is non-empty. This demonstrates using the `-n` condition in Bash to test if a string has a non-zero length.
bash
Check if `STRING` is empty using the `-z` conditional expression in Bash. This demonstrates how to test for an empty string in a conditional statement.
bash
`$_` is a special variable in Bash that holds the last argument of the previous command. This can be useful for reusing the last argument in subsequent commands. This demonstrates how to access and reuse the last argument from the previous command.
bash
The `$#` variable in Bash holds the number of arguments passed to a script or function. This snippet demonstrates how to access the count of arguments provided.
bash
This code iterates over a range of numbers from 1 to 5 and prints a welcome message for each number. This demonstrates the use of ranges in Bash loops.
bash
This code iterates over a range of numbers from 5 to 50, incrementing by 5 each time, and prints a welcome message with the current number. This demonstrates the use of ranges with a specified step size in Bash.
bash
This code defines a Bash function `myfunc` that assigns a value to a local variable `myresult` and then echoes it. This demonstrates how to return a value from a function in Bash by echoing it.
bash
Call the `myfunc` function with the argument `"John"`. This demonstrates how to invoke a function in Bash with a specific argument.
bash
Define a function `myfunc` that takes one argument and prints "hello" followed by the argument. This demonstrates an alternate syntax for defining functions in Bash.
echo
bash
Define a Bash function `myfunc` that takes one argument and prints "hello" followed by the argument. This demonstrates how to define a simple function in Bash.
bash
Read and print each line from `file.txt` using a `while` loop. This demonstrates how to process a file line by line in Bash.
read
bash
This snippet demonstrates the use of `${foo:+val}` in Bash, which substitutes `val` if the variable `foo` is set and not null. This is a way to provide alternative values based on the existence of a variable.
bash
This snippet demonstrates the use of the `${var:?message}` syntax in Bash, which checks if the variable `foo` is set and not null. If `foo` is unset or null, the script will exit with an error message `message`. This is useful for enforcing mandatory parameters or variables in scripts.
bash
This code demonstrates string manipulation in Bash using parameter expansion. It shows how to convert the first character of a string to lowercase (`${str,}`), convert the entire string to lowercase (`${str,,}`), convert the first character to uppercase (`${str^}`), and convert the entire string to uppercase (`${str^^}`). This is an example of case conversion in Bash.
echo
bash
Get the length of the variable `foo`. This demonstrates how to retrieve the length of a string stored in a variable in Bash.
bash
Remove the suffix `suffix` from the variable `foo`. This demonstrates parameter expansion in Bash to manipulate strings by removing a specified suffix.