In Bash, $1 represents the first argument of a script. This means when users provide the first argument while executing a script, it is accessible within the script by the first positional parameter ($1). In this article, primarily I will show how to provide and access the first argument. Moreover, I will try to satiate users’ various other queries related to the first argument in Bash.
How to Provide the First Argument in Shell Script?
To provide the first argument in a shell script, simply pass it as a command line argument while executing the script. For instance, if you have a script named “myscript.sh” and want to provide “Linux” as the first argument, then execute the script as follows:
./myscript.sh Linux
How to Access the First Argument in Bash Script?
To access the first argument in Bash script, use the first positional parameter $1. Here is a simple example:
#!/bin/bash
# Print the first argument
echo "First argument: $1"
This Bash script prints out the first argument passed to it. It uses the echo command to display the text “First argument: ” followed by the value of $1
, which represents the first argument provided to the script.
5 Cases of Handling First Argument in Bash Script
Bash users often need to check the existence of the first argument in a script. Moreover, it is required to modify or skip the first argument occasionally. Therefore, handling the first argument effectively is an essential skill. The following discussion will highlight a few cases for effectively managing the first argument.
1. How to Check the Existence of the First Argument in Bash
To check the existence of the first argument, compare whether the first argument ($1) is empty or not. Use an if statement with -z test operator for this purpose. Here’s an example:
#!/bin/bash
if [ -z "$1" ]; then
echo "First argument doesn’t exist or it’s empty."
else
echo "First argument exists and its value is: $1"
fi
The if statement checks the length of the first argument ($1) using the -z operator. If the length is zero, it indicates that the first argument doesn’t exist or is empty. On the other hand, a non-zero length of the first argument implies that the first argument exists.
""
or no argument is provided to the script it shows that “First argument doesn’t exist or it’s empty”. However, when the argument “a” is parsed it displays that the first argument exists.
2. How to Pass a File as the First Input Argument in Bash
Provide the file’s path as the first argument when running the script to pass a file as an input argument in a Bash script. Consider a file arg1.txt that contains some data:
#Users
Kenny
Anita
Smith
Mohammad
Now, write the following script that uses a while loop to process each line of the arg1.txt file after receiving it as the first argument:
#!/bin/bash
file="$1"
arr1=()
# Skip the first line and read the rest of the file
while read arg; do
arr1+=("$arg")
done < <(tail -n +2 "$file")
# Print the elements of arr1
echo "${arr1[@]}"
This Bash script reads the contents of a file specified as the first argument, excluding the first line. It then populates an array, arr1
, with each line from the file. The script achieves this by using process substitution <()
to provide the output of tail -n +2 "$file"
(which skips the first line of the file) as input to a while loop.
Within the loop, each line is read and appended to the “arr1” array using the +=
operator. Finally, the script prints all elements of the “arr1” array using "${arr1[@]}"
.
3. Iterate Over Input Arguments Except First One
To iterate over input arguments except the first one in a Bash script, use a loop starting from the second argument onwards. The “$@” variable holds all the command line arguments. Looping over “${@:2}” allows you to access each argument individually, starting from the second argument($2). Here is an example:
#!/bin/bash
for i in "${@:2}"
do
echo "$i"
done
This Bash script utilizes a for loop to iterate over all arguments passed to the script except for the first one. The loop iterates over the positional parameters starting from the second one using "${@:2}"
. For each iteration, it echoes the current argument ($i
). Essentially, this script prints out all arguments passed to the script, excluding the first one.
4. Change the First Argument in Bash Script
Use the set command to change the first argument in a Bash script. Rewriting the arguments after set – –, reset the command line arguments. The script below demonstrates how set built-in is used to modify the first command line argument:
#!/bin/bash
# Update the arguments as required
set -- "5" "${@:2}"
# Display the updated arguments
echo "Updated arguments: $@"
This Bash script updates its first argument by replacing it with “5” while keeping the rest unchanged. "${@:2}"
in the set command denotes all arguments except the first one. Finally, the script accesses the special variable $@
to show all the command line arguments including the modified first argument.
5. How to Skip the First Argument in Bash Script
Sometimes users want to get rid of the first argument passed to a Bash script. To skip the first argument, use the shift command to shift all the positional parameters to the left by one. Here’s how you can do it:
#!/bin/bash
# Skip the first argument
shift
# Now $1 refers to the second argument
echo "Second argument: $1"
# Loop through all remaining arguments
echo "Remaining arguments: $@"
The script uses shift to skip the first positional argument passed to the script. Therefore, the second positional argument becomes the first argument and can be accessed using $1
. Finally, it also prints all the arguments using $@
, which represents all arguments passed to the script except the first one, as the first one has been shifted out.
Conclusion
In conclusion, handling the first argument in Bash script is quite easy once you are familiar with the idea of positional parameters. The first positional parameter ($1) is used to access the first argument passed to a script. Moreover, the set built-in and shift command allows users to change or skip the first argument of a Bash script. I hope this article has helped you manipulate the first argument of your script.
People Also Ask
How to check if the first argument is empty?
To check whether the first argument is empty or not use the operators such -n or -z with the test command in an if statement. if [ -n "$1" ]
holds true if the length of the first argument is nonzero. On the other hand, if [ -z "$1" ]
is true if the length of the first argument is 0.
What is the difference between $1 and $@?
Though both $1 and $@ are used to represent command line arguments, they don’t refer to the same set of arguments. $1 is used to access the first positional parameter whereas $@ expands to all the command line arguments. One can say, “$@” is equivalent to “$1” “$2” “$3″…up to the last argument.
How to store the first argument in a variable in Bash script?
Assign $1 as the value of a variable to store the first argument in a variable within a Bash script. For instance:
#!/bin/bash
# Store the first argument in a variable
arg1=$1
Here, variable arg1 contains the value of the first argument provided to the script.
Why is $1 in a function not printing the script’s first argument?
$1 in a function can’t print a script’s first argument. Arguments provided to a script are accessible using positional parameters only within the main level of the script not in a function body written in the script. A function can take its own set of arguments independent of the parent script containing the function. So, a function written in a script can access arguments that are provided to the function while calling the function within the script.
Related Articles
- How to Check Number of Arguments in Bash? [3 Methods]
- How to Get Argument in Bash? [4 Methods]
- How to Pass Arguments to Bash Script? [5 Methods]
- How to Parse Optional Arguments in Bash Script [2 Ways]
- How to Pass Array as an Argument in Bash Function? [3 Ways]
- How to Set Default Argument in Bash [2 Methods]
- How to Use OPTARG in Bash [3 Practical Examples]
- How to Use “getopts” in Bash [Complete Guide]
- [Solved] “bash: Argument list too long” Error
<< Go Back to Argument in Bash Script | Bash Functions | Bash Scripting Tutorial
FUNDAMENTALS A Complete Guide for Beginners