In Bash, the set command modifies shell options and parameters. When you use the set command followed by a hyphen and a variable name (set – $VARIABLE), it allows you to set the positional parameters in the shell. In this article, I am going to discuss how to set – $variable in Bash scripts. So, let’s start!
Key Takeaways
- Understanding of Set – $Variable in Bash Script.
- Learning to use for loop with Set – $Variable.
Free Downloads
What is set – $Variable in Bash?
In Bash scripting, you might encounter the expression set -$VARIABLE. But what does it signify? Essentially, set-$VARIABLE serves to divide the value of a Bash Variable into separate words, utilizing the Internal Field Separator (IFS) as the separator. For instance, if VARIABLE holds the value “NY Ohio Miami“, executing “set – $VARIABLE” would assign the positional parameters as “NY“, “Ohio“, and “Miami“.
Initially, this might not appear exceedingly useful, but it can be a potent technique when applied appropriately. One prevalent application involves processing command-line arguments provided to a bash script. When executing a bash script, the positional parameters (i.e. $1, $2, etc.) represent the arguments passed to the script. By utilizing “set – $VARIABLE“, you can conveniently split a single argument into multiple words, enabling easier processing of the individual components.
2 Practical Examples to Use Set – $Variable in Bash Scripts
To understand the context fully how you can use the set – $variable command in Bash scripts, here I have provided two examples for your perusal. Check these out.
Example 1: Using of set – $VARIABLE in Bash Script
The set – $VARIABLE syntax allows you to set the positional parameters in a Bash script. Here in the below script, I will assign a string with the set – $VARIABLE command to give you a demonstration of it. This is a simple code, so you don’t worry about what comes next. Just follow the steps specified below.
❶ At first, launch an Ubuntu terminal.
❷ Write the following command to open a file in Nano:
nano set_variable_1.sh
- nano: Opens a file in the Nano text editor.
- set_variable_1.sh: Name of the file.
❸ Copy the script mentioned below:
#!/bin/bash
# Set the value of $VARIABLE
VARIABLE="Hello, World!"
# Set the positional parameters using set - $VARIABLE
set - "$VARIABLE"
# Access and display the first positional parameter
echo "First parameter: $1"
The first line #!/bin/bash specifies the interpreter to use (/bin/bash) for executing the script. The value of $VARIABLE is set to “Hello, World!“. Then, using set – “$VARIABLE“, the value of $VARIABLE is assigned as the first positional parameter ($1). Finally, the echo command displays the first positional parameter. It is worth mentioning that if I did not use the set – “$VARIABLE” in the code, I had to assign the VARIABLE value to the positional parameter ($1) manually to channel the command line argument to the output line. In this case, the alternate line would be VARIABLE=$1 instead of using set – “$VARIABLE”.
❹ Press CTRL+O and ENTER to save the file; CTRL+X exit.
❺ Use the following command to make the file executable:
chmod u+x set_variable_1.sh
- chmod: changes the permissions of files and directories.
- u+x: Here, u refers to the “user” or the owner of the file and +x specifies the permission being added, in this case, the “execute” permission. When u+x is added to the file permissions, it grants the user (owner) of the file permission to execute (run) the file.
- set_variable_1.sh: is the name of the file.
❻ Run the script by the following command:
./set_variable_1.sh
The output returns First parameter: Hello, World! Where Hello, World! acts as a positional parameter of the VARIABLE variable.
Example 2: Applying Loop on the Elements of a List Stored in a Variable
Another frequent scenario where set – $VARIABLE comes in handy is when you need to iterate over the elements of a list stored in a variable. Let’s consider an example:
You can follow the steps of example 01, to save & make the script executable.
Script (set_variable_loop.sh) >
#!/bin/bash
# Set the value of $VARIABLE
VARIABLE="apple orange banana"
# Set the positional parameters using set - $VARIABLE
set - $VARIABLE
# Loop through the positional parameters and display each one
for param in "$@"; do
echo "Fruit: $param"
done
The first line #!/bin/bash specifies the interpreter to use (/bin/bash) for executing the script. Then, The value of $VARIABLE has been set to a string containing three fruits separated by spaces: “apple orange banana“. Next, using set – $VARIABLE, the value of $VARIABLE has been assigned as the positional parameter. This operation splits the string into separate elements and assigns them to $1, $2, and $3, respectively. Finally, The for loop iterates over the positional parameters (“$@”) and displays each fruit. The loop runs three times, once for each fruit, printing “Fruit: [fruit_name]” for each iteration.
Run the script by using the following command:
./set_variable_loop.sh
As the VARIABLE variable stores the “apple orange banana” string, set – $VARIABLE fragments the string based on the whitespace and later returns Fruit: apple, Fruit: orange, and Fruit: banana using the for loop.
Assignment Task
- Create a new Bash script file named “variable_operations.sh” and then do an algebraic addition of two numbers by putting them in a variable initially. You have to use the set -$VARIABLE command to do the job.
- Suppose you have a file composed of 100 student names in it. Now your task is to list the name along with a unique number. If you already know how to crack it, share your thoughts in the comment section and disseminate your Bash knowledge to all.
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 processes command-line arguments or to loop over the elements of a list stored in a variable. If you have any questions or queries related to this article, feel free to comment below.
People Also Ask
Related Articles
- How to Declare Variable in Bash Scripts? [5 Practical Cases]
- Bash Variable Naming Conventions in Shell Script [6 Rules]
- How to Assign Variables in Bash Script? [8 Practical Cases]
- How to Assign Float to Variable in Bash Script? [2 Methods]
- How to Check Variable Value Using Bash Scripts? [5 Cases]
- How to Use Default Value in Bash Scripts? [2 Methods]
- How to Read Environment Variables in Bash Script? [2 Methods]
- How to Export Environment Variables with Bash? [4 Examples]
<< Go Back to Variable Declaration and Assignment | Bash Variables | Bash Scripting Tutorial