Part 10 Introduction To Bash - Functions

Поділитися
Вставка
  • Опубліковано 27 січ 2025

КОМЕНТАРІ • 4

  • @michaelotu9723
    @michaelotu9723 4 роки тому +2

    The first arg pass on the terminal is $1, I also saw that the same was done for the first arg of a function. How do I differentiate between the two (when does, $1 refers to first arg from terminal and func

    • @bobbyiliev_
      @bobbyiliev_  4 роки тому +1

      Hi there, very good question!
      The file argument $1 will not be available inside your function. So if you want to pass your $1 argument to your function you need to add it after the name of the function when calling it.
      Here is an example script to test with:
      ```
      #!/bin/bash
      echo "Argument " ${1}
      function test(){
      echo "Function Argument: " ${1}
      }
      test Fun
      ```

    • @michaelotu9723
      @michaelotu9723 4 роки тому +1

      @@bobbyiliev_ assuming you do, `bash filename.sh Bobby` the output will be `Argument Bobby`
      `Function argument Fun`
      But `${1}` was used for both which made me confused a little. Thanks for the clarification

    • @bobbyiliev_
      @bobbyiliev_  4 роки тому

      @@michaelotu9723 Hi there! Yep exactly, you got it!
      Thanks for the great question!