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
Variables in LISP
Similar to other languages, in LISP variables are named places that store a particular value, but they are not declared the way you declare variables in C++ or Java i.e you don't need to mention data-type of variables when declaring them as LISP is dynamically typed. LISP supports two types of varia
3 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
Create timestamp variable in bash script
Handling timestamps is a common need in Bash scripting for a number of tasks, including file naming, data processing, and logging. Tracking events or organizing data can be made easier with the help of timestamps, which serve as a precise point-in-time representation. Creating timestamps in Bash scr
4 min read
Scope of Variable in R
In R, variables are the containers for storing data values. They are reference, or pointers, to an object in memory which means that whenever a variable is assigned to an instance, it gets mapped to that instance. A variable in R can store a vector, a group of vectors or a combination of many R obje
5 min read
Variables in Cypress
Cypress is a front-end testing tool. It allows us developers and QA engineers who build web applications using the modern JavaScript framework to set up, write, run, and debug tests. It is getting popular as it enables us to write faster, easier, and more reliable tests. Cypress can test anything th
6 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
7 min read
How to Declare a Variable in PL/SQL?
Declaring variables in PL/SQL is a fundamental step towards building powerful and efficient database applications. Variables act as placeholders for data which enable us to manipulate and store information within our PL/SQL programs. Here, we will explore various methods of declaring variables in PL
5 min read
GitLab CI/CD Variables
GitLab CI/CD variables allow you to customize your CI/CD pipelines by defining and managing key values used in the build, deployment, and operational processes. These variables act as placeholders for dynamic data, such as environment-specific settings, sensitive information like passwords, or any o
4 min read