Languages
[Edit]
EN

Bash - how to use command line arguments in script

0 points
Created by:
martineau
690

This article will show you how to use command-line arguments in your script in Bash.

In Bash, we can use arguments that were passed as parameters of our script. We can refer to them using $1 (first parameter), $2 (second argument), etc.

 

Practical example

my_script.sh
#!/bin/bash

echo $1
echo $2
echo $3

After calling the script by:

$ ./my_script.sh one two three

The output is:

one
two
three

Go through all given arguments

my_script.sh
#!/bin/bash

for parameter in "$@" 
do
    echo $parameter
done

Calling the script: 

$ ./my_script.sh one two three four

Output: 

one
two
three
four
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

Bash

Bash - how to use command line arguments in script
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join