How to pass arguments to a script?

QuestionsQuestions8 SkillsProPermissions of FilesSep, 01 2025
0107

To pass arguments to a Bash script, you can include them after the script name when you run it. Here's how to do it:

  1. Create a script (e.g., script.sh):

    #!/bin/bash
    echo "First argument: $1"
    echo "Second argument: $2"
  2. Make the script executable:

    chmod +x script.sh
  3. Run the script with arguments:

    ./script.sh "Hello" "World"

In this example:

  • $1 refers to the first argument ("Hello").
  • $2 refers to the second argument ("World").

You can pass as many arguments as you need, and they can be accessed using $3, $4, etc. The special variable $@ can be used to refer to all arguments.

If you have any further questions or need clarification, feel free to ask!

0 Comments

no data
Be the first to share your comment!