Languages
[Edit]
EN

Bash - how to use command line arguments in script

3 points
Created by:
martineau
1320

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), "$3" (third argument), etc.

 

Practical example

Example my_script.sh file:

#!/bin/bash

echo "$1"
echo "$2"
echo "$3"

Run in command line:

./my_script.sh one two three

Output:

one
two
three

Go through all given arguments

Example my_script.sh file:

#!/bin/bash

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

Run in command line:

./my_script.sh one two three four

Output: 

one
two
three
four

 

Alternative titles

  1. Bash - pass argument as variable to script file
  2. Bash - pass argument as variable into script.sh file
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