Use the Lines of a File as Arguments of a Linux Command
Last Updated :
07 Feb, 2024
Handling multiple arguments for a command can be difficult, especially when there are a lot of parameters. One way to make this easier is to put the arguments in a text file and use each line of the file as an argument for the command. This can simplify the process and save time.
For example, let's say you have a text file with a list of 100 client names, and you want to create a folder for each client. Writing mkdir <client_name> for 100 clients is not a productive approach. Instead, What if we could pass the text file of client names as an argument to the mkdir command? This article will explore ways to do this.
There are three methods to operate. Let's understand them one by one :
We have a list of clients, stored in a file named clients.txt and we want to create a folder for each client. So instead of giving the name of every client in the mkdir command, we'll pass the file 'clients.txt' as an argument to the mkdir command using the redirection operator '<'.
Syntax:
command `<FILE_NAME`
You can replace the command with the operation that you want to perform. And FILE_NAME with the name of the file you want to use as an argument.
Command:
mkdir `<clients.txt`
Output:
Creating a folder for each client using the shell redirection operatorWe can see after running the command mkdir `< clients.txt` 4 folders for each client Robin, Bob, Martin, and Jack are created. The < operator instructs the shell to read the contents of clients.txt and feed it as input to mkdir.
Note: We can rewrite the above command without backticks as mkdir $(< clients.txt) it will give same output.
Rewriting the command insideLimitation of shell input redirection
The shell redirection operator '<' has a limitation, it does not care about the spaces or new lines, to be specific it doesn't have any option to set a delimiter for the input.
In clients.txt we have full names of the clients like "Alison George", "Zora Hood", when we'll execute the same command it will generate 4 separate folders named "Alison", "George", "Zora" and "Good", but we want two folders for each client.
Limitation of shell redirection operatorXargs command overcomes this limitation of shell input redirection.
2. Using xargs command
Syntax:
xargs [options] command
The xargs command allows us to specify a delimiter on the redirected input using -d option. This capability is useful when you need precise control over how input data is separated and processed. Thus for creating folders with full names the command will be,
Command:
xargs -d '\n' mkdir < clients.txt

Explanation:
- < clients.txt: It tells the shell to take the contents of the clients.txt file as the input for the xargs command.
- xargs command: reads the contents of the clients.txt passed by '<' operator.
- -d '\n': tells xargs to use a newline character as the delimiter.
- mkdir: For each line in the file, xargs invokes the mkdir command, passing the line as an argument to create a directory will full client name.
To learn more about xargs checkout xargs command in Linux.
3. Using read command and while loop
We can use a command called "read" to read one line at a time from a file we specify. When we read each line, we also consider any spaces or special characters by using something called "Internal Field Separator" (IFS).
Then, we can use a "while" loop to keep doing this for each line in the file, and as we read each line, we use the "mkdir" command to create a new folder with the name we just read. This way, we can create separate folders for each line in the file, ensuring that we handle each line correctly, even if it contains spaces or special characters.
Command:
while IFS= read -r client_name;
do
mkdir "$client_name"
done < clients.txt
Output:

Explanation:
Let's understand this line by line,
while IFS= read -r client_name;
- while marks the starting of a while loop in the shell script.
- IFS= We are setting IFS to empty string.
IFS (Internal Field Separator) is a special variable used to separate items within a line of text. By default, IFS is set to whitespace characters. When we set IFS to an empty string, it means that each line from the clients.txt file should be treated as a single, complete unit. This way we ensure that spaces or special characters in a line won't be accidentally removed. It keeps everything in a line intact. - read -r client_name: The read command is used to read a line from the file and store it in the client_name variable.
Using -r option we are like telling the computer, "Please read the name exactly as it is, without changing anything." or treat it as a raw string. If you don't use the "-r" option and your names or text contain special characters or backslashes '\', the computer might interpret those characters differently and may give unexpected results or errors.
do: This word marks the beginning of our action in a while loop. Whatever we put after "do" is what we do for each line we read from the file.
mkdir "$client_name": We are creating folder for each client name stored in the variable client_name.
done < clients.txt
- done: This word tells us we're finished with our action for one line. After we've created the folder, we're ready to do the same for the next line.
- < clients.txt: Here we are using redirection operator to redirect file as an input to the read command. It's like saying, "Hey, read these lines from the 'clients.txt' file."
Conclusion
In this article we discussed how to deal with lots of command parameters, a practical way is to store them in a text file and use each line as a command argument. This saves time and effort. For example, if you have a text file with 100 client names, you can create a folder for each client without manually typing each command. The article explores three methods to achieve this: shell input redirection, xargs command, and read command with a while loop. Each method simplifies using file lines as command arguments, making command-line tasks more efficient.
Similar Reads
Bash Script - How to use Command Line Arguments
In this article, let us see about Command Line Arguments usage in Bash script. Arguments are inputs that are necessary to process the flow. Instead of getting input from a shell program or assigning it to the program, the arguments are passed in the execution part. Positional Parameters Command-line
4 min read
How to pass a list as a command-line argument with argparse?
Argparse is a Python library used to parse command-line arguments in a user-friendly manner. It makes it easy to write user-friendly command-line interfaces, and it is widely used in Python applications. In this tutorial, we will discuss how to pass a list as a command-line argument using the Argpar
5 min read
Command Line Arguments in Objective-C
In Objective-C, command-line arguments are strings of text that are passed to a command-line program when it is launched. They can be used to specify options or parameters that control the behavior of the program or to provide input data that the program can process. To access command-line arguments
2 min read
How to Parse Command Line Arguments in Node ?
Command-line arguments, in the context of a command-line interface (CLI), are text strings that provide extra information to a program when it is executed. In Nodejs, these arguments are accessible through an array known as argv (arguments values), where the shell passes all received command-line ar
3 min read
How to View the Content of File in Linux | cat Command
The cat command in Linux is more than just a simple tool, it's a versatile companion for various file-related operations, allowing users to view, concatenate, create, copy, merge, and manipulate file contents. Let's see the details of some frequently used cat commands, understanding each example alo
7 min read
Bash shell script to find out the largest value from given command line arguments
Write a shell script to find out the largest value from the given number of command-line arguments. Example: Special variables in bash: $@- All arguments. $#- Number of arguments. $0- Filename. $1, $2, $3, $4 ... - Specific arguments. Approach If the number of arguments is 0, end the program. If not
1 min read
Command Line Arguments in Golang
Command-line arguments are a way to provide the parameters or arguments to the main function of a program. Similarly, In Go, we use this technique to pass the arguments at the run time of a program. In Golang, we have a package called as os package that contains an array called as "Args". Args is an
2 min read
How to Save Output of Command in a File in Linux?
Linux has a robust command-line interface (CLI) that makes system interaction efficient for users. Saving a command's output to a file is among the most frequent activities in Linux. When working with big outputs, generating logs, or needing to save command results for later use, this is quite helpf
4 min read
Command Line Argument in Scala
The arguments which are passed by the user or programmer to the main() method are termed as Command-Line Arguments. main() method is the entry point of execution of a program. main() method accepts an array of strings. runtime. But it never accepts parameters from any other method in the program. Sy
2 min read
How to Print Second Command line Argument in shell?
Command line arguments are the parameters we pass to a command or script when we run it from the terminal. These arguments are passed as strings and can be accessed within your program to perform various operations. How to Print Second Command Line Argument in Shell?The shell stores the command line
5 min read