Skip to content

Instantly share code, notes, and snippets.

@sethbergman
Forked from awwsmm/printargs.sh
Created June 23, 2019 07:42
Show Gist options
  • Save sethbergman/1fb0e343dbc97c2213fdac6da00249c6 to your computer and use it in GitHub Desktop.
Save sethbergman/1fb0e343dbc97c2213fdac6da00249c6 to your computer and use it in GitHub Desktop.

Revisions

  1. @awwsmm awwsmm revised this gist Jun 21, 2019. 1 changed file with 12 additions and 3 deletions.
    15 changes: 12 additions & 3 deletions printargs.sh
    Original file line number Diff line number Diff line change
    @@ -1,10 +1,19 @@
    #!/usr/bin/env bash

    function printargs {
    #-------------------------------------------------------------------------------
    #
    # print_args - easily inspect arguments passed to a bash script
    #
    # sources:
    # https://unix.stackexchange.com/a/332126/183920
    #
    #-------------------------------------------------------------------------------

    function print_args {

    echo "arguments:"

    ii=1
    local ii=1
    for arg; do
    printf " \$%u: '%s'\n" "$ii" "$arg"
    ((ii++))
    @@ -14,7 +23,7 @@ function printargs {

    # usage:
    #
    # $ printargs 3 "3.a" "hello world"
    # $ print_args 3 "3.a" "hello world"
    # arguments:
    # $1: '3'
    # $2: '3.a'
  2. @awwsmm awwsmm revised this gist Jun 21, 2019. 1 changed file with 18 additions and 15 deletions.
    33 changes: 18 additions & 15 deletions printargs.sh
    Original file line number Diff line number Diff line change
    @@ -4,36 +4,39 @@ function printargs {

    echo "arguments:"

    ii=0
    ii=1
    for arg; do
    printf " %u: '%s'\n" "$ii" "$arg"
    printf " \$%u: '%s'\n" "$ii" "$arg"
    ((ii++))
    done

    }

    # usage:
    #
    # $ source ex.sh
    # $ printargs a b c
    # $ printargs 3 "3.a" "hello world"
    # arguments:
    # 0: 'a'
    # 1: 'b'
    # 2: 'c'
    # $1: '3'
    # $2: '3.a'
    # $3: 'hello world'

    function another {
    function example {

    echo "printing arguments passed to 'another'..."
    echo "printing arguments passed to 'example'..."
    printargs "$@"
    echo "...done."

    printf "\$2 = %s\n" "$2"

    }

    # example:
    # $ another "g h" 1
    # printing arguments passed to 'another'...
    #
    # $ example "a b" 5.5 ho-ho
    # printing arguments passed to 'example'...
    # arguments:
    # 0: ''
    # 1: 'g h'
    # 2: '1'
    # ...done.
    # $1: 'a b'
    # $2: '5.5'
    # $3: 'ho-ho'
    # ...done.
    # $2 = 5.5
  3. @awwsmm awwsmm created this gist Jun 21, 2019.
    39 changes: 39 additions & 0 deletions printargs.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,39 @@
    #!/usr/bin/env bash

    function printargs {

    echo "arguments:"

    ii=0
    for arg; do
    printf " %u: '%s'\n" "$ii" "$arg"
    ((ii++))
    done

    }

    # usage:
    #
    # $ source ex.sh
    # $ printargs a b c
    # arguments:
    # 0: 'a'
    # 1: 'b'
    # 2: 'c'

    function another {

    echo "printing arguments passed to 'another'..."
    printargs "$@"
    echo "...done."

    }

    # example:
    # $ another "g h" 1
    # printing arguments passed to 'another'...
    # arguments:
    # 0: ''
    # 1: 'g h'
    # 2: '1'
    # ...done.