LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   * and @ in substring extraction in bash (https://www.linuxquestions.org/questions/linux-newbie-8/%2A-and-%40-in-substring-extraction-in-bash-4175611116/)

vincix 08-02-2017 05:11 AM

* and @ in substring extraction in bash
 
I understand how a substring extraction is done,
Code:

${string:position:length}
but I don't understand how I can actually use * or @ in this context. The explanation from Advanced Bash Scripting isn't enough:

If the $string parameter is "*" or "@", then this extracts a maximum of $length positional parameters, starting at $position.
Code:

echo ${*:2} # Echoes second and following positional parameters.
echo ${@:2} # Same as above.
echo ${*:2:3} # Echoes three positional parameters, starting at second.

So it extracts the maximum of $length position parameteres, but what from, if the string is * or @?

Can you offer any concrete examples?

GazL 08-02-2017 05:31 AM

from the command-line arguments when it was invoked

Code:

test@ws1:~$ cat /tmp/example.sh
#!/bin/bash

echo "${@:1:2}"

test@ws1:~$ /tmp/example.sh one two three
one two


vincix 08-02-2017 05:35 AM

Right, I simply ignored the word group "positional parameter". That was clearly unambiguous. Thanks!

michaelk 08-02-2017 05:43 AM

Not sure I understand the question. By positional parameters the tldp is referencing command line arguments.
Code:

#!/bin/bash
echo $*
echo ${*:2:3}

./myscript.sh 1 2 3 4 5
1 2 3 4 5
2 3 4

A bit late...


All times are GMT -5. The time now is 12:02 PM.