BASH Programming

How to Pass a Filename as Argument in a Shell Script

The shell programming languages can be used for a variety of tasks like conducting operations and doing repetitive tasks for the automotive industry. We may execute our commands, applications, and shell scripts in a shell environment. You provide input and the program is then executed using that input. A program’s output is shown when it has completed running. How to provide a file name as input to a shell script is addressed in this article. To accomplish this, a script must be created using the “#!/bin/bash” shell. Now, we’ll see how to do this step-by-step.

Passing the Filename as an Argument in a Shell Script

A program that analyzes only a few files which can be valuable as a log of your actions. But a program that processes whatever files you choose is more beneficial. To demonstrate this, the “$” special term which stands for all of the control of the given parameters to the script can be used. A shell script is typically created for function series that a person requires to use frequently to save their effort. By simply typing the document on the command line, the users can start the series of commands that are contained in the shell script.

The command in this step now gets the file name as input. Here, our script is already placed on the desktop. Therefore, getting the desktop’s directory requires typing “c desktop/” before we can write the command to pass the file name as an argument in the shell script.

Linux@linux:~$ cd Desktop/

Then, in the following step, we open the script from the desktop because we already know that adding the shell is necessary for Bash shell scripting. To do this, type “#!/bin/bash”. Then, in the next line, we use the “echo” command. The echo command is used in the Bash shell scripting to print the statements. We use the inverted commas and type “The name of the file” inside of it. Then, in the following line, we use the echo command once more and supply the argument, this time is “$1”, to print. The first control argument that is passed to the shell script is $1. Another name for them is positional variables. In essence, this implies that the file name that we type into the prompt as an argument is saved in this “$1” variable.

#!/ bin/bash
echo “The name of the file is”
echo $1

Now, we use the command to pass the file name as an argument. To do this, we use the command in which we first type the dot slash “./”. Then, we type the name of the script which is “”. We then pass the argument which is the name of the file that we’re passing here and store the “” in “$1” . We must also add the “sh” extension to the argument.

Linux@linux:~/Desktop$ ./

When we execute this command, it displays the “The name of the file is” statement  on the terminal. In the next line, it also displays the file name “” which is supplied as an argument.

Now, in the following section, we use the “bash” keyword in the command to pass the file as an argument. For this, we use the command in which we first type “bash”. Then, we type the name of the script which is “” before passing the “” argument that is saved in the “$1” parameter.

Linux@linux:~/Desktop$ bash

So, as you can see, the displayed output on the terminal after this command is performed. It is identical to what was shown in the previous illustration.

Passing Multiple Filenames as Arguments to Shell Scripts

In this section, we pass multiple file names as arguments in the script. To do this, we first type the script, adding the shell with the “#!/bin/bash” command before using the echo command to pass a statement. However, before passing the statement, we must use the inverted commas. Inside these inverted commas, we pass the “Multiple files are” statement. Next, we type “echo”. Inside of this, we pass the parameters. This parameter stores the values that we enter as the filename as an argument in the terminal.

#!/ bin/bash
echo “Multiple files are”
echo $1 $2 $3 $4

We now enter the command to open the script on the terminal and add the file names as arguments. To begin, we first type “./” followed by the name of the script which is “,” and four arguments. The first argument is “” which is stored in “$1”. The second argument is “” which is stored in “$2.” The third name, “”, is stored for $3. And the fourth name which is “” is available for $4.

Linux@linux:~/Desktop$ ./,,

After executing this command, it first displays the “Multiple files are” statement followed by the names of the files that we pass as arguments. The first line displays “”. The second line displays “”. The third line displays “”. And the fourth line displays “”

Now, in the following section, we run this command using the “bash” keyword. It produces the same results as to those which are shown in the previous image. So, for this, we type “bash”. Then, we type “”. Then, follow the same steps as we performed with the previous function.

Linux@linux:~/Desktop$ bash,,

Therefore, when we run this command, it produces the same results as when we run the command before it where “./” is used.

Shell Script that Takes the Current File as an Argument

In this section, we pass the current file name, “”, as an argument. For this, we use the script in which we add the shell as we did in the previous section. Then, we use the “echo” command and pass the “the current file” statement in the next line. Finally, we pass the “$0” parameter inside of the “echo” which stores the actual name of the scripts.

#!/ bin/bash
echo “The current file is”
echo $0

Currently, we type “bash” followed by the name of the script which is “” on the terminal to display the output in the terminal window.

Linux@linux:~/Desktop$ bash

As you can see in the following image, after running this command, the output is split into two lines. The first of which has the “The current file” echo statement. The second of which includes the name of the script that we provided as a file name which is “”.


We covered how to pass a file name as an argument in Linux shell scripting in this article. In this article, we used a variety of methods to pass the file name. We examined how to pass a single file name as an input in the first section and how to save it in the “$1” parameter. In the second section, we showed how to use the parameters in the script to pass multiple filenames as arguments. In the third line, we demonstrated how to use the “$0” parameter to pass the current filename as input. In the command to open the script on the terminal, we also used the “bash” term.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.