In the world of bash scripting, you may come across the phrase “set – $VARIABLE”. But what does it mean?
At its most basic, “set – $VARIABLE” is used to split the value of a bash variable into separate words, using the Internal Field Separator (IFS) as the delimiter. For example, if VARIABLE has the value “a b c”, then running “set – $VARIABLE” would set the positional parameters to “a”, “b”, and “c”.
This may not seem particularly useful at first glance, but it can be a powerful tool when used in the right context. One common use case is to process command-line arguments passed to a bash script. When you run a bash script, the positional parameters (i.e., $1, $2, etc.) represent the arguments passed to the script. By using “set – $VARIABLE”, you can easily split a single argument into multiple words, allowing you to process them more easily.
Here’s an example of how this might be used:
#!/bin/bash
# Set the value of VARIABLE to the first command-line argument
VARIABLE="$1"
# Split the value of VARIABLE into separate words
set - $VARIABLE
# Loop over the words
for word in "$@"; do
echo "$word"
done
If you save this script as “example.sh” and run it like this:
./example.sh "a b c"
Output:
a
b
c
Loop over the elements of a list stored in a variable
Another common use case for “set – $VARIABLE” is to loop over the elements of a list stored in a variable. For example:
# Set the value of VARIABLE to "a b c"
VARIABLE="a b c"
# Split the value of VARIABLE into separate words
set - $VARIABLE
# Loop over the words
for word in "$@"; do
echo "$word"
done
Output:
a
b
c
It’s worth noting that “set – $VARIABLE” only works if the value of VARIABLE is a single string. If VARIABLE is an array, you’ll need to use a different approach. One option is to use the “printf ‘%s\n’ “${VARIABLE[@]}” syntax, which expands the array into a series of separate words, each separated by a new line character.
Here’s an example of how this might be used:
#!/bin/bash
# Set the value of VARIABLE to an array containing "a", "b", and "c"
VARIABLE=("a" "b" "c")
# Expand the array into a series of separate words
set - $(printf '%s\n' "${VARIABLE[@]}")
# Loop over the words
for word in "$@"; do
echo "$word"
done
Output:
a
b
c
Conclusion
In conclusion, “set – $VARIABLE” is a useful bash feature that allows you to split the value of a variable into separate words, using the IFS as the delimiter. It can be used to process command-line arguments or to loop over the elements of a list stored in a variable. While it only works with single strings, there are alternative approaches that can be used with arrays. Understanding how “set – $VARIABLE” works and when to use it can be a valuable addition to your bash scripting toolkit.
Similar Reads
SET Variable in MariaDB
In MariaDB, the SET statement is a main tool in variable handling. Users can assign values to variables, operate with them, and control database operations in various respects. This article includes a look at the SET variable usage in MariaDB and its syntax together with some examples. SET Variable
4 min read
Variable in Maths
A variable is like a placeholder or a box that can hold different values. In math, it's often represented by a letter, like x or y. The value of a variable can change depending on the situation. For example, if you have the equation y = 2x + 3, the value of y depends on the value of x. So, if you ch
5 min read
Global Variables in MATLAB
Variables in programming are generally storage spaces to store a certain type of data. There are many types of variables, but two commonly used types are local and Global variables. Generally, each MATLAB function has its own local variables. But sometimes for the sake of programming, we need to cha
4 min read
Batch Script - Environment Variables
Environment variables are a collection of dynamic named values that are saved on the system and utilized by programs that are running in shells or subshells on Linux and Unix-based systems. An environment variable is, to put it simply, a variable with a name and a corresponding value. You can alter
6 min read
How to Declare a Variable in SQL?
Variables in SQL are fundamental for building efficient and scalable database applications. They enhance the flexibility and efficiency of database queries by acting as placeholders for data. Understanding how to declare and use variables in SQL is crucial for writing dynamic and effective queries I
3 min read
How to represent a variable in Less ?
LESS stands for Leaner Style Sheets. It is a backward-compatible language extension for CSS. One of its features is that it lets you use variables in the style sheet. Variables can be used to store CSS values that may be reused. This makes it easier for the user by letting them define commonly used
2 min read
Shell Scripting - Local Variables
The shell is a command-line interpreter for Linux and Unix systems. It provides an interface between the user and the kernel and executes commands. A sequence of commands can be written in a file for execution in the shell. It is called shell scripting. It helps to automate tasks in Linux. There are
3 min read
Special variables in Perl
Special Variables in Perl are those which are already defined to carry out a specific function when required. The differentiating factor between a special Variable in Perl and a random character is the use of Punctuation mark after the variable, these could be @, $ or %, etc, for example, $_. Perl V
6 min read
SAP ABAP | Understanding Variables
What is a Variable?Variable are named data objects that refer to the memory location allocated during the program execution for processing. As the name indicates, users can use refer to the range of values that may be stored inside and the range of actions that can be carried out on the variable. Sy
7 min read
Bash Scripting - How to check If variable is Set
In BASH, we have the ability to check if a variable is set or not. We can use a couple of arguments in the conditional statements to check whether the variable has a value. Â In this article, we will see how to check if the variable has a set/empty value using certain options like -z, -v, and -n. Usi
10 min read